android 起动APP时锁住(Lock apps)

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); } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值