android 起动APP时锁住(Lock apps)
2019獨角獸企業重金招聘Python工程師標準>>>
android手機管家鎖住其它APP,發現有點流氓,后臺一個線程在不停的運行 大致的核心代碼如下:起時在service中啟動這個類,但做測試為圖個方便直接在Activity中啟動 package com.example.testsafe;import android.content.Context; import android.content.pm.ApplicationInfo; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException;public class ApkInfo extends ApplicationInfo{private String appname;private String pname;private String versionName;private int versionCode;private PackageInfo packageInfo;public static ApkInfo getInfoFromPackageName(String pkgName,Context mContext) {ApkInfo newInfo = new ApkInfo();try {PackageInfo p = mContext.getPackageManager().getPackageInfo(pkgName, PackageManager.GET_PERMISSIONS);newInfo.appname = p.applicationInfo.loadLabel(mContext.getPackageManager()).toString();newInfo.pname = p.packageName;newInfo.versionName = p.versionName;newInfo.versionCode = p.versionCode;newInfo.setPackageInfo(p);} catch (NameNotFoundException e) {e.printStackTrace();return null;}return newInfo;}public String getAppname() {return appname;}public void setAppname(String appname) {this.appname = appname;}public String getPname() {return pname;}public void setPname(String pname) {this.pname = pname;}public String getVersionName() {return versionName;}public void setVersionName(String versionName) {this.versionName = versionName;}public int getVersionCode() {return versionCode;}public void setVersionCode(int versionCode) {this.versionCode = versionCode;}public PackageInfo getPackageInfo() {return packageInfo;}public void setPackageInfo(PackageInfo packageInfo) {this.packageInfo = packageInfo;}}
package com.example.testsafe; import java.util.List; import java.util.Timer; import java.util.TimerTask; import android.app.ActivityManager; import android.app.ActivityManager.RunningAppProcessInfo; import android.content.Context; import android.content.Intent; import android.content.pm.ApplicationInfo; public class Safe { String lastFrontAppPkg = null; public void test(final Context mContext){ Timer timer = new Timer(); timer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { ? ActivityManager activityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE); ? ? ? ? ? List<RunningAppProcessInfo> appProcesses= activityManager.getRunningAppProcesses(); ? ? ? ?for (RunningAppProcessInfo appProcess : appProcesses) { ? ? ? ? ? ?try { ? ? ? ? ? ?if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { ? ? ? ? ? ?if (!lastFrontAppPkg.equals((String) appProcess.pkgList[0])) { ? ? ? ? ? ? ApkInfo apkInfo = ApkInfo.getInfoFromPackageName(appProcess.pkgList[0], mContext); ? ? ? ? ? ? ? ?if (apkInfo == null || (apkInfo.getPackageInfo().applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 1) { ? ? ? ? ? ? ? ? ?// System app ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? continue; ? ? ? ? ? ? ? ? ? ? ? ?} else if (((apkInfo.getPackageInfo().versionName == null)) || (apkInfo.getPackageInfo().requestedPermissions == null)) { ? ? ? ? ? ? ? ? ? ?//Application that comes preloaded with the device ? ? ? ? ? ? ? ? ? ? ? ?continue; ? ? ? ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ? ? lastFrontAppPkg = (String) appProcess.pkgList[0]; ? ? ? ? ? ? ? ? ? ? } //kill the app //Here do the pupop with password to launch the lastFrontAppPkg if the pass is correct if(lastFrontAppPkg.equals(mContext.getPackageName())){ Intent intent = new Intent(mContext, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); mContext.startActivity(intent); } ? ? ? ? ? ?} ?} ? ? ? ? ? ? ? ? } catch (Exception e) { ? ? ? ? ? ? ? ? ?//e.printStackTrace(); ? ? ? ? ? ? ? ?} ? ? ? ?} ? } }, 0, 100); } }
轉載于:https://my.oschina.net/u/1013713/blog/133795
總結
以上是生活随笔為你收集整理的android 起动APP时锁住(Lock apps)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何编写 Nagios 插件
- 下一篇: 王超:奇虎360MongoDB