防止HOME按钮关闭活动 [英] Preventing the HOME button to close activity

查看:79
本文介绍了防止HOME按钮关闭活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am currently using a custom AlertDialog which has a Theme.Dialog theme in a Activity that requires users to type the current password to proceed.

根据> Android-是否可能禁用主页按钮的点击,我尝试了大多数方法,但是不起作用.

According to Android - Is It possible to disable the click of home button i have tried most of the methods but it doesn't work.

活动-onCreate

Activity - 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);
        }
    }

Logcat

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

推荐答案

McAfee Wave Secure通过以下方式进行此操作:他们运行后台服务,每隔几秒钟检查一次屏幕是否处于活动状态.如果不是,则Service重新启动您的活动或将其置于最前面(使其活动).在onPause中,您可以保存输入数据以在活动重新开始时重新加载.

McAfee Wave Secure did it in the following way: they run a background service that checks every couple seconds if your screen is active. If not Service relaunch your activity or bring it to the front (make it active). In onPause you can save input data to reload it when activity restarted.

我的例子:

// 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"是因为我得到了同一活动的许多实例.也许有一种方法可以使其活跃起来.

I do "FLAG_ACTIVITY_NEW_TASK" because I`ve been gotten many instances of the same activity. Maybe there is a way just to make it active.

示例"PSEUDO"

Example "PSEUDO"

{
    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");
    }

}

这篇关于防止HOME按钮关闭活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