1. Download our Official Android App: Forums for Android!

Apps How to avoid crashes and update time in this gps-application?

Discussion in 'Android Development' started by Xerxes Linde, Jul 30, 2016.

  1. Xerxes Linde

    Xerxes Linde Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    2
    Joined:
    Mar 7, 2012

    Mar 7, 2012
    2
    0
    5
    My goal is to get the gps-location continuously about every second. This is what I tried, I stripped it down to the GPS-part. Not only does it not get the GPS-location every second, it also crashes after some time, meaning, that no new position is being fetched anymore. Also it seems to be a huge burden on the phone performancewise.

    Code (Java):
    1. package com.me.bluetoothtest;
    2.  
    3.  
    4. public class MainActivity extends AppCompatActivity {
    5.  
    6. private static final int PERMISSION_ACCESS_COARSE_LOCATION = 0;
    7. private static final int PERMISSION_ACCESS_FINE_LOCATION = 2;
    8. private static final int PERMISSION_WRITE_EXTERNAL_STORAGE = 3;
    9.  
    10. LocationManager mLocationManager;
    11. LocationListener mLocLi;
    12.  
    13. @Override
    14. protected void onCreate(Bundle savedInstanceState) {
    15.  
    16.   mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    17.  
    18.   genehmigen(android.Manifest.permission.ACCESS_COARSE_LOCATION, PERMISSION_ACCESS_COARSE_LOCATION);
    19.   genehmigen(android.Manifest.permission.ACCESS_FINE_LOCATION, PERMISSION_ACCESS_FINE_LOCATION);
    20.   genehmigen(android.Manifest.permission.WRITE_EXTERNAL_STORAGE, PERMISSION_WRITE_EXTERNAL_STORAGE);
    21.  
    22.  
    23.  
    24.   if (mLocLi == null) {
    25.    mLocLi = new LocationListener() {
    26.     @Override
    27.     public void onLocationChanged(Location location) {
    28.  
    29.      aktuell = location;
    30.      Log.v("Bluetooth", aktuell.getLatitude() + " " + aktuell.getLongitude());
    31.  
    32.     }
    33.  
    34.     @Override
    35.     public void onStatusChanged(String provider, int status, Bundle extras) {
    36.  
    37.     }
    38.  
    39.     @Override
    40.     public void onProviderEnabled(String provider) {
    41.  
    42.     }
    43.  
    44.     @Override
    45.     public void onProviderDisabled(String provider) {
    46.  
    47.     }
    48.    };
    49.  
    50.    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    51.     mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000,
    52.      2, mLocLi);
    53.     mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000,
    54.      2, mLocLi);
    55.    }
    56.   }
    57.  
    58. }
    59.  
    60.  
    61.  
    62. private void genehmigen(String Erlaubnis, int AnfrageCode) {
    63.   if (ActivityCompat.checkSelfPermission(this, Erlaubnis) != PackageManager.PERMISSION_GRANTED) {
    64.    ActivityCompat.requestPermissions(this, new String[] {
    65.      Erlaubnis
    66.     },
    67.     AnfrageCode);
    68.   }
    69. }
    70.  
    71. @Override
    72. public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    73.  
    74.   if (requestCode == PERMISSION_ACCESS_COARSE_LOCATION || requestCode == PERMISSION_ACCESS_FINE_LOCATION) {
    75.    if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
    76.     mLocLi = new LocationListener() {
    77.      @Override
    78.      public void onLocationChanged(Location location) {
    79.       aktuell = location;
    80.       Log.v("Bluetooth", aktuell.getLatitude() + " " + aktuell.getLongitude());
    81.      }
    82.  
    83.      @Override
    84.      public void onStatusChanged(String provider, int status, Bundle extras) {}
    85.  
    86.      @Override
    87.      public void onProviderEnabled(String provider) {}
    88.  
    89.      @Override
    90.      public void onProviderDisabled(String provider) {}
    91.     };
    92.     if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
    93.      if (!mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
    94.       mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000,
    95.        2, mLocLi);
    96.      if (!mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
    97.       mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000,
    98.        2, mLocLi);
    99.     }
    100.    } else {
    101.     Toast.makeText(this, "Abgelehnt",
    102.      Toast.LENGTH_LONG).show();
    103.     //cancelOperation();
    104.    }
    105.   }
    106. }
    107.  
    108.  
    109. @Override
    110. protected void onPause() {
    111.   super.onPause();
    112.   try {
    113.  
    114.    mBluetoothAdapter.cancelDiscovery();
    115.    if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    116.  
    117.     ActivityCompat.requestPermissions(this, new String[] {
    118.       android.Manifest.permission.ACCESS_COARSE_LOCATION
    119.      },
    120.      1000);
    121.    }
    122.    if (this != null) {
    123.     if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    124.  
    125.      ActivityCompat.requestPermissions(this, new String[] {
    126.        android.Manifest.permission.ACCESS_FINE_LOCATION
    127.       },
    128.       1000);
    129.     }
    130.    }
    131.    if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
    132.  
    133.     ActivityCompat.requestPermissions(this, new String[] {
    134.       android.Manifest.permission.WRITE_EXTERNAL_STORAGE
    135.      },
    136.      1000);
    137.    }
    138.    mLocationManager.removeUpdates(mLocLi);
    139.   } catch (Exception m) {
    140.  
    141.   }
    142. }
    143.  
    144. @Override
    145. protected void onDestroy() {
    146.   super.onDestroy();
    147.   if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    148.  
    149.    ActivityCompat.requestPermissions(this, new String[] {
    150.      android.Manifest.permission.ACCESS_COARSE_LOCATION
    151.     },
    152.     1000);
    153.   }
    154.   if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    155.  
    156.    ActivityCompat.requestPermissions(this, new String[] {
    157.      android.Manifest.permission.ACCESS_FINE_LOCATION
    158.     },
    159.     1000);
    160.   }
    161.   if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
    162.  
    163.    ActivityCompat.requestPermissions(this, new String[] {
    164.      android.Manifest.permission.WRITE_EXTERNAL_STORAGE
    165.     },
    166.     1000);
    167.   }
    168.   mLocationManager.removeUpdates(mLocLi);
    169. }
    170.  
    171. private class DownloadFilesTask extends AsyncTask < URL, Integer, Long > {
    172.   protected Long doInBackground(URL...urls) {
    173.    if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    174.     Location l = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    175.     if (l == null)
    176.      l = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    177.     if (l == null)
    178.      return totalSize;
    179.    }
    180.    return totalSize;
    181.   }
    182. }
    183. }

    What is the reason? I presume some causes to be around
    Code (Java):
    1. Location l = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    but I haven't found a working example for my purpose, yet.
     

    Advertisement

  2. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #23
    Points:
    1,488
    Posts:
    6,700
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    6,700
    9,398
    1,488
    Male
    Software developer
    South West of England
    You need to:-

    1. Run the application from Android Studio on a device or emulator
    2. When the app crashes, capture the stack trace from the Logcat output and post it here.
     

Share This Page

Loading...