android安全问题(二) 程序锁

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

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

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

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

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

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

2可以设置密码

3可以关闭程序锁

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

我们先说最关键的部分

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

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

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

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

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

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

Java代码 复制代码 收藏代码
  1. ComponentNametopActivity=mActivityManager.getRunningTasks(1).get(0).topActivity;
  2. StringpackageName=topActivity.getPackageName();
  3. StringclassName=topActivity.getClassName();
  4. Log.v(TAG,"packageName"+packageName);
  5. Log.v(TAG,"className"+className);
  6. if(testPackageName.equals(packageName)
  7. &&testClassName.equals(className)){
  8. Intentintent=newIntent();
  9. intent.setClassName("com.example.locktest","com.example.locktest.PasswordActivity");
  10. intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  11. mContext.startActivity(intent);
  12. }
ComponentName topActivity = mActivityManager.getRunningTasks(1).get(0).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应用

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

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

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

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

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

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

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

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

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

Java代码 复制代码 收藏代码
  1. publicclassLockTaskextendsTimerTask{
  2. publicstaticfinalStringTAG="LockTask";
  3. privateContextmContext;
  4. StringtestPackageName="com.htc.notes";
  5. StringtestClassName="com.htc.notes.collection.NotesGridViewActivity";
  6. privateActivityManagermActivityManager;
  7. publicLockTask(Contextcontext){
  8. mContext=context;
  9. mActivityManager=(ActivityManager)context.getSystemService("activity");
  10. }
  11. @Override
  12. publicvoidrun(){
  13. ComponentNametopActivity=mActivityManager.getRunningTasks(1).get(0).topActivity;
  14. StringpackageName=topActivity.getPackageName();
  15. StringclassName=topActivity.getClassName();
  16. Log.v(TAG,"packageName"+packageName);
  17. Log.v(TAG,"className"+className);
  18. if(testPackageName.equals(packageName)
  19. &&testClassName.equals(className)){
  20. Intentintent=newIntent();
  21. intent.setClassName("com.example.locktest","com.example.locktest.PasswordActivity");
  22. intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  23. mContext.startActivity(intent);
  24. }
  25. }
  26. }
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(1).get(0).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,负责执行定时任务,取消任务等

Java代码 复制代码 收藏代码
  1. publicclassLockServiceextendsService{
  2. privateTimermTimer;
  3. publicstaticfinalintFOREGROUND_ID=0;
  4. privatevoidstartTimer(){
  5. if(mTimer==null){
  6. mTimer=newTimer();
  7. LockTasklockTask=newLockTask(this);
  8. mTimer.schedule(lockTask,0L,1000L);
  9. }
  10. }
  11. publicIBinderonBind(Intentintent){
  12. returnnull;
  13. }
  14. publicvoidonCreate(){
  15. super.onCreate();
  16. startForeground(FOREGROUND_ID,newNotification());
  17. }
  18. publicintonStartCommand(Intentintent,intflags,intstartId){
  19. startTimer();
  20. returnsuper.onStartCommand(intent,flags,startId);
  21. }
  22. publicvoidonDestroy(){
  23. stopForeground(true);
  24. mTimer.cancel();
  25. mTimer.purge();
  26. mTimer=null;
  27. super.onDestroy();
  28. }
  29. }
public class LockService extends Service {
	private Timer mTimer;
	public static final int FOREGROUND_ID = 0;

	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)。

Java代码 复制代码 收藏代码
  1. publicclassMainActivityextendsActivity{
  2. publicvoidonCreate(BundlesavedInstanceState){
  3. super.onCreate(savedInstanceState);
  4. startService(newIntent(this,LockService.class));
  5. }
  6. }
public class MainActivity extends Activity {

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

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

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

Java代码 复制代码 收藏代码
  1. publicclassPasswordActivityextendsActivity{
  2. privatestaticfinalStringTAG="PasswordActivity";
  3. ButtonokButton;
  4. EditTextpasswordEditText;
  5. privatebooleanmFinish=false;
  6. @Override
  7. protectedvoidonCreate(BundlesavedInstanceState){
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.password);
  10. passwordEditText=(EditText)findViewById(R.id.password);
  11. okButton=(Button)findViewById(R.id.ok);
  12. okButton.setOnClickListener(newView.OnClickListener(){
  13. publicvoidonClick(Viewv){
  14. Stringpassword=passwordEditText.getText().toString();
  15. Log.v(TAG,"password"+password);
  16. mFinish=true;
  17. finish();
  18. }
  19. });
  20. }
  21. publicvoidonBackPressed(){}
  22. publicvoidonPause(){
  23. super.onPause();
  24. if(!mFinish){
  25. finish();
  26. }
  27. }
  28. }
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这里就不贴了,记得添加权限

Xml代码 复制代码 收藏代码
  1. <uses-permissionandroid:name="android.permission.GET_TASKS"/>
<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.youkuaiyun.com/su1216/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值