首页 > 代码库 > 90程序锁的优化

90程序锁的优化

在程序锁中,当打开一个软件的时候,会短暂的出现软件的界面,然后再出现输入密码的界面,对此需要坐下修改:

第一个地方:

List<RunningTaskInfo> infos = am.getRunningTasks(1);
原来的是100,我们在此只需要得到一个最近运行的task就可以了。

第二个地方:

以前在查询的时候,查询的是数据库,这样比较慢,改之查询内存,修改dao方法:

	/**
	 * 查询全部包名
	 */
	public List<String> findAll(){
		List<String> protectPacknames = new ArrayList<String>();
		boolean result = false;
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db.query("applock", new String[]{"packname"}, null, null, null, null, null);
		if (cursor.moveToNext()) {
			protectPacknames.add(cursor.getString(0));
		}
		cursor.close();
		db.close();
		return protectPacknames;
	}
第三个地方:

在子线程外面定义Intent

第四个地方:

修改线程睡眠的时间为20秒。整体的代码修改后为:

package com.ustc.mobilemanager.service;

import java.util.List;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;

import com.ustc.mobilemanager.EnterPwdActivity;
import com.ustc.mobilemanager.db.dao.AppLockDao;

/**
 * 
 * 程序锁的功能后台服务,监视系统程序的运行信息
 * 
 * @author
 *
 */
public class WatchDogService extends Service {

	private static final String TAG = "WatchDogService";
	private ActivityManager am;

	private AppLockDao dao;

	private boolean flag;

	private InnerReceiver receiver;

	private String tempStopProtectPackageName;
	
	
	private ScreenOffReceiver offReceiver;
	
	private List<String> protectPacknames;
	private Intent intent;

	@Override
	public void onCreate() {
		
		offReceiver = new ScreenOffReceiver();

		registerReceiver(offReceiver,
				new IntentFilter(Intent.ACTION_SCREEN_OFF));

		receiver = new InnerReceiver();

		registerReceiver(receiver, new IntentFilter(
				"com.ustc.mobilemanager.TEMPSTOP"));

		dao = new AppLockDao(this);

		am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);

		flag = true;
		
		intent = new Intent(getApplicationContext(),
				EnterPwdActivity.class);

		// 服务是没有任务栈的,在服务中开启activity,要指定这个activity运行的任务栈
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

		new Thread(new Runnable() {

			@Override
			public void run() {
				while (flag) {
					@SuppressWarnings("deprecation")
					List<RunningTaskInfo> infos = am.getRunningTasks(1);

					String packageName = infos.get(0).topActivity
							.getPackageName();

//					 Log.i(TAG, "WatchDogService当前用户操作的包名:-->packageName:"
//					 + packageName);
					
					protectPacknames = dao.findAll();

				//	if (dao.find(packageName)) {//查询数据库太慢了,消耗资源,改成查村内存

					if(protectPacknames.contains(packageName)){//查询数据库太慢了,消耗资源,改成查村内存,效率很高
						// 判断这个程序是否需要临时的停止保护
						if (packageName.equals(tempStopProtectPackageName)) {

						} else {

							

							intent.putExtra("packageName", packageName);

							startActivity(intent);
						}
					}

					try {
						Thread.sleep(20);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}

			}
		}).start();

		super.onCreate();
	}

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	private class InnerReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			Log.i(TAG, "接受到了临时停止保护广播的事件");
			tempStopProtectPackageName = intent.getStringExtra("packageName");
		}

	}
	
	private class ScreenOffReceiver extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			tempStopProtectPackageName = null;
		}

	}

	@Override
	public void onDestroy() {
		flag = false;
		unregisterReceiver(offReceiver);
		offReceiver = null;
		unregisterReceiver(receiver);
		receiver = null;
		super.onDestroy();
	}

}
这样做的确带来了优化,但是也带来了bug,锁定第一个应用还可以,当锁定第二个的时候,就不行了,直接进入软件的界面。

90程序锁的优化