这个问题不言而喻。
我正在做一个类,注册用户的活动,以避免不活动,2分钟后没有活动或每次用户最小化应用程序,应用程序应该关闭。我的代码如下:

public class InActivityTimer extends Activity {

    public static final long DISCONNECT_TIMEOUT = 2*60*1000; // 2 min = 2 * 60 * 1000 ms
    public static int ESTADO_ACTIVIDAD=0; //Variable para saber si la actividad está al frente o no.

    private static Handler disconnectHandler = new Handler(){
        public void handleMessage(Message msg) {
        }
    };

    private Runnable disconnectCallback = new Runnable() {
        @Override
        public void run() {
            // Perform any required operation on disconnect
            //finish();
            finishAffinity();
            Intent intent = new Intent(getApplicationContext(),VentanaInactividadCierreAplicacion.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);

            //intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(intent);
        }
    };

    public void resetDisconnectTimer(){
        disconnectHandler.removeCallbacks(disconnectCallback);
        disconnectHandler.postDelayed(disconnectCallback, DISCONNECT_TIMEOUT);
    }

    public void stopDisconnectTimer(){
        disconnectHandler.removeCallbacks(disconnectCallback);
    }

    @Override
    public void onUserInteraction(){
        resetDisconnectTimer();
    }

    @Override
    public void onResume() {
        super.onResume();
        ESTADO_ACTIVIDAD ++;
        resetDisconnectTimer();
        comprobarEstado();
    }

    private void comprobarEstado() {

        if (ESTADO_ACTIVIDAD == 0){
//            Intent startMain = new Intent(getApplicationContext(), SplashScreen.class);
//            startMain.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
//            startActivity(startMain);
//            finish();
            finishAffinity();
            Toast.makeText(getApplicationContext(), "RandomPassGEN cerrado por seguridad", Toast.LENGTH_SHORT).show();}
    }

    @Override
    public void onStop() {
        super.onStop();
        ESTADO_ACTIVIDAD--;
        stopDisconnectTimer();
        comprobarEstado();
    }

 /*   @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(getBaseContext(),"La aplicación se reiniciará por seguridad", Toast.LENGTH_SHORT).show();
        finishAffinity();
    }*/

}

如你所见,我用finishaffinty两次。我怎么能不用那个命令呢?我想避免它,因为使用finishaffinity()需要android+4.1
非常感谢你的帮助。

最佳答案

要关闭应用程序,请执行以下操作:

Intent intent = new Intent(this, MyRootActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("exit", true);
startActivity(intent);

MyRootActivity应该是任务的根活动(这是清单中action=main和category=launcher的活动)。
MyRootActivity.onCreate()中,在super.onCreate()之后添加以下内容:
if (getIntent().hasExtra("exit")) {
    // User wants to exit, so do that
   finish();
   return;
}
... here is the rest of your onCreate() code

只有当MyRootActivity在启动工作流中的下一个活动时不调用finish()时,此操作才有效。如果MyRootActivity在开始下一个活动时确实调用了finish(),则应该更改它,这样就不会调用了。在这种情况下,需要将用户返回MyRootActivity的情况作为特殊情况处理。

07-24 09:46