0

私は現在、ユーザーが続行するために現在のパスワードを入力する必要があるアクティビティで Theme.Dialog テーマを持つカスタム AlertDialog を使用しています。

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

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 に答える
0

ホームボタンを押して呼び出されたときにアクティビティが停止しないように、 onPause() メソッドをオーバーライドしてみてください

于 2012-07-25T14:42:11.353 に答える