Good day. Such a problem was written by the service, which in the background collects coordinates with GPS. The problem is what when gps is enabled on the phone, the application works and collects the coordinates even if the application backs up to kill the application, but as soon as the gps on the device is turned off, the service also dies and when the gps is turned back on, the service does not start (PS , suddenly the service did not die, but went to bed). Broadcast receiver below
public class GPSReciever extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO: This method is called when the BroadcastReceiver is receiving // an Intent broadcast. Log.wtf("backservice","in RECIEVER"); context.startService(new Intent(context,GPSService.class)); } } Service Code that catches gps in the background
public class GPSService extends Service { private LocationManager locationManager; private NotificationManager notificationManager; private DBHelper dbHelper; private SQLiteDatabase db; public GPSService() { } @Override public void onCreate() { super.onCreate(); locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { Log.wtf("backservice", "permissions denied"); } locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000 * 10, 10, locationListener); locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000 * 10, 10, locationListener); locationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 1000 * 10, 10, locationListener); notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Log.wtf("backservice", "Notification manager enabled" + notificationManager.toString()); dbHelper = new DBHelper(getApplicationContext()); db = dbHelper.getWritableDatabase(); } @Override public void onDestroy() { super.onDestroy(); locationManager.removeUpdates(locationListener); dbHelper.close(); Log.wtf("backservice", "GPSService Destroyed"); } @Override public void onTaskRemoved(Intent rootIntent) { //super.onTaskRemoved(rootIntent); dbHelper.close(); startService(new Intent(this, getClass())); Log.wtf("backservice", "GPSService TASKREMOVED!"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { }else { locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000 * 10, 10, locationListener); locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000 * 10, 10, locationListener); locationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 1000 * 10, 10, locationListener); } Log.wtf("backservice", "GPS service is started call from onStartCommand"); if (ActivityCompat.checkSelfPermission(GPSService.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(GPSService.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { Notification.Builder nb = new Notification.Builder(getApplicationContext()).setTicker("have no permissions for gps") .setContentText("Please give permissions to use ur gps") .setContentTitle("Need some permissions"); Intent fIntent = new Intent(this, MainActivity.class); PendingIntent pIntent = PendingIntent.getActivity(this, 0, fIntent, 0); nb.setContentIntent(pIntent) .setWhen(System.currentTimeMillis()) .setSmallIcon(R.drawable.ic_launcher_foreground) .setDefaults(Notification.DEFAULT_ALL); Notification nf = nb.build(); notificationManager.notify(0, nf); return START_NOT_STICKY; } locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); return START_STICKY_COMPATIBILITY; } private LocationListener locationListener = new LocationListener() { @Override public void onLocationChanged(Location location) { ContentValues cv = new ContentValues(); cv.put("latitude",location.getLatitude()); cv.put("longitude",location.getLongitude()); cv.put("stamp",location.getTime()); long rawId = db.insert("positions",null,cv); Log.wtf("backservice", "onlocation changed" + location.toString()); Log.wtf("backservice","ID in database->"+rawId); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { switch (provider) { case LocationManager.GPS_PROVIDER: Log.wtf("backservice", "GPS provider status changed to " + status); if(status!= LocationProvider.AVAILABLE){ Log.wtf("backservice","GPS IS UNVALIABLE, so from there we can send data to back"); } break; case LocationManager.NETWORK_PROVIDER: Log.wtf("backservice", "Network provider status changed to " + status); break; case LocationManager.PASSIVE_PROVIDER: Log.wtf("backservice", "Passive provider status changed to " + status); break; } if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then overriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. Log.wtf("backservice","Permissions denied"); }else{ Log.wtf("backservice", "Call from StatusChanged:"+String.valueOf(locationManager.getLastKnownLocation(provider))); } } @Override public void onProviderEnabled(String provider) { switch (provider) { case LocationManager.GPS_PROVIDER: Log.wtf("backservice", "GPS ENABLED"); break; case LocationManager.NETWORK_PROVIDER: Log.wtf("backservice", "NETWORK PROVIDER ENABLED"); break; case LocationManager.PASSIVE_PROVIDER: Log.wtf("backservice", "PASSIVE PROVIDER ENABLED"); break; default: break; } if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then overriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. Log.wtf("backservice","Permissions denied"); }else{ Log.wtf("backservice", "Call from ProviderEnabled"+String.valueOf(locationManager.getLastKnownLocation(provider))); } } @Override public void onProviderDisabled(String provider) { switch (provider){ case LocationManager.GPS_PROVIDER: Log.wtf("backservice","GPS DISABLED"); break; case LocationManager.NETWORK_PROVIDER: Log.wtf("backservice","NETWORK PROVIDER DISABLED"); break; case LocationManager.PASSIVE_PROVIDER: Log.wtf("backservice","PASSIVE PROVIDER DISABLED"); break; default: break; } }; @Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. return null; } } And the manifesto itself
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="kz.dokazhi.sendinggps" android:installLocation="internalOnly"> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <uses-permission android:name="android.permission.QUICKBOOT_POWERON" /> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <receiver android:enabled="true" android:exported="true" android:name="kz.dokazhi.sendinggps.services.GPSReciever"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"/> <action android:name="android.intent.action.QUICKBOOT_POWERON" /> </intent-filter> </receiver> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".services.GPSService" android:enabled="true" android:exported="true" android:process=":gpsproc"/> </application> </manifest> Please explain why this is happening and how to fix it.