Reputation: 283
i'm making a simple Activity about killing running services. i found a method "killBackgroundProcesses" to kill background app or servies. i tried killing background apps and services. running apps and some servies are killed well, but especially Music Service is never killed...
I want to make a simeple app which is killing background music service. the app has just one button(name is "Kill Music") If user clicks the button, the background music should be killed.
the music is played by default music player.. how can i kill the background music? is there any other method to kill music service?
here is my code i tried.. any suggestion will be appreciate. thank you.
public class Android32_TaskKillerActivity extends Activity {
ActivityManager manager;
List<RunningServiceInfo> list;
String str=""; //to contain running Servies name and write on Toast message.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button bt = (Button)findViewById(R.id.button1); //to kill All Services
manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
list = manager.getRunningServices(300); //save current Servies to List
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for(int i=0; i<list.size(); i++){
RunningServiceInfo info = list.get(i);
String temp = info.service.getPackageName();
str += temp+"\n";
manager.killBackgroundProcesses(temp);//killing services one by one to the end.
}
Toast.makeText(getBaseContext(), "All Services are finished \n" + str, 3000).show();
}
});
}
}
Upvotes: 1
Views: 1079
Reputation: 1007554
I want to make a simeple app which is killing background music service. the app has just one button(name is "Kill Music") If user clicks the button, the background music should be killed.
This is not possible.
killBackgroundProcesses()
will kill background processes. However, there are multiple definitions of the word "background" (and, conversely, "foreground"). The Music app has called startForeground()
to indicate to Android that it is part of the foreground user experience and should not be shut down due to low memory conditions if possible.
Quoting the killBackgroundProcesses()
documentation:
This is the same as the kernel killing those processes to reclaim memory; the system will take care of restarting these processes in the future as needed.
Hence, killBackgroundProcesses()
should not be killing services that use startForeground()
, such as the Music app.
The user can stop the Music app either through the Music app or, if they are a particularly strange user, via the Settings application.
Upvotes: 3