[英]I want to kill/ close a particular app by pressing a button in my current app. How can I do this?
Here's what i want: My app is having a button for closing another independent application say - Google map. 这就是我想要的:我的应用程序有一个用于关闭另一个独立应用程序的按钮-Google地图。 I want to know if the process has started and if it does then i want to close it by pressing that button.
我想知道该过程是否已经开始,如果已经开始,那么我想通过按该按钮将其关闭。
You can use the BroadcastReceiver Class. 您可以使用BroadcastReceiver类。 Try this way.
尝试这种方式。
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (isAppForground(context)) {
// App is in Foreground
} else {
// App is in Background
}
}
public boolean isAppForground(Context mContext) {
ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasks = am.getRunningTasks(1);
if (!tasks.isEmpty()) {
ComponentName topActivity = tasks.get(0).topActivity;
if (!topActivity.getPackageName().equals(mContext.getPackageName())) {
return false;
}
}
return true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.