android安全问题(二) 程序锁

导读:本文介绍如何实现对应用加锁的功能,无须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(1).get(0).topActivity;

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

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应用

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(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,负责执行定时任务,取消任务等

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

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



  • 6
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 14
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值