如何阻止其他应用?

时间:2017-12-23 18:45:01

标签: java android

我已经看到很多家长控制&生产力应用程序可以通过在安装时请求您允许用户访问来阻止您使用您希望的某些应用程序。他们如何做到这一点,任何帮助或相关信息表示赞赏

1 个答案:

答案 0 :(得分:1)

这些应用只是检查当前正在运行的任务,然后终止他们的活动。

    public class TaskChecker{
    public static String getForegroundApplication(Context context){
        ActivityManager am=(ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
        RunningTaskInfo foreground=am.getRunningTasks(1).get(0);
        return foreground.topActivity.getPackageName();
    }
}

这段代码只是检查当前的运行情况,看看它是否是一项活动。这就是你如何使用该代码阻止WhatsApp

String currentRunningApp = TaskChecker.getForegroundApplication(yourContext);

if(currentRunningApp.equals("com.whatsapp")){
ActivityManager am = (ActivityManager)yourContext.getSystemService(Context.ACTIVITY_SERVICE);    

    Intent startMain = new Intent(Intent.ACTION_MAIN); 
    startMain.addCategory(Intent.CATEGORY_HOME); 
    startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    this.startActivity(startMain);

    am.killBackgroundProcesses(currentRunningApp );
}

PS您需要以下权限,并确保每隔一分钟从服务运行

<android:name="android.permission.GET_TASKS">
<android:name="android.permission.KILL_BACKGROUND_PROCESSES">
相关问题