0

我目前正在使用自定义 AlertDialog,它在 Activity 中具有 Theme.Dialog 主题,需要用户输入当前密码才能继续。

根据Android - 是否可以禁用单击主页按钮我已经尝试了大多数方法,但它不起作用。

活动 - onCreate

      SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(PhysicalTheftDialog.this);

        boolean isServerRunning = sp.getBoolean("isServerRunning", false);
            if (isServerRunning == false) {
                startService(new Intent(PhysicalTheftDialog.this, MyService.class));

            }
            else {
                sp.getBoolean("isServerRunning", true);
            }

服务

    @Override
    public void onCreate() {
/*      //Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onCreate");

        player = MediaPlayer.create(this, R.raw.braincandy);
        player.setOnPreparedListener(this);
        try {
            player.prepareAsync();

        } catch (Exception e) {
            Log.e(TAG, "", e);
        }
        player.setLooping(true); // Set looping*/
        this.context = getApplicationContext();
        //Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
        boolean isServerRunning = sp.getBoolean("isServerRunning", true);

        while (!isServerRunning) {
            // THAT CODE: check if activity is on the top
            Handler handler = new Handler(); 

            handler.postDelayed(new Runnable() { 
                 public void run() { 

                        // get a list of running processes and iterate through them
                        ActivityManager activityManager = (ActivityManager)context.getSystemService(Activity.ACTIVITY_SERVICE);

                        // get the info from the currently running task
                        List<ActivityManager.RunningTaskInfo> taskInfo = activityManager.getRunningTasks(1);
                        ComponentName componentInfo = taskInfo.get(0).topActivity;

                        // if top activity is not "MyActivity" activity, then launch it
                        if("nyp.android.project".equals(componentInfo.getClassName()) == false) {
                            Intent intent = new Intent(MyService.this, PhysicalTheftDialog.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
}
                 } 
            }, 2000); 

            boolean serviceStatus = sp.getBoolean("isServerRunning", false);
        }
    }

日志猫

E/AndroidRuntime( 9799): FATAL EXCEPTION: main
E/AndroidRuntime( 9799): java.lang.RuntimeException: Unable to start service nyp.android.project.MyService@41b29ff0 with Intent { cmp=xxxx }: java.lang.NullPointerException
E/AndroidRuntime( 9799):    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2387)
E/AndroidRuntime( 9799):    at android.app.ActivityThread.access$1900(ActivityThread.java:127)
E/AndroidRuntime( 9799):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1221)
E/AndroidRuntime( 9799):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 9799):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime( 9799):    at android.app.ActivityThread.main(ActivityThread.java:4511)
E/AndroidRuntime( 9799):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 9799):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime( 9799):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
E/AndroidRuntime( 9799):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
E/AndroidRuntime( 9799):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 9799): Caused by: java.lang.NullPointerException
E/AndroidRuntime( 9799):    at nyp.android.project.MyService.onStart(MyService.java:95)
E/AndroidRuntime( 9799):    at android.app.Service.onStartCommand(Service.java:438)
E/AndroidRuntime( 9799):    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2370)
E/AndroidRuntime( 9799):    ... 10 more
4

4 回答 4

1

McAfee Wave Secure 通过以下方式做到这一点:他们运行一个后台服务,每隔几秒钟检查一次您的屏幕是否处于活动状态。如果不是服务重新启动您的活动或将其带到前面(使其处于活动状态)。在 onPause 中,您可以保存输入数据以在活动重新启动时重新加载它。

我的例子:

// get a list of running processes and iterate through them
ActivityManager activityManager = (ActivityManager)_context
        .getSystemService(Activity.ACTIVITY_SERVICE);

// get the info from the currently running task
List<ActivityManager.RunningTaskInfo> taskInfo = activityManager.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;

// if top activity is not "MyActivity" activity, then launch it
if(componentInfo.getClassName().equals("com.namespace.MyActivity") == false) {
    Intent intent = new Intent(context, MyActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intent);
}

我做“FLAG_ACTIVITY_NEW_TASK”是因为我已经获得了许多相同活动的实例。也许有一种方法可以让它活跃起来。

示例“伪”

{
    Activity:
    preference isServerRunning = getPreference("isServerRunning");
    if (serviceIsRunning == false) {
        runService();
    }
    // othervise, service is running and will do the job
    Onclick.UserLogged() {
        setPreference("isUserLoged", true);
    }

    Service:
    while (!userIsLogged) {
        // THAT CODE: check if activity is on the top

        sleepFor2seconds();
        userIsLogger = getPreference("isUserLogged");
    }

}
于 2012-07-25T14:48:00.787 回答
0

尝试覆盖 onPause() 方法,以便当按下主页按钮调用它时,活动不会停止

于 2012-07-25T14:42:11.353 回答
0

尝试覆盖要禁用 HOME 按钮的 Activity 类的 onKeyDown 方法。在此方法中,检查用户是否按下了 HOME 按钮。

于 2012-07-25T14:48:49.460 回答
0

也许我迟到了但是

这是唯一可能的方法

首先你必须检测到家被按下

看到这个答案来检测它

检测主页按钮按下

对以上答案的这一部分进行更改

 HomeWatcher homeWatcher=new HomeWatcher(this);
    homeWatcher.setOnHomePressedListener(new OnHomePressedListener() {
        @Override
        public void onHomePressed() {
            //So Here The Main Thing Is..

            //You Will Have To Figure Out A Condition On When You Do You Want The Home Button Not To Close The Activity

            //In The Condition You Have To Restart The Activity 

            Intent intent = getIntent();
            overridePendingTransition(0, 0);
            intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
            finish();
            overridePendingTransition(0, 0);
            startActivity(intent);
           }
        });

所以这段代码将禁用主页按钮(不完全是一种)

我希望你理解它并且工作正常......如果没有,请随时与我联系(与此相关的任何其他类型的其他问题对此的解释)

于 2022-01-30T09:23:11.453 回答