Android service stopped once clear apps. I need a service that is continuously running in the background even if the user clears all apps. I created an alarm for starting a service.
public class AlarmReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {
Log.d("Alarm","Alarm receive");
Intent i=new Intent(context,GetLocationService.class);
context.startService(i);
}
}
My Service file
public class GetLocationService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(getApplicationContext(),"calling Get Location service", Toast.LENGTH_LONG).show();
//service code here
return Service.START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d("Service","Service destroy");
}
}
Manifest File
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<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:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity" android:configChanges="keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".GetLocationService" android:stopWithTask="false" android:exported="false" />
<receiver android:name=".AlarmReceiver" android:enabled="true" />
</application>
Now I need to track mobile location even if activity destroy..
but in my case once I clear apps my service is destroyed without executing on destroy method. I have read about STICKY_INTENT
but it didn't work for me.