日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android安全问题(二) 程序锁

發布時間:2025/4/5 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android安全问题(二) 程序锁 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

導讀:本文介紹如何實現對應用加鎖的功能,無須root權限


某些人有時候會有這樣一種需求,小A下載了個軟件,只是軟件中的美女過于誘惑與暴露,所以他不想讓別人知道這是個什么軟件,起碼不想讓別人打開瀏覽。而這款軟件又沒有鎖,任何人都可以打開,腫么辦呢?如果打開它的時候需要輸入密碼,那該多好阿!于是,程序鎖這種應用就產生了


程序鎖不是最近才有的,很久之前android就有這種apk了

這一期我們來苛刻如何實現程序加鎖功能


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

1可以選擇需要加鎖的程序

2可以設置密碼

3可以關閉程序鎖


這里作為演示,我們就盡量簡化代碼

我們先說最關鍵的部分

最關鍵的地方在于:當用戶打開一個應用的時候,怎么彈出密碼頁面?

這里沒有什么太好的辦法,需要掃描task中的topActivity

首先,我們先獲得運行的task

Java代碼
  • mActivityManager = (ActivityManager) context.getSystemService("activity"); ?

  • //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代碼
  • ComponentName topActivity = mActivityManager.getRunningTasks(1).get(0).topActivity; ?

  • ComponentName topActivity = mActivityManager.getRunningTasks(1).get(0).topActivity;

    topActivity居然是ComponentName類型,下面的事情就好辦了,獲得包名和類名

    Java代碼
  • 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); ?

  • } ?

  • 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代碼
  • String testPackageName = "com.htc.notes"; ?

  • String testClassName = "com.htc.notes.collection.NotesGridViewActivity"; ?

  • String testPackageName = "com.htc.notes"; String testClassName = "com.htc.notes.collection.NotesGridViewActivity";


    下面我們該想,這段代碼何時執行了

    打開一個應用程序,系統不會發送廣播,我們無法直接監聽,所以這里我們采取定時掃描的策略

    這里只是一個簡單的實現,之后我們再討論優化

    我們采取每秒中檢查一次task的方式,這里使用Timer吧,用Handler也一樣可以實現

    Java代碼
  • private Timer mTimer; ?

  • privatevoid startTimer() { ?

  • if (mTimer == null) { ?

  • ? ? ? ?mTimer = new Timer(); ?

  • ? ? ? ?LockTask lockTask = new LockTask(this); ?

  • ? ? ? ?mTimer.schedule(lockTask, 0L, 1000L); ?

  • ? ?} ?

  • } ?

  • 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代碼
  • publicclass LockTask extends TimerTask { ?

  • publicstaticfinal 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

  • publicvoid 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); ?

  • ? ? ? ?} ?

  • ? ?} ?

  • } ?

  • 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");}@Overridepublic 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代碼
  • publicclass LockService extends Service { ?

  • private Timer mTimer; ?

  • publicstaticfinalint FOREGROUND_ID = 0; ?


  • privatevoid startTimer() { ?

  • if (mTimer == null) { ?

  • ? ? ? ? ? ?mTimer = new Timer(); ?

  • ? ? ? ? ? ?LockTask lockTask = new LockTask(this); ?

  • ? ? ? ? ? ?mTimer.schedule(lockTask, 0L, 1000L); ?

  • ? ? ? ?} ?

  • ? ?} ?


  • public IBinder onBind(Intent intent) { ?

  • returnnull; ?

  • ? ?} ?


  • publicvoid onCreate() { ?

  • super.onCreate(); ?

  • ? ? ? ?startForeground(FOREGROUND_ID, new Notification()); ?

  • ? ?} ?


  • publicint onStartCommand(Intent intent, int flags, int startId) { ?

  • ? ? ? ?startTimer(); ?

  • returnsuper.onStartCommand(intent, flags, startId); ?

  • ? ?} ?


  • publicvoid onDestroy() { ?

  • ? ? ? ?stopForeground(true); ?

  • ? ? ? ?mTimer.cancel(); ?

  • ? ? ? ?mTimer.purge(); ?

  • ? ? ? ?mTimer = null; ?

  • super.onDestroy(); ?

  • ? ?} ?

  • } ?

  • 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代碼
  • publicclass MainActivity extends Activity { ?


  • publicvoid onCreate(Bundle savedInstanceState){ ?

  • super.onCreate(savedInstanceState); ?

  • ? ? ? ?startService(new Intent(this, LockService.class)); ?

  • ? ?} ?

  • } ?

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


    PasswordActivity,密碼頁面,很粗糙,沒有核對密碼邏輯,自行實現

    記得重寫onBackPressed函數,不然按返回鍵的時候……你懂的

    Java代碼
  • publicclass PasswordActivity extends Activity { ?


  • privatestaticfinal String TAG = "PasswordActivity"; ?

  • ? ?Button okButton; ?

  • ? ?EditText passwordEditText; ?

  • privateboolean mFinish = false; ?


  • @Override

  • protectedvoid 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() { ?

  • publicvoid onClick(View v) { ?

  • ? ? ? ? ? ? ? ?String password = passwordEditText.getText().toString(); ?

  • ? ? ? ? ? ? ? ?Log.v(TAG, "password" + password); ?

  • ? ? ? ? ? ? ? ?mFinish = true; ?

  • ? ? ? ? ? ? ? ?finish(); ?

  • ? ? ? ? ? ?} ?

  • ? ? ? ?}); ?

  • ? ?} ?


  • publicvoid onBackPressed(){} ?


  • publicvoid onPause(){ ?

  • super.onPause(); ?

  • if(!mFinish){ ?

  • ? ? ? ? ? ?finish(); ?

  • ? ? ? ?} ?

  • ? ?} ?

  • } ?

  • public class PasswordActivity extends Activity {private static final String TAG = "PasswordActivity";Button okButton;EditText passwordEditText;private boolean mFinish = false;@Overrideprotected 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代碼
  • <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"即可


    轉載于:https://blog.51cto.com/laokaddk/1211866

    總結

    以上是生活随笔為你收集整理的android安全问题(二) 程序锁的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。