阻止Android应用程序编程应用程序、Android

2023-09-12 03:53:06 作者:心有归属

我尝试开发这样的应用程序,在我想锁定在我的设备中的所有应用程序与任何我想要的密码感。但我没有找到任何一件code的解决方案。所以,我开发了我自己,不幸的是它并没有succeed.I发现了许多解决方案,锁定的Andr​​oid设备,但没有找到一个锁定的应用程序。如果你提出一个解决方案,会很高兴。谢谢

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. Thank You

推荐答案

我使用的背景服务来检查哪个应用是在前台,这意味着应用程序正在由用户使用。然后我检查我是否需要锁定的应用程序或没有。

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 whether I need to lock the application or not.

要查找所有安装的应用程序的列表,但不包括系统应用:

To find the list of all installed application, excluding system application:

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;
    }
    apps.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 you use the package name to identify any application so we know that the package name is always unique.

现在功能锁定应用程序

当我们发现它的应用程序运行在前台,并希望将其锁定,我们只需要启动它有一个EditText密码,并确定其他活动和取消按钮。

When we 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活动。如果密码不正确,简单地使用下面提及code的关闭应用程序,并显示设备的主屏幕:

On click of Ok if the password is correct then simply finish the LockScreen activity. If the password is incorrect then simply use the below mention code 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);

同样的code也可以取消按钮。

The same code is also used on the cancel button.