簡體   English   中英

如何以編程方式為應用設置鎖或 pin

[英]How to programmatically set a lock or pin for an app

所以現在我正在嘗試為我的孩子們開發一個 Android 應用程序。 我想在特定時間段內為選定的應用程序設置 PIN 碼或密碼,以防止它們打開應用程序。 例如,假設我的女兒想在我工作時在我的手機上玩一段時間的憤怒的小鳥。 當她玩憤怒的小鳥時,我會選擇我的重要應用程序,例如消息傳遞、gmail 等,並在上面放置 30 分鍾的 PIN 碼或密碼。 30 分鍾后,我從女兒那里拿到手機,我可以在沒有密碼的情況下打開應用程序,因為時限已過。

我對此進行了大量研究,但我一直無法找到適合我的特定案例的實現。

android“應用鎖”應用程序如何工作?

我知道應用程序鎖與我想要做的具有類似的結構。 我只是為鎖定設置一個時間限制。

https://play.google.com/store/apps/details?id=com.domobile.applock&hl=en

我正在遠離使用 ActivityManager 等殺死活動/應用程序。我真的只想在選定的應用程序上在特定時間段內有一個干凈的鎖定屏幕。

我有一個 CountdownTimer 來倒計時我設置的時間。 如果我擁有所有包名,我將如何修改此代碼以在選定的時間內阻止某些應用程序?

    start_timer.setOnClickListener(new View.OnClickListener() {


        @Override
        public void onClick(View view) {

            new AlertDialog.Builder( MainActivity.this )
                    .setMessage( "Are you sure you want to block the selected apps for the set amount of time?" )
                    .setPositiveButton( "Yeah man!", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            Log.d( "AlertDialog", "Positive" );

                            hourint = Integer.valueOf(number_text.getText().toString());

                            minuteint = Integer.valueOf(minute_text.getText().toString());

                            secondint = Integer.valueOf(second_text.getText().toString());

                            Log.i("YourActivity", "Hours: " + hourint);

                            Log.i("YourActivity", "Minutes: " + minuteint);

                            Log.i("YourActivity", "Seconds: " + secondint);

                            totalTimeCountInMilliseconds = ((hourint*60*60) +(minuteint*60) + (secondint)) * 1000;      // time count
                            timeBlinkInMilliseconds = 30*1000;

                            countDownTimer = new CountDownTimer(totalTimeCountInMilliseconds, 500) {
                                // 500 means, onTick function will be called at every 500 milliseconds

                                @Override
                                public void onTick(long leftTimeInMilliseconds) {
                                    Context context = MainActivity.this;





                                    long seconds = leftTimeInMilliseconds / 1000;
                                    mSeekArc.setVisibility(View.INVISIBLE);
                                    start_timer.setVisibility(View.INVISIBLE);
                                    block_button1.setVisibility(View.INVISIBLE);



                                    if ( leftTimeInMilliseconds < timeBlinkInMilliseconds ) {
                                        // textViewShowTime.setTextAppearance(getApplicationContext(), R.style.blinkText);
                                        // change the style of the textview .. giving a red alert style

                                        if ( blink ) {
                                            number_text.setVisibility(View.VISIBLE);
                                            minute_text.setVisibility(View.VISIBLE);
                                            second_text.setVisibility(View.VISIBLE);


                                            // if blink is true, textview will be visible
                                        } else {
                                            number_text.setVisibility(View.INVISIBLE);
                                            minute_text.setVisibility(View.INVISIBLE);
                                            second_text.setVisibility(View.INVISIBLE);


                                        }

                                        blink = !blink;         // toggle the value of blink
                                    }

                                    second_text.setText(String.format("%02d", seconds % 60));
                                    minute_text.setText(String.format("%02d", (seconds / 60) % 60));
                                    number_text.setText(String.format("%02d", seconds / 3600));                     // format the textview to show the easily readable format
                                }


                                @Override
                                public void onFinish() {
                                    // this function will be called when the timecount is finished
                                    //textViewShowTime.setText("Time up!");
                                    number_text.setVisibility(View.VISIBLE);
                                    minute_text.setVisibility(View.VISIBLE);
                                    second_text.setVisibility(View.VISIBLE);
                                    mSeekArc.setVisibility(View.VISIBLE);
                                    start_timer.setVisibility(View.VISIBLE);
                                    block_button1.setVisibility(View.VISIBLE);


                                }

                            }.start();
                        }
                    })
                    .setNegativeButton("Nope!", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            Log.d("AlertDialog", "Negative");
                            dialog.cancel();
                        }
                    })
                    .show();

