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

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

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

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

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

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

2可以设置密码

3可以关闭程序锁

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

我们先说最关键的部分

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

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

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

mActivityManager = (ActivityManager) context.getSystemService("activity");
//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

ComponentName topActivity = mActivityManager.getRunningTasks().get().topActivity;

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

ComponentName topActivity = mActivityManager.getRunningTasks().get().topActivity;
String packageName = topActivity.getPackageName();
String className = topActivity.getClassName();
Log.v(TAG, "packageName" + packageName);
Log.v(TAG, "className" + className); if (testPackageName.equals(packageName)
&& testClassName.equals(className)) {
Intent intent = new Intent();
intent.setClassName("com.example.locktest", "com.example.locktest.PasswordActivity");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
}

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

String testPackageName = "com.htc.notes";
String testClassName = "com.htc.notes.collection.NotesGridViewActivity";

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

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

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

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

private Timer mTimer;
private void startTimer() {
if (mTimer == null) {
mTimer = new Timer();
LockTask lockTask = new LockTask(this);
mTimer.schedule(lockTask, 0L, 1000L);
}
}

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

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

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

public class LockTask extends TimerTask {
public static final String TAG = "LockTask";
private Context mContext;
String testPackageName = "com.htc.notes";
String testClassName = "com.htc.notes.collection.NotesGridViewActivity"; private ActivityManager mActivityManager; public LockTask(Context context) {
mContext = context;
mActivityManager = (ActivityManager) context.getSystemService("activity");
} @Override
public void run() {
ComponentName topActivity = mActivityManager.getRunningTasks().get().topActivity;
String packageName = topActivity.getPackageName();
String className = topActivity.getClassName();
Log.v(TAG, "packageName" + packageName);
Log.v(TAG, "className" + className); if (testPackageName.equals(packageName)
&& testClassName.equals(className)) {
Intent intent = new Intent();
intent.setClassName("com.example.locktest", "com.example.locktest.PasswordActivity");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
}
}
}

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

public class LockService extends Service {
private Timer mTimer;
public static final int FOREGROUND_ID = ; private void startTimer() {
if (mTimer == null) {
mTimer = new Timer();
LockTask lockTask = new LockTask(this);
mTimer.schedule(lockTask, 0L, 1000L);
}
} public IBinder onBind(Intent intent) {
return null;
} public void onCreate() {
super.onCreate();
startForeground(FOREGROUND_ID, new Notification());
} public int onStartCommand(Intent intent, int flags, int startId) {
startTimer();
return super.onStartCommand(intent, flags, startId);
} public void onDestroy() {
stopForeground(true);
mTimer.cancel();
mTimer.purge();
mTimer = null;
super.onDestroy();
}
}

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

public class MainActivity extends Activity {

    public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
startService(new Intent(this, LockService.class));
}
}

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

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

public class PasswordActivity extends Activity {

    private static final String TAG = "PasswordActivity";
Button okButton;
EditText passwordEditText;
private boolean mFinish = false; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.password);
passwordEditText = (EditText) findViewById(R.id.password);
okButton = (Button) findViewById(R.id.ok);
okButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String password = passwordEditText.getText().toString();
Log.v(TAG, "password" + password);
mFinish = true;
finish();
}
});
} public void onBackPressed(){} public void onPause(){
super.onPause();
if(!mFinish){
finish();
}
}
}

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

<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"即可

