导读:本文介绍如何实现对应用加锁的功能,无须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. Powerful Sleep(神奇的睡眠-睡眠生物钟的秘密:如何睡得更少却睡得更好)阅读笔记

    睡眠机制 我们活着的时候,大脑会产生脑电波.脑电图仪器通过贴在人头上的一些电极读出脑电波的活动,然后把活动用图表显示出来. 睡眠过程可以分为5个过程,划分依据与大脑发出的脑电波类型. 当人清醒时,大脑 ...

  2. Traveller项目介绍

    Traveller,翻译为旅行家,是我用来实践最佳web技术的项目,主题是一个给旅行爱好者提供旅行信息的网站. 目标是组合现最流行的web技术,实现符合中国用户使用习惯的网站. 相关网址 Git:ht ...

  3. 基于css3新属性transform及原生js实现鼠标拖动3d立方体旋转

    基于css3新属性transform,实现3d立方体的旋转 通过原生JS,点击事件,鼠标按下.鼠标抬起和鼠标移动事件,实现3d立方体的拖动旋转,并将旋转角度实时的反应至界面上显示 实现原理:通过获取鼠 ...

  4. asp:HyperLink vs asp:LinkButton

    asp:HyperLink NavigateUrl - 页面刷新为新的页面 pageload ispostback = false asp:LinkButton PostbackUrl - postb ...

  5. mcrypt加密与解密

    $key = 'test'; $result_array = array('name' => 'ta', 'age' => 28); $str = encode($result_array ...

  6. Django1.6添加comments应用的简单过程

    今天尝试为自己开发的博客加上评论功能,查看Django的文档,发现1.6之后Django不再自带comments这个app了,下面是官方文档上的说明: Django’s comment framewo ...

  7. Flashback删除(闪回删除)

    oracle 9i以前,当drop一个表时,到该表的所有引用都会从数据字典中删除. oracle 10g及以后,当drop一个表时,数据库根本没有删除表,而只是重命名了并放入了回收站.即当发出drop ...

  8. linux 常用命令 集锦

    第一章  LINUX简介及安装    1一.LINUX介绍    1二.LINUX安装    2三.LINUX目录    2四.总结来说:    3第二章 常用命令及帐户管理    4一.linux命 ...

  9. POJ1734 - Sightseeing trip

    DescriptionThere is a travel agency in Adelton town on Zanzibar island. It has decided to offer its ...

  10. 10+ 最流行的 jQuery Tree 菜单插件

    jstree – jQuery Tree Plugin With HTML & JSON Data jstree is a lightweight and flexible jQuery pl ...