編輯: http : //pastebin.com/MHGFw7PK

邏輯

  • 當您想阻止應用程序時,您必須創建並啟動服務,
  • 並且在服務中,您必須檢查應用程序的包名,以便您可以決定運行哪個應用程序以及顯示密碼/密碼活動

現在代碼示例

  • 要啟動一個服務,代碼如下,

     startService(new Intent(this, SaveMyAppsService.class));
  • 現在,在您的服務中,檢查這樣的包裹,

     public class SaveMyAppsService extends android.app.Service { String CURRENT_PACKAGE_NAME = {your this app packagename}; String lastAppPN = ""; boolean noDelay = false; public static SaveMyAppsService instance; @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public int onStartCommand(Intent intent, int flags, int startId) { // TODO Auto-generated method stub scheduleMethod(); CURRENT_PACKAGE_NAME = getApplicationContext().getPackageName(); Log.e("Current PN", "" + CURRENT_PACKAGE_NAME); instance = this; return START_STICKY; } private void scheduleMethod() { // TODO Auto-generated method stub ScheduledExecutorService scheduler = Executors .newSingleThreadScheduledExecutor(); scheduler.scheduleAtFixedRate(new Runnable() { @Override public void run() { // TODO Auto-generated method stub // This method will check for the Running apps after every 100ms if(30 minutes spent){ stop(); }else{ checkRunningApps(); } } }, 0, 100, TimeUnit.MILLISECONDS); } public void checkRunningApps() { ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningTaskInfo> RunningTask = mActivityManager.getRunningTasks(1); ActivityManager.RunningTaskInfo ar = RunningTask.get(0); String activityOnTop = ar.topActivity.getPackageName(); Log.e("activity on TOp", "" + activityOnTop); // Provide the packagename(s) of apps here, you want to show password activity if (activityOnTop.contains("whatsapp") // you can make this check even better || activityOnTop.contains(CURRENT_PACKAGE_NAME)) { // Show Password Activity } else { // DO nothing } } public static void stop() { if (instance != null) { instance.stopSelf(); } } }

編輯:(獲取棒棒糖的頂級包名稱)

一個很好的答案在這里。

String lastAppPN = "";
public void checkRunningApps() {
    ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    String activityOnTop;
    if (Build.VERSION.SDK_INT > 20) {
        activityOnTop = mActivityManager.getRunningAppProcesses().get(0).processName;
    } else {
        List<ActivityManager.RunningTaskInfo> RunningTask = mActivityManager.getRunningTasks(1);
        ActivityManager.RunningTaskInfo ar = RunningTask.get(0);
        activityOnTop = ar.topActivity.getPackageName();
    }
    //Log.e("activity on TOp", "" + activityOnTop);

    // Provide the packagename(s) of apps here, you want to show password activity
    if (activityOnTop.contains("whatsapp")  // you can make this check even better
            || activityOnTop.contains(CURRENT_PACKAGE_NAME)) {
        if (!(lastAppPN.equals(activityOnTop))) {
            lastAppPN = activityOnTop;
            Log.e("Whatsapp", "started");
        }
    } else {
        if (lastAppPN.contains("whatsapp")) {
            if (!(activityOnTop.equals(lastAppPN))) {
                Log.e("Whatsapp", "stoped");
                lastAppPN = "";
            }
        }
        // DO nothing
    }
}

我創建了小型演示項目。 希望這可能對某人有用Link to project

您也可以使用 DialogAlertView:

  1. 使用共享首選項將密碼保存在存儲中。
  2. 如果用戶為應用程序啟用了密碼,則在主頁中顯示警報
  3. 詢問密碼,確認是否與保存的密碼相同。
  4. 如果是,關閉警報視圖,如果錯誤,再次顯示警報視圖
  5. 確保警報視圖 setCancellable() 為 false
  6. 您可以在視圖中擁有自己的設計和動畫

我認為這很容易做到。 沒有人也可以破解密碼。 假設如果有人卸載(或清除數據)應用程序,那么他們必須重新登錄或注冊(這樣更安全)。

提示 - 您可以將密碼保存在雲端或任何您想要的地方。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM