0

Im developing an application and I need to block some apps from one service.

¿How can I kill those applications services and prevent them to restart when my service is running?

Thank you very much :)

Rodrigo Cifuentes Gómez
  • 1,304
  • 2
  • 13
  • 28
  • This link will help you ask a better question. http://stackoverflow.com/about You should start with google or the android documentation – nexus_2006 Sep 14 '13 at 23:08

1 Answers1

0

You can stop a service by calling

context.stopService(Intent intent);

That intent has to be the one you use to start the service itself, for example:

Intent i = new Intent(context, MyService.class);
context.stopService(i);

Now you have to run this code in regular intervals so you can be sure you're killing the service while not using too much CPU. This can be achieved with an AlarmManager, you can find the documentation here: http://developer.android.com/reference/android/app/AlarmManager.html

You'll have to get an AlarmManager instance, set it to repeat every N milliseconds and run a Receiver:

int interval = 120000;

Intent i = new Intent(context, MyBroadcastReceiver.class);
PendingIntent action = PendingIntent.getBroadcast(context, 0, i, 0);

AlarmManager am = Context.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, action);

Interval is in milliseconds and sets how often your "onReceive()" will fire. The longer the interval, the less battery you use.

And MyBroadcastReceiver will be a class which extends BroadcastReceiver and overrides the onReceive() method

 @Override
 public void onReceive(Context context, Intent intent) 
 {   
     //STOP THE SERVICE HERE
 }
LuigiPower
  • 1,113
  • 10
  • 20