1

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.

Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
Bipin Gawand
  • 521
  • 1
  • 6
  • 17
  • 1
    which device have you tested? some device don't support service run after you kill app like HUAWEI and XIOMI – Linh Feb 13 '17 at 03:37
  • tested on emulator and asus – Bipin Gawand Feb 13 '17 at 03:54
  • 1
    This link might help you :http://stackoverflow.com/questions/15758980/android-service-needs-to-run-always-never-pause-or-stop – d1vivek681065 Feb 13 '17 at 04:47
  • Possible duplicate of [Android Service needs to run always (Never pause or stop)](http://stackoverflow.com/questions/15758980/android-service-needs-to-run-always-never-pause-or-stop) – David Rawson Feb 13 '17 at 04:48

3 Answers3

1

You should look into JobScheduler

Here are some references.

https://developer.android.com/reference/android/app/job/JobScheduler.html

http://www.vogella.com/tutorials/AndroidTaskScheduling/article.html

https://github.com/googlesamples/android-JobScheduler/tree/master/Application/src/main/java/com/example/android/jobscheduler

Pratik Popat
  • 2,891
  • 20
  • 31
0

use stopSelf() inside service where you to destroy the service

bind service docs here

service connection docs here

example bind and service connection here

Hemanth S
  • 669
  • 6
  • 16
  • using stopSelf() my service gets stopped...but i need long running service which should not destroy by os or apps – Bipin Gawand Feb 13 '17 at 03:39
  • If your service don't want to destroy means you have `bind service` and or use `service connection` for long running service by this method you can prevent to destroy – Hemanth S Feb 13 '17 at 03:42
  • service is new topic for me. can you please briefly explain or give some links to study?? – Bipin Gawand Feb 13 '17 at 03:46
0

This is the code of just work around I am not supporting it. So if your service get stopped it can restart by registering in a Broad cast receiver. put this on your manifest

<receiver android:name=".RestartTrigger">
            <intent-filter>
                <action android:name="donotkill" />
            </intent-filter>
        </receiver>

And just add this class

public class RestartTrigger extends BroadcastReceiver {
    private static final String TAG = "RestartServiceReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.e(TAG, "onReceive");
                    Intent intent1 = Intent(context,GetLocationService.class);
            context.startService(intent1);


    }
}

And in the service add this

@Override
    public void onDestroy() {
        super.onDestroy();

            sendBroadcast(new Intent("donotkill"));
    }

And in Manifest yo should make your service as separate process add android:process=":name_of_process"

<service android:name=".GetLocationService" android:stopWithTask="false" android:process=":name_of_process"   android:exported="true" android:enabled="true" />

And I am do not support this kind of code. It will be a hazard to the user.

Kiran Benny Joseph
  • 6,755
  • 4
  • 38
  • 57