Created
August 6, 2015 09:01
-
-
Save dp-singh/06ec55fd6f5700e15aac to your computer and use it in GitHub Desktop.
Managing service if it killed by the user
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Sticky Service | |
public class StickyService extends Service | |
{ | |
private static final String TAG = "StickyService"; | |
@Override | |
public IBinder onBind(Intent arg0) { | |
// TODO Auto-generated method stub | |
return null; | |
} | |
@Override | |
public int onStartCommand(Intent intent, int flags, int startId) { | |
Log.e(TAG, "onStartCommand"); | |
return START_STICKY; | |
} | |
@Override | |
public void onDestroy() { | |
super.onDestroy(); | |
sendBroadcast(new Intent("YouWillNeverKillMe")); | |
} | |
} | |
//Restart Service Receiver | |
public class RestartServiceReceiver extends BroadcastReceiver | |
{ | |
private static final String TAG = "RestartServiceReceiver"; | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
Log.e(TAG, "onReceive"); | |
context.startService(new Intent(context.getApplicationContext(), StickyService.class)); | |
} | |
} | |
//Manifest.xml | |
<service android:name=".StickyService" > | |
</service> | |
<receiver android:name=".RestartServiceReceiver" > | |
<intent-filter> | |
<action android:name="YouWillNeverKillMe" > | |
</action> | |
</intent-filter> | |
</receiver> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment