外部进程启动时停止服务

时间:2012-03-09 15:58:53

标签: java android service process

是否可以在启动外部应用程序时停止服务?

对于活动,您可以使用getRunningAppProcesses()并在列表中检查您想要的应用是否正在运行。但是服务呢?

1 个答案:

答案 0 :(得分:0)

使用此:

public class TestService extends Service {

    @Override
    public void onCreate()
    {
        ActivityManager am = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
        List rs = am.getRunningServices(50);

        for (int i=0; i<rs.size(); i++) {
        ActivityManager.RunningServiceInfo rsi = (RunningServiceInfo) rs.get(i);
        Log.i("Service", "Process " + rsi.process + " with component " + rsi.service.getClassName());
        }
        super.onDestroy();
        Toast.makeText(getApplicationContext(),"1. onCreate()", Toast.LENGTH_SHORT).show();
    }
}