且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检查Android中所有正在运行的服务?

更新时间:2022-11-06 18:08:10

下面是一个完整的答案。

Here is a complete answer.

步骤1.首先,你需要声明和初始化几个变量: -

Step 1. First you need to declare and initialize few variables :-

private static final String APP_DETAILS_PACKAGE_NAME = "com.android.settings"; // Here you need to define the  package name

private static final String SCREEN_CLASS_NAME = "com.android.settings.RunningServices"; // Here you need to define the class name but NOTICE!! you need to define its full name including  package name.

步骤2.实例化一个意向

Step 2. Instantiate an Intent

Intent intent = new Intent();

第三步:将动作设置为ACTION_VIEW

Step 3. Set Action to ACTION_VIEW

intent.setAction(Intent.ACTION_VIEW);

因为我们知道,包可以有多个活动的意图内第3步:设置类名。因此,意图需要的东西相匹配的包名内的活动。

Step 3. Set class name inside the intent as we know that package can have more than one activity. So Intent needs something to match the Activity inside the package name.

intent.setClassName(APP_DETAILS_PACKAGE_NAME,  SCREEN_CLASS_NAME); //Here you need to set package name as well as class name so it could refer to the Package and IntentFilter of the given Activity.

步骤4.启动活动

context.startActivity(intent); // As soon as this line will be executed Running Service screen will be displayed as a foreground activity.

在上面的例子中,如果你想访问一些不同的屏幕,然后改变APP_DETAILS_PACKAGE_NAME和SCREEN_CLASS_NAME根据您的需要。

In above example if you want to access some different screen then change the APP_DETAILS_PACKAGE_NAME and SCREEN_CLASS_NAME as per your need.

我真的不知道,这种方法是记录或没有,但它的作品般的魅力对我来说。

I really don't know that this method is documented or not but it works like charm for me.