6.8 程序锁功能的实现

 
1.在看门狗中检查正在运行的应用的包名是否在已添加锁的集合中,true:携带该应用包名 跳转到输入密码的activity, 
2 .在输入密码的activity中getIntent得到看门狗发过来的应用包名
3. 创建packageManager 得到该包名对应的app的信息,  将这些信息附加给关心的控件
4.判断密码是否输入正确, true: finish当前activity       
5.在输入密码的activity中,屏蔽返回键,  不然一按返回键就会短暂的回到 想要进入的app的主界面
6.如果密码输入正确, 发送条自定义广播, 携带当前用户已输入密码的app的包名
7.在看门狗的服务中, 代码注册自定义广播事件, 得到自定义广播中携带的包名,并将包名添加到  暂时不需要受保护的app集合中
8.在看门狗功能中,判断正在运行的服务是否在已加锁应用和展示不受保护的app集合中  
9.代码注册锁屏事件, 当屏幕锁屏时,清空暂时不需要受保护的app集合


-------------------------------------------------------------------------------------------------------------------------------------
 
 
public   class   WatchDogActivity   extends   Activity {
      
      
       private   ImageView   iv_icon ;
       private   TextView    tv_name ;
       private   EditText    et_password ;
      String   packageName ;
      
          @Override
       protected   void   onCreate(Bundle savedInstanceState) {
               super .onCreate(savedInstanceState);
            setContentView(R.layout.   activity_watchdog );
            
               iv_icon =(ImageView) findViewById(R.id.   iv_icon );
               tv_name =(TextView) findViewById(R.id.   tv_name );
               et_password =(EditText) findViewById(R.id.   et_password );
            
               //得到服发来的包名
            Intent intent = getIntent();
            packageName   = intent.getStringExtra(   "packageName" );
            
               //得到包管理者
            PackageManager pm = getPackageManager();
            
               //得到app 的信息
               try   {
                  ApplicationInfo applicationInfo = pm.getApplicationInfo( packageName , 0);
                  String appName = applicationInfo.loadLabel(pm).toString();
                  Drawable appIcon = applicationInfo.loadIcon(pm);
                  
                     //给关心的控件赋值
                     iv_icon .setImageDrawable(appIcon);
                     tv_name .setText(appName);
                  
                  
            }   catch   (NameNotFoundException e) {
            
                  e.printStackTrace();
            }
            
      }
       
       
       
            /**
           * 屏蔽返回键
           */
          @Override
               public   boolean   onKeyDown(   int   keyCode, KeyEvent event) {
                     if   (keyCode == KeyEvent.   KEYCODE_BACK ) {   // 后退键盘被点击了
                           // 回桌面的操作
                           // <action android:name="android.intent.action.MAIN" />
                           // <category android:name="android.intent.category.HOME" />
                           // <category android:name="android.intent.category.DEFAULT" />
                           // <category android:name="android.intent.category.MONKEY"/>
                        Intent intent =   new   Intent();
                        intent.setAction(   "android.intent.action.MAIN" );
                        intent.addCategory(   "android.intent.category.HOME" );
                        intent.addCategory(   "android.intent.category.DEFAULT" );
                        intent.addCategory(   "android.intent.category.MONKEY" );
                        startActivity(intent);
                        finish();
                           return   true ;
                  }
                     return   super .onKeyDown(keyCode, event);
            }
       
       
       
       
       
       
          /**
         * 确定按钮的点击事件
         *   @param   view
         */
          public   void   login(View view){
             
              String pwd=   et_password .getText().toString().trim();
                if (   "123" .equals(pwd)){
                    finish();    //关闭当前activity, 进入加锁的程序页面
                   
                      //发送自定义广播给看门狗, 当前activity不要保护了
                    Intent   intent_pass=   new   Intent();
                    intent_pass.setAction( "this.activity.not.protected"   );
                    intent_pass.putExtra(   "pkgname" ,   packageName );
                    sendBroadcast(intent_pass);
                   
                   
              }   else {
                    Toast. makeText( this ,   "密码输入不正确" , 0).show();
                   
              }
             
             
        }
       
       

}



----------------------------------------------------------------------------------------------------------------------------------------



public   class   WatchDogService   extends   Service {

       protected   static   final   String   TAG   =   "WatchDogService" ;
       private   ActivityManager    am ;
       private   boolean   flags ;
       private   AppLockDao   dao ;
       private   pauseProtectBroad   ppbreceiver ;
       private   ScreenLockReceiver    lockReceiver ;
       private   List<String>   tempStopProtectPacknames ;
      
      
       @Override
       public   IBinder onBind(Intent intent) {
            
               return   null ;
      }
      
      
       /**
       * 注册自定义广播事件, 拿到watchdogActivity返回过来的包名,并告诉看门狗该应用暂时不用保护了
       */
       public   class   pauseProtectBroad    extends   BroadcastReceiver{

               @Override
               public   void   onReceive(Context context, Intent intent) {
            
                  String pkgname = intent.getStringExtra(   "pkgname" );     //得到暂停保护的包名
                     tempStopProtectPacknames .add(pkgname);    //将包名装进暂停保护的集合中
                  
                  
            }
            
      }
      
       /**
       * 注册当屏幕锁屏时的广播事件: 从暂时不受密码保护的集合中去掉该包名
       *   @author   Administrator
       *
       */
       private   class   ScreenLockReceiver   extends   BroadcastReceiver{
               @Override
               public   void   onReceive(Context context, Intent intent) {
                     //屏幕锁屏
                     tempStopProtectPacknames .clear();
            }
      }
      
      
      
      
      
       /**
       * 服务开启时
       */
       @Override
       public   void   onCreate() {
            Log. i( TAG ,   "开启服务"   );
               tempStopProtectPacknames =   new   ArrayList<String>();
            
               //当服务开启时开启初始化pass集合的广播
               ppbreceiver   =   new   pauseProtectBroad();
            IntentFilter filter=   new   IntentFilter();
            filter.addAction(   "this.activity.not.protected" );
         registerReceiver(   ppbreceiver , filter);
        
           //当服务开启时开启清空pass集合的广播
           lockReceiver   =   new   ScreenLockReceiver();
            IntentFilter lockFilter =   new   IntentFilter();
            lockFilter.addAction(Intent.   ACTION_SCREEN_OFF );
            registerReceiver(   lockReceiver , lockFilter);
            
            
            
               dao =   new   AppLockDao(getApplicationContext()); 
            
               //1.创建activity管理员
               am =(ActivityManager) getSystemService(   ACTIVITY_SERVICE );
            
               //2.看门狗在服务开启时不停的监视着后台正在运行的程序
               new   Thread(){
                     public   void   run() {
                           flags =   true ;
                        
                           while (   flags ){       //获取正在运行的任务栈     3:可以任意定义,表示近期开启的3个任务栈
                        List<RunningTaskInfo> runningTasks =   am .getRunningTasks(3);
                        
                           //最上层的任务栈:   刚打开的应用程序的任务栈
                        RunningTaskInfo runningTaskInfo = runningTasks.get(0);
                        
                           //得到刚刚开启的应用程序的activity的名称
                        String packageName = runningTaskInfo. topActivity .getPackageName();
                        
                           //判断得到的包名的应用是否在已加锁的数据库中
                           if (   dao .find(packageName)){
                              
                                 if (!   tempStopProtectPacknames .contains(packageName)){     //如果暂停保护集合中存在该包名
                                    
                                    
                                    Log. i( TAG ,   "弹出提示输入密码的activity"   );
                                    Intent intent=   new   Intent(getApplicationContext(),WatchDogActivity. class   );
                                    intent.setFlags(Intent.   FLAG_ACTIVITY_NEW_TASK );   //开启新任务栈
                                    
                                    intent.putExtra(   "packageName" , packageName);
                                    startActivity(intent);
                                    
                                    }

                                    try   {                   
                                    Thread. sleep(50);                 
                              }   catch   (InterruptedException e) {
                                    
                                    e.printStackTrace();
                              }
                              
                        }
                        
                        
                        
                        }
                  };
                  
      }.start();
            
            
            
            
            
               super .onCreate();
      }
      
      
       /**
       * 服务销毁时
       */
       public   void   onDestroy() {
            Log. i( TAG ,   "关闭服务..."   );
               flags =   false   //服务关闭时关闭关门狗
            
            //服务关闭时终止自定义广播
            ppbreceiver .abortBroadcast();
            
            lockReceiver .abortBroadcast();
            
      };
      

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值