tha*_*eph 32 android password-protection
我试图开发这样一个应用程序,在某种意义上我想用我想要的密码锁定我的设备中的所有应用程序.但我没有找到解决方案的任何代码.所以我自己开发了一个,不幸的是它没有成功.我找到了许多锁定Android设备的解决方案,但没有找到一个锁定应用程序.如果你建议一个解决方案,将很高兴.
Ami*_*pta 52
我使用后台服务来检查哪个应用程序在前台(这意味着用户正在使用该应用程序).然后我检查是否需要锁定应用程序.
要查找所有已安装应用程序的列表(不包括系统应用程序):
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);
}
Run Code Online (Sandbox Code Playgroud)
要查找当前的前台应用程序:
ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> RunningTask = mActivityManager.getRunningTasks(1);
ActivityManager.RunningTaskInfo ar = RunningTask.get(0);
activityOnTop=ar.topActivity.getClassName();
Run Code Online (Sandbox Code Playgroud)
这里的class-name提供了应用程序的包名.我建议您使用包名来标识任何应用程序,以便我们知道包名称始终是唯一的.
现在,锁定应用程序的功能:
要查找哪个应用程序在前台运行并想要锁定它,我们只需要启动另一个具有EditText for password和OK和Cancel按钮的活动.
Intent lockIntent = new Intent(mContext, LockScreen.class);
lockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(lockIntent);
Run Code Online (Sandbox Code Playgroud)
单击"确定",如果密码正确,则只需完成 LockScreen活动.如果密码不正确,则只需使用下面的代码即可关闭应用程序并显示设备的主屏幕:
Intent startHomescreen = new Intent(Intent.ACTION_MAIN);
startHomescreen.addCategory(Intent.CATEGORY_HOME);
startHomescreen.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(startHomescreen);
Run Code Online (Sandbox Code Playgroud)
取消按钮也使用相同的代码.
7ge*_*eky 10
看起来这仍然是上面提到的评论所暗示的一个谜.所以我把代码帮助我解决了这个问题.
getForegroundApp
public String getForegroundApp() {
String currentApp = "NULL";
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
UsageStatsManager usm = (UsageStatsManager) this.mContext.getSystemService(Context.USAGE_STATS_SERVICE);
long time = System.currentTimeMillis();
List<UsageStats> appList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000 * 1000, time);
if (appList != null && appList.size() > 0) {
SortedMap<Long, UsageStats> mySortedMap = new TreeMap<Long, UsageStats>();
for (UsageStats usageStats : appList) {
mySortedMap.put(usageStats.getLastTimeUsed(), usageStats);
}
if (mySortedMap != null && !mySortedMap.isEmpty()) {
currentApp = mySortedMap.get(mySortedMap.lastKey()).getPackageName();
}
}
} else {
ActivityManager am = (ActivityManager) this.mContext.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> tasks = am.getRunningAppProcesses();
currentApp = tasks.get(0).processName;
}
return currentApp;
}
Run Code Online (Sandbox Code Playgroud)
调用getForegroundApp()它将返回一个字符串,其中包含currentForegroundApp的名称,包括包名称,例如com.example.app
现在,要使用此代码,我们需要在Manifest文件中使用这行代码
<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS" />
Run Code Online (Sandbox Code Playgroud)
并将用户带到Usage Data access Settings:
usageAccessSettingsPage
public void usageAccessSettingsPage(){
Intent intent = new Intent();
intent.setAction(Settings.ACTION_USAGE_ACCESS_SETTINGS);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromParts("package", mContext.getPackageName(), null);
intent.setData(uri);
startActivity(intent);
}
Run Code Online (Sandbox Code Playgroud)
或手动通过找到LockScreen and Security> Other security settings> Usage access data.
现在是阻止应用程序的部分,这部分在Amit的答案中得到了很好的解决.但是,如果有人正在寻找限制用户使用应用程序的方法,那么当特定应用程序启动时,诀窍就是打开主屏幕.
这可以通过在currentApp等于被阻止的应用程序时调用以下方法来完成
if(<NameOfBlockedApp>.equals currentApp){
showHomeScreen();
}
Run Code Online (Sandbox Code Playgroud)
ShowHomeScreen
public boolean showHomeScreen(){
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(startMain);
return true;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
29328 次 |
| 最近记录: |