• 程序锁的核心原理(获取正在显示的任务栈)


      1 package com.android.hzy.mobilesafe.service;
      2 
      3 import java.util.ArrayList;
      4 import java.util.List;
      5 
      6 import android.app.ActivityManager;
      7 import android.app.ActivityManager.RunningTaskInfo;
      8 import android.app.KeyguardManager;
      9 import android.app.Service;
     10 import android.content.BroadcastReceiver;
     11 import android.content.ComponentName;
     12 import android.content.Context;
     13 import android.content.Intent;
     14 import android.content.IntentFilter;
     15 import android.database.ContentObserver;
     16 import android.net.Uri;
     17 import android.os.Binder;
     18 import android.os.Handler;
     19 import android.os.IBinder;
     20 import android.os.SystemClock;
     21 
     22 import com.android.hzy.mobilesafe.MobileSafeApplication;
     23 import com.android.hzy.mobilesafe.activity.EnterPasswordActivity;
     24 import com.android.hzy.mobilesafe.dao.AppLockDao;
     25 import com.android.hzy.mobilesafe.inter.IService;
     26 import com.android.hzy.mobilesafe.utils.Logger;
     27 
     28 public class AppLockService extends Service {
     29 
     30     private ActivityManager am;
     31     private AppLockDao appLockDao;
     32     private List<String> appLocks;
     33     private List<String> tempAppLocks; // 放置临时解锁的应用
     34     private MyContentObserver observer;
     35     // 手机锁屏和解锁的服务
     36     private KeyguardManager keyguardManager;
     37     private LockScreenReceiver receiver; 
     38     
     39     @Override
     40     public void onCreate() {
     41         // TODO Auto-generated method stub
     42         super.onCreate();
     43         
     44         am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
     45         keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
     46         appLockDao = new AppLockDao(this);
     47         appLocks = appLockDao.findAll();
     48         tempAppLocks = new ArrayList<String>();
     49         
     50         observer = new MyContentObserver(new Handler());
     51         Uri uri = Uri.parse("content://applock/applock");
     52         // 注册一个内容观察者
     53         getContentResolver().registerContentObserver(uri, true, observer);
     54         
     55         // 注册锁屏广播接收者
     56         IntentFilter filter = new IntentFilter();
     57         filter.addAction(Intent.ACTION_SCREEN_OFF);
     58         receiver = new LockScreenReceiver();
     59         registerReceiver(receiver, filter);
     60         
     61         new Thread() {
     62             public void run() {
     63                 // 类似一个看门狗
     64                 while (true) {
     65                     // 判断是否锁屏
     66                     boolean isLockScreen = keyguardManager.inKeyguardRestrictedInputMode();
     67                     if(isLockScreen){
     68                         SystemClock.sleep(1000);
     69                         Logger.i("i", " 现在处于锁屏模式.....");
     70                     }else{
     71                         Logger.i("i", " 现在处于监听模式.....");
     72                         // 得到当前正在运行的任务栈
     73                         List<RunningTaskInfo> runningTasksInfos = am.getRunningTasks(1);
     74                         // 得到前台显示的任务栈
     75                         RunningTaskInfo runningTaskInfo = runningTasksInfos.get(0);
     76                         //获取栈顶的组件
     77                         ComponentName component = runningTaskInfo.topActivity;
     78                         String packageName = component.getPackageName();
     79                         // 判断当前的应用程序是否在程序锁里面
     80                         if(appLocks.contains(packageName)){
     81                             // 判断在不在临时集合中
     82                             if(!tempAppLocks.contains(packageName)){
     83                                 // 如果在程序锁里面 就将应用的包信息封装到application里面
     84                                 MobileSafeApplication application = (MobileSafeApplication) getApplication();
     85                                 application.setPackname(packageName);
     86                                 
     87                                 // 弹出输入密码的界面
     88                                 Intent intent = new Intent(getApplicationContext(),EnterPasswordActivity.class);
     89                                 // 由于服务是不存在任务栈中  因此需要设置FLAG
     90                                 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     91                                 startActivity(intent);
     92                             }
     93                         }
     94                     }
     95                 }
     96             };
     97         }.start();
     98     }
     99     
    100     private final class MyContentObserver extends ContentObserver{
    101 
    102         public MyContentObserver(Handler handler) {
    103             super(handler);
    104             // TODO Auto-generated constructor stub
    105         }
    106         
    107         @Override
    108         public void onChange(boolean selfChange) {
    109             // TODO Auto-generated method stub
    110             super.onChange(selfChange);
    111             // 重新查询数据
    112             appLocks = appLockDao.findAll();
    113             // 清除一下临时集合
    114             tempAppLocks.clear();
    115         }
    116         
    117     }
    118     
    119     /*******锁屏广播************/
    120     private final class LockScreenReceiver extends BroadcastReceiver{
    121 
    122         @Override
    123         public void onReceive(Context context, Intent intent) {
    124             // TODO Auto-generated method stub
    125             // 清除一下临时集合
    126             tempAppLocks.clear();
    127         }
    128         
    129     }
    130     
    131     private MyBinder ibinder = new MyBinder();
    132     
    133     @Override
    134     public IBinder onBind(Intent intent) {
    135         // TODO Auto-generated method stub
    136         return ibinder;
    137     }
    138     
    139     private final class MyBinder extends Binder implements IService{
    140 
    141         @Override
    142         public void addTemp(String packageName) {
    143             // TODO Auto-generated method stub
    144             addTempAppLock(packageName);
    145         }
    146         
    147     }
    148     
    149     /***********在临时集合中添加应用程序***************************/
    150     public void addTempAppLock(String packageName){
    151         tempAppLocks.add(packageName);
    152     }
    153     
    154     @Override
    155     public void onDestroy() {
    156         // TODO Auto-generated method stub
    157         super.onDestroy();
    158         // 取消监听
    159         getContentResolver().unregisterContentObserver(observer);
    160         // 取消锁屏广播的监听
    161         unregisterReceiver(receiver);
    162     }
    163 
    164 }
  • 相关阅读:
    python之模块和包
    python之常用模块一
    关于jQuery库的引用
    Python数据挖掘-相关性-相关分析
    Python数据挖掘-使用sklearn包
    Python数据挖掘-关键字提取
    Python数据挖掘-词云美化
    Python数据挖掘-词云
    Python数据挖掘-词频统计-实现
    Python数据挖掘-中文分词
  • 原文地址:https://www.cnblogs.com/androidez/p/2945198.html
Copyright © 2020-2023  润新知