美文网首页
android 起动APP时锁住(Lock apps)

android 起动APP时锁住(Lock apps)

作者: 游侠_6fb7 | 来源:发表于2020-04-14 13:14 被阅读0次

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

    相关文章

      网友评论

          本文标题:android 起动APP时锁住(Lock apps)

          本文链接:https://www.haomeiwen.com/subject/crwzmhtx.html