以编程方式阻止android应用 [英] Blocking android apps programmatically

查看:79
本文介绍了以编程方式阻止android应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图开发这样的应用程序,从某种意义上讲,我想使用所需的密码锁定设备中的所有应用程序.但是我没有找到解决方案的任何代码.所以我自己开发了一个,不幸的是它没有成功.我找到了许多用于锁定android设备的解决方案,但没有找到一种用于锁定应用程序的解决方案.如果您提出解决方案,将非常高兴.

I tried to develop such an app, in the sense I want to lock all the applications in my device with a password whatever I want. But I didn't find any piece of code for the solution. So I developed one by myself and unfortunately it didn't succeed. I found many solutions for locking android devices but, didn't find one for locking an app. Will be glad if you suggest a solution.

推荐答案

我使用了后台服务来检查哪个应用程序位于前台(这意味着该应用程序正在被用户使用).然后检查是否需要锁定应用程序.

I used a background service to check which application is in the foreground (which means that application is being used by the user). Then I check to see whether I need to lock the application or not.

要查找所有已安装的应用程序(不包括系统应用程序)的列表,请执行以下操作:

To find the list of all installed applications (excluding system applications):

PackageManager packageManager = getPackageManager();
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);

List<ResolveInfo> appList = packageManager.queryIntentActivities(mainIntent, 0);
Collections.sort(appList, new ResolveInfo.DisplayNameComparator(packageManager));
List<PackageInfo> packs = packageManager.getInstalledPackages(0);
for (int i = 0; i < packs.size(); i++) {
    PackageInfo p = packs.get(i);
    ApplicationInfo a = p.applicationInfo;
    // skip system apps if they shall not be included
    if ((a.flags & ApplicationInfo.FLAG_SYSTEM) == 1) {
        continue;
    }
    appList.add(p.packageName);
}

要找到当前的前台应用程序,请执行以下操作:

To find the current foreground application:

ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> RunningTask = mActivityManager.getRunningTasks(1);
ActivityManager.RunningTaskInfo ar = RunningTask.get(0);
activityOnTop=ar.topActivity.getClassName();

在这里,类名提供了应用程序的程序包名称.我建议您使用程序包名称来标识任何应用程序,因此我们知道程序包名称始终是唯一的.

Here the class-name provides the package name of the application. I suggest that you use the package name to identify any application so we know that the package name is always unique.

现在,用于锁定应用程序的功能:

要查找哪个应用程序在前台运行并想对其进行锁定,我们只需启动另一个活动,该活动包含用于密码的EditText以及确定"和取消"按钮.

To find which application is running in the foreground and want to lock it, we just have to start another activity which has an EditText for password and OK and Cancel button.

Intent lockIntent = new Intent(mContext, LockScreen.class);
lockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(lockIntent);

单击确定",如果密码正确,则只需完成 LockScreen活动.如果密码不正确,则只需使用下面的代码,该代码将关闭应用程序并显示设备的主屏幕:

On click of OK, if the password is correct then simply finish the LockScreen activity. If the password is incorrect then simply use the code below, which closes the application and shows the home screen of the device:

Intent startHomescreen = new Intent(Intent.ACTION_MAIN);
startHomescreen.addCategory(Intent.CATEGORY_HOME);
startHomescreen.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(startHomescreen);

取消按钮上也使用了相同的代码.

The same code is also used on the cancel button.

这篇关于以编程方式阻止android应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