如何在Android中获取正在运行的服务列表?

use*_*058 3 service android list

如何在android代码中获取正在运行的服务列表并通过短信发送SMSManager

anj*_*aly 8

在您的AndroidManifest.xml文件中包含以下权限

<uses-permission android:name="android.permission.SEND_SMS" />
Run Code Online (Sandbox Code Playgroud)

此方法显示两个任务:

public void sendSMS {
    // This is the code to find the running services
    ActivityManager am = (ActivityManager)this.getSystemService(Activity.ACTIVITY_SERVICE);
    List<ActivityManager.RunningServiceInfo> rs = am.getRunningServices(50);
    String message = null;

    for (int i=0; i<rs.size(); i++) {
        ActivityManager.RunningServiceInfo rsi = rs.get(i);
        Log.i("Service", "Process " + rsi.process + " with component " + rsi.service.getClassName());
        message =message+rsi.process ;
    }

    //This is the code to send sms.
    String phoneNumber = "0123456789";
    SmsManager smsManager = SmsManager.getDefault();
    smsManager.sendTextMessage(phoneNumber, null, message, null, null);
}
Run Code Online (Sandbox Code Playgroud)

方法细节.

public void sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent)
Run Code Online (Sandbox Code Playgroud)


Vip*_*hit 5

你可以这样做:

ActivityManager am = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> rs = am.getRunningServices(50);

for (int i=0; i<rs.size(); i++) {
  ActivityManager.RunningServiceInfo
  rsi = rs.get(i);
  Log.i("Service", "Process " + rsi.process + " with component " + rsi.service.getClassName());
}
Run Code Online (Sandbox Code Playgroud)

  • @SaddaHussain 出于隐私原因,该 API 自 Android 6.0 起不再可用。它只会返回属于调用者包的正在运行的服务 (2认同)

Jar*_*vis 5

就像这样:

ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
      String serv = service.service.getClassName();
}
Run Code Online (Sandbox Code Playgroud)