Android安全问题 程序锁的更多相关文章

  1. Android开发——程序锁的实现(可用于开发钓鱼登录界面)

    1. 程序锁原理 1.1 实现效果: 在用户打开一个应用时,若此应用是我们业务内的逻辑拦截目标,那就在开启应用之后,弹出一个输入密码的界面,输入密码正确则进入目标应用.若不输入直接按返回键,则直接返回 ...

  2. Android项目实战_手机安全卫士程序锁

    ###1.两个页面切换的实现1. 可以使用Fragment,调用FragmentTransaction的hide和show方法2. 可以使用两个布局,设置visibility的VISIABLE和INV ...

  3. android147 360 程序锁fragment

    package com.itheima.mobileguard.fragment; import java.util.ArrayList; import java.util.List; import ...

  4. android147 360 程序锁

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  5. Android安全问题 钓鱼程序

    导读:文本介绍一种钓鱼应用,讲述如何骗取用户的用户名和密码,无须root 这个话题是继续android安全问题(二) 程序锁延伸的 之前我已经展示了如何制作程序锁.当打开指定应用的时候,弹出一个密码页 ...

  6. Android学习笔记_63_手机安全卫士知识点归纳(3)分享 程序锁 服务 进程管理 widget

    1.分享: Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND); shareIntent.setT ...

  7. android安全问题(八)伪造短信(利用原生android4.0漏洞)

    导读:本文利用android4.0的一个原生漏洞来伪造短信.无须声明任何权限即可伪造发送方为任何号码的短信给用户. android4.0发布已经是很久很久很久很久以前的事情了,这个漏洞早就报了出来,之 ...

  8. 与Android应用程序相关的文件目录都有哪些?(转载)

    与Android应用程序相关的文件目录都有哪些? | 浏览:1312 | 更新:2014-09-28 19:43 | 标签:android 一.方法介绍:   每个Android应用程序都可以通过Co ...

  9. fir.im Weekly - iOS/Android 应用程序架构解析

    假如问你一个iOS or Android app的架构,你会从哪些方面来说呢? 本期 fir.im Weekly 收集了关于  iOS/Android 开发资源,也加入了一些关于 Web 前端方面的分 ...

随机推荐

  1. 关于delete

    上面三图在debug下,delete的时候会以fe ee覆盖指针所指向要回收内存前后较大一块区域的值. 下图对于release下delete的时候会先用fe ee覆盖指针所指位置起6*4=24字节, ...

  2. vs工程链接出现error LNK2005...already defined

    今天使用vs2008编译工程无错误,链接过程,出现很多这样的错误: error LNK2005: "public: __thiscall std::basic_string<char, ...

  3. 《RedHatLinux逻辑卷的管理》——一条龙服务

    首先建2分区 [root@localhost ~]# partx -d /dev/sdb error deleting partition 4: BLKPG: No such device or ad ...

  4. jLink(v8)GDB 命令总结

    /** ****************************************************************************** * @author    Maox ...

  5. prototype原型理解

    一切都是对象,对象是若干属性的集合   数组是对象.函数是对象.对象还是对象.对象里面的一切都是属性,只有属性,没有方法.方法也是属性. 一切引用类型都是属性 怎么判断一个值是否是对象? 值类型的类型 ...

  6. Android 使用日常

    如何让Android Studio的智能感知不区分大小写? http://ask.csdn.net/questions/155844

  7. 解决ListView滑动时卡的问题,实现异步加载图片解决

    ListView是最为常见的空间之一,现在的应用的呈现形式大多数都需要用到ListView来呈现,以列表的方式最直观最便于操作. 那么在使用的过程中大家一定使用adapter适配器来匹配这个ListV ...

  8. Jquery实现图片左右自动滚动

    图片左右滚动的效果想必大家都有见到过吧,其实很简单.在本文将为大家介绍下使用Jquery是如何实现图片左右自动滚动的. 代码如下:<!DOCTYPE HTML>  <html> ...

  9. 《疯狂的android讲义第3版》读书笔记

    第一章.开始启程,你的第一行android代码 1.android系统架构: 1)linux内核层:为底层硬件提供驱动,如显示驱动.音频驱动.照相机驱动.蓝牙驱动.Wifi驱动.电源管理等 2)系统运 ...

  10. EXTJS 4.2 资料 控件之Grid Columns 列renderer 绑定事件

    columns: [ { header: '序号', xtype: 'rownumberer', align: 'center', width: 100 }, { header: 'CompanyId ...