如何控制服务中正在运行的应用程序?

时间:2018-02-27 10:52:21

标签: java android service android-service

我按进程名称检查。但我想按应用程序名称搜索。我怎样才能做到这一点 ?例如,facebook,whatsapp,instagram等检查应用程序是否正在运行?我应该为此发送什么字符串值? isnamedprocessrunning(“com.example.processlis”);

将应用程序名称作为字符串,并相应地找到它

MyService.java

public class MyService extends Service {

@Override
public void onCreate() {
    super.onCreate();
    isNamedProcessRunning("com.example.processlistener");


}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return super.onStartCommand(intent, flags, startId);
}

@Nullable
@Override
public IBinder onBind(Intent ıntent) {
    return null;
}

boolean isNamedProcessRunning(String processName) {
    if (processName == null)
        return false;

    ActivityManager manager =
            (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> processes = manager.getRunningAppProcesses();
    for (ActivityManager.RunningAppProcessInfo process : processes) {
        if (processName.equals(process.processName)) {
            Toast.makeText(this, "Same", Toast.LENGTH_SHORT).show();
            return true;
        }
    }
    Toast.makeText(this, "Different", Toast.LENGTH_SHORT).show();
    return false;
}

         }

MainActivity.java

  Intent startIntent = new Intent(this,MyService.class);
    startService(startIntent);

0 个答案:

没有答案