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