Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android安全問題之程序鎖

android安全問題之程序鎖

編輯:關於Android編程

原理:

啟動服務掃描運行中的task棧,取出task中的topActivity,然後進行判斷,如果是你想鎖定的應用,然後彈出一個需要輸入密碼的activity對原程序進行覆蓋,當驗證通過才讓用戶使用應用。

代碼如下:

啟動類MainActivity(也可使用廣播作為入口):

package com.home.testlock;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class MainActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		startService(new Intent(this, LockService.class));
	}
}

程序鎖的後台服務類LockService:

package com.home.testlock;

import java.util.Timer;

import android.app.Notification;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

/**
 * 程序鎖的後台服務類
 *  
 */
public class LockService extends Service {
	private Timer timer;// 定時器
	public static final int FOREGROUND_ID = 0;

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

	@Override
	public void onCreate() {
		super.onCreate();
		startForeground(FOREGROUND_ID, new Notification());
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		startTimer();
		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public void onDestroy() {
		stopForeground(true);
		timer.cancel();
		timer.purge();
		timer = null;
		super.onDestroy();
	}

	/**
	 * 啟動定時器任務
	 */
	private void startTimer() {
		if (timer == null) {
			timer = new Timer();
			LockTask lockTask = new LockTask(this);
			timer.schedule(lockTask, 0L, 1000L);
		}
	}
}

程序鎖的任務類LockTask:

package com.home.testlock;

import java.util.TimerTask;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;

/**
 * 程序鎖的任務類
 * 
 * 
 */
public class LockTask extends TimerTask {
	private Context mContext;
	// 這是筆者自己手機上的測試應用
	private String testPackageName = "com.lovo.sport";
	private String testClassName = "com.lovo.ui.RegistActivity";
	private ActivityManager am;

	public LockTask(Context context) {
		mContext = context;
		am = (ActivityManager) mContext.getSystemService("activity");
	}

	@Override
	public void run() {
		ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
		String packageName = cn.getPackageName();
		String className = cn.getClassName();
		if (SharedUtil.getIntence().getIsFinished(mContext)) {
			return;
		}
		// 如果想對整個程序鎖定判斷包名即可
		if (testPackageName.equals(packageName)
				&& testClassName.equals(className)) {
			Intent intent = new Intent();
			intent.setClassName("com.home.testlock",
					"com.home.testlock.PWDActivity");
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			mContext.startActivity(intent);
		}
	}

}

PWDActivity:

package com.home.testlock;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class PWDActivity extends Activity implements OnClickListener {
	private Button sureBtn;
	private EditText pwdText;
	private static final String PASSWORD = "1234";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pwd);
		initWidget();
	}

	private void initWidget() {
		sureBtn = (Button) findViewById(R.id.pwd_btn_sure);
		pwdText = (EditText) findViewById(R.id.pwd_et);
		sureBtn.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		if (v == sureBtn) {
			String pwd = pwdText.getText().toString();
			if (PASSWORD.equals(pwd)) {
				SharedUtil.getIntence().saveIsFinished(this, true);
				finish();
			}
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (KeyEvent.KEYCODE_BACK == keyCode) {
			return true;// 屏蔽返回鍵
		}
		return super.onKeyDown(keyCode, event);
	}
}

SharedUtil:

package com.home.testlock;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * 處理SharedPreferences工具類
 * 
 * @author 孤雲
 * 
 */
public class SharedUtil {

	private static SharedUtil shared;

	/**
	 * 使用單例模式獲取SharedUtil實例
	 * 
	 * @return
	 */
	public static synchronized SharedUtil getIntence() {
		if (shared == null) {
			shared = new SharedUtil();
		}
		return shared;
	}

	/**
	 * 獲取是否驗證過密碼
	 * 
	 * @param context
	 * @return
	 */
	public boolean getIsFinished(Context context) {
		SharedPreferences sp = context.getSharedPreferences("data",
				Context.MODE_PRIVATE);
		boolean isFinished = sp.getBoolean("isFinished", false);
		return isFinished;
	}

	/**
	 * 保存驗證邏輯
	 * 
	 * @param context
	 * @param isFinished
	 */
	public void saveIsFinished(Context context, boolean isFinished) {
		SharedPreferences sp = context.getSharedPreferences("data",
				Context.MODE_PRIVATE);
		sp.edit().putBoolean("isFinished", isFinished).commit();
	}
}

布局文件省略。

權限:

另外:如果不想某個本程序的某個activity顯示在task中,對其activity加如下屬性:

 android:excludeFromRecents="true" 


本文核心代碼轉自:http://su1216.iteye.com/blog/1699681

請大家不要用root的手機隨意下載軟件,更不要以任何借口制造任何病毒!





  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved