android - 如何杀死音乐服务?

标签 android service

我正在制作一个关于终止正在运行的服务的简单 Activity 。 我找到了一种方法“killBackgroundProcesses”来终止后台应用程序或服务。 我试过杀死后台应用程序和服务。 正在运行的应用程序和一些服务被很好地杀死,但特别是音乐服务永远不会被杀死......

我想制作一个终止背景音乐服务的简单应用程序。 该应用程序只有一个按钮(名称为“Kill Music”) 如果用户点击按钮,背景音乐应该停止。

默认音乐播放器播放音乐.. 我怎样才能杀死背景音乐? 有没有其他方法可以杀死音乐服务?

这是我试过的代码.. 任何建议将不胜感激。 谢谢。

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();

        }
    });

}
}

最佳答案

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.

这是不可能的。

killBackgroundProcesses() 将终止后台 进程。但是,“背景”(反之,“前景”)一词有多种定义。音乐应用已调用 startForeground() 以向 Android 表明它是前台用户体验的一部分,如果可能,不应因内存不足而关闭。

引用 killBackgroundProcesses() 文档:

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.

因此,killBackgroundProcesses() 不应终止使用 startForeground() 的服务,例如音乐应用。

用户可以通过音乐应用程序停止音乐应用程序,或者如果他们是一个特别陌生的用户,可以通过设置应用程序停止音乐应用程序。

关于android - 如何杀死音乐服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8287119/

相关文章:

unit-testing - 带有模拟服务的单元测试过滤器

forms - Symfony2 : Difference between those metodology "Embedded Form" and "Data Transformer"

java - 即使我有 wifi 和移动数据,在室内时 isNetworkEnabled == false

java - Android 模拟器 Nexus 5x 加载错误的可绘制大小?

android - Phonegap/Cordova 中的 "Review this App"链接

android - 从异步服务更新 Activity

java - Android 如何响应主页点击

java - Android 服务在 OPPO 小米 MIUI vivio 等某些设备上停止工作

android - 如何获取 Android Gradle 插件正在使用的 R8 版本?

android - AsyncTask 不调用 onPostExecute()