zoukankan      html  css  js  c++  java
  • android安全问题(二) 程序锁

    导读:本文介绍如何实现对应用加锁的功能,无须root权限

    某些人有时候会有这样一种需求,小A下载了个软件,只是软件中的美女过于诱惑与暴露,所以他不想让别人知道这是个什么软件,起码不想让别人打开浏览。而这款软件又没有锁,任何人都可以打开,肿么办呢?如果打开它的时候需要输入密码,那该多好阿!于是,程序锁这种应用就产生了

    程序锁不是最近才有的,很久之前android就有这种apk了

    这一期我们来苛刻如何实现程序加锁功能

    首先,我们先明确一下我们要做的程序具有什么功能

    1可以选择需要加锁的程序

    2可以设置密码

    3可以关闭程序锁

    这里作为演示,我们就尽量简化代码

    我们先说最关键的部分

    最关键的地方在于:当用户打开一个应用的时候,怎么弹出密码页面?

    这里没有什么太好的办法,需要扫描task中的topActivity

    首先,我们先获得运行的task

    Java代码 复制代码 收藏代码
    1. mActivityManager = (ActivityManager) context.getSystemService("activity");   
    2. //mActivityManager.getRunningTasks(1);//List<RunningTaskInfo>  

    getRunningTasks方法返回一个List,我们来看看这个List是什么

    getRunningTasks 写道
    Return a list of the tasks that are currently running, with the most recent being first and older ones after in order.
    …… 

    返回的List是有序的,第一个是最近的,所以我们取出第一个即可,然后得到此task中的最上层的Activity

    Java代码 复制代码 收藏代码
    1. ComponentName topActivity = mActivityManager.getRunningTasks(1).get(0).topActivity;  

    topActivity居然是ComponentName类型,下面的事情就好办了,获得包名和类名

    Java代码 复制代码 收藏代码
    1. ComponentName topActivity = mActivityManager.getRunningTasks(1).get(0).topActivity;   
    2. String packageName = topActivity.getPackageName();   
    3. String className = topActivity.getClassName();   
    4. Log.v(TAG, "packageName" + packageName);   
    5. Log.v(TAG, "className" + className);   
    6.   
    7. if (testPackageName.equals(packageName)   
    8.         && testClassName.equals(className)) {   
    9.     Intent intent = new Intent();   
    10.     intent.setClassName("com.example.locktest""com.example.locktest.PasswordActivity");   
    11.     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);   
    12.     mContext.startActivity(intent);   
    13. }  

    由于我没有选择程序这一步,所以我就固定一个应用做测试,这里选择的是htc的note应用

    Java代码 复制代码 收藏代码
    1. String testPackageName = "com.htc.notes";   
    2. String testClassName = "com.htc.notes.collection.NotesGridViewActivity";  

    下面我们该想,这段代码何时执行了

    打开一个应用程序,系统不会发送广播,我们无法直接监听,所以这里我们采取定时扫描的策略

    这里只是一个简单的实现,之后我们再讨论优化

    我们采取每秒中检查一次task的方式,这里使用Timer吧,用Handler也一样可以实现

    Java代码 复制代码 收藏代码
    1. private Timer mTimer;   
    2. private void startTimer() {   
    3.     if (mTimer == null) {   
    4.         mTimer = new Timer();   
    5.         LockTask lockTask = new LockTask(this);   
    6.         mTimer.schedule(lockTask, 0L, 1000L);   
    7.     }   
    8. }  

    到这里,其实我们的关键代码就已经完成了

    下面贴出完整带代码,注意:我们只关注弹出锁界面这部分,其他部分自行实现(比如文章末尾提到的)

    Task,负责检查task,并在适当的时候弹出密码页面

    Java代码 复制代码 收藏代码
    1. public class LockTask extends TimerTask {   
    2.     public static final String TAG = "LockTask";   
    3.     private Context mContext;   
    4.     String testPackageName = "com.htc.notes";   
    5.     String testClassName = "com.htc.notes.collection.NotesGridViewActivity";   
    6.   
    7.     private ActivityManager mActivityManager;   
    8.   
    9.     public LockTask(Context context) {   
    10.         mContext = context;   
    11.         mActivityManager = (ActivityManager) context.getSystemService("activity");   
    12.     }   
    13.   
    14.     @Override  
    15.     public void run() {   
    16.         ComponentName topActivity = mActivityManager.getRunningTasks(1).get(0).topActivity;   
    17.         String packageName = topActivity.getPackageName();   
    18.         String className = topActivity.getClassName();   
    19.         Log.v(TAG, "packageName" + packageName);   
    20.         Log.v(TAG, "className" + className);   
    21.   
    22.         if (testPackageName.equals(packageName)   
    23.                 && testClassName.equals(className)) {   
    24.             Intent intent = new Intent();   
    25.             intent.setClassName("com.example.locktest""com.example.locktest.PasswordActivity");   
    26.             intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);   
    27.             mContext.startActivity(intent);   
    28.         }   
    29.     }   
    30. }  

    LockService,负责执行定时任务,取消任务等

    Java代码 复制代码 收藏代码
    1. public class LockService extends Service {   
    2.     private Timer mTimer;   
    3.     public static final int FOREGROUND_ID = 0;   
    4.   
    5.     private void startTimer() {   
    6.         if (mTimer == null) {   
    7.             mTimer = new Timer();   
    8.             LockTask lockTask = new LockTask(this);   
    9.             mTimer.schedule(lockTask, 0L, 1000L);   
    10.         }   
    11.     }   
    12.   
    13.     public IBinder onBind(Intent intent) {   
    14.         return null;   
    15.     }   
    16.   
    17.     public void onCreate() {   
    18.         super.onCreate();   
    19.         startForeground(FOREGROUND_ID, new Notification());   
    20.     }   
    21.   
    22.     public int onStartCommand(Intent intent, int flags, int startId) {   
    23.         startTimer();   
    24.         return super.onStartCommand(intent, flags, startId);   
    25.     }   
    26.   
    27.     public void onDestroy() {   
    28.         stopForeground(true);   
    29.         mTimer.cancel();   
    30.         mTimer.purge();   
    31.         mTimer = null;   
    32.         super.onDestroy();   
    33.     }   
    34. }  

    MainActivity,测试用,作为应用入口,启动service(产品中,我们可以在receiver中启动service)。

    Java代码 复制代码 收藏代码
    1. public class MainActivity extends Activity {   
    2.   
    3.     public void onCreate(Bundle savedInstanceState){   
    4.         super.onCreate(savedInstanceState);   
    5.         startService(new Intent(this, LockService.class));   
    6.     }   
    7. }  

    PasswordActivity,密码页面,很粗糙,没有核对密码逻辑,自行实现

    记得重写onBackPressed函数,不然按返回键的时候……你懂的

    Java代码 复制代码 收藏代码
    1. public class PasswordActivity extends Activity {   
    2.   
    3.     private static final String TAG = "PasswordActivity";   
    4.     Button okButton;   
    5.     EditText passwordEditText;   
    6.     private boolean mFinish = false;   
    7.        
    8.     @Override  
    9.     protected void onCreate(Bundle savedInstanceState) {   
    10.         super.onCreate(savedInstanceState);   
    11.         setContentView(R.layout.password);   
    12.         passwordEditText = (EditText) findViewById(R.id.password);   
    13.          okButton = (Button) findViewById(R.id.ok);   
    14.          okButton.setOnClickListener(new View.OnClickListener() {   
    15.             public void onClick(View v) {   
    16.                 String password = passwordEditText.getText().toString();   
    17.                 Log.v(TAG, "password" + password);   
    18.                 mFinish = true;   
    19.                 finish();   
    20.             }   
    21.         });   
    22.     }   
    23.   
    24.     public void onBackPressed(){}   
    25.        
    26.     public void onPause(){   
    27.         super.onPause();   
    28.         if(!mFinish){   
    29.             finish();   
    30.         }   
    31.     }   
    32. }  

    xml这里就不贴了,记得添加权限

    Xml代码 复制代码 收藏代码
    1. <uses-permission android:name="android.permission.GET_TASKS"/>  

    关于程序的其他部分,这里只做简要说明

    选择应用对其进行加锁部分

    1列出系统中所有程序(你也可以自由发挥,比如过滤掉原始应用)

    2选择,然后存入数据库(当然,最好也有取消功能,记得从数据库中删除数据)

    程序锁总开关

    可以使用sharedPreference,设置一个boolean开关

    现在,当我想要打开htc的note应用的时候,就会弹出密码页面当我解锁,按home会回到桌面,长按home,点击note,还是会弹出密码框

    因为是每秒检查一次,所以可能会有一点点延迟,你可以设置为500毫秒,但是越频繁,占用资源就越多

    上面的代码我取得topActivity后检查了其包名行和类名,所以只有当打开指定的页面的时候,才会弹出密码锁

    比如我对Gallery应用加密了,但是用户正在编辑短信,这时候它想发彩信,于是他通过短信进入到了Gallery……

    对于某些用户的某些需求来说,这是不能容忍的,这时,我们只需简单修改下判断逻辑即可:只检查包名,包名一致就弹出密码锁,这样就完美了

    程序锁我就分析到这里

    最后一句

    当使用程序锁的时候,你长按home,发现程序锁也出现在“最近的任务”中,肿么办……给此activity设置android:excludeFromRecents="true"即可

     

    请大家不要用root的手机随意下载软件,更不要以任何借口制造任何病毒!

    转贴

    http://su1216.iteye.com/

    http://blog.csdn.net/su1216/

  • 相关阅读:
    用户在第二层,而你在第五层,深度玩转华为预测服务
    Docker启动PostgreSQL时创建多个数据库
    CORS跨源资源共享概念及配置(Kubernetes Ingress和Spring Cloud Gateway)
    HTTP Cookies知识-查看、发送、获取、返回
    Kubernetes ConfigMap详解,多种方式创建、多种方式使用
    示例讲解PostgreSQL表分区的三种方式
    Springboot WebFlux集成Spring Security实现JWT认证
    Bean初始化操作initMethod、@PostConstruct和InitializingBean
    Springboot集成Spring Security实现JWT认证
    Spring Data JPA的Audit功能,审计数据库的变更
  • 原文地址:https://www.cnblogs.com/jackrex/p/3001409.html
Copyright © 2011-2022 走看看