【问题标题】:How to close app in activity如何在活动中关闭应用程序
【发布时间】:2014-02-19 13:28:00
【问题描述】:

我的应用以启动画面开始。闪屏可见 5 秒,然后菜单活动开始。但是,当显示启动画面时,我按下返回键或主页button 应用程序进入后台,但几秒钟后,它会随着菜单活动自动进入前台。我想如果用户按下主页或返回键应用程序永久关闭。这是我到目前为止所尝试的。

启动画面活动-

public class SplashScreen extends Activity 
{
    /** Called when the activity is first created. */
    TimerTask task;
    Intent objIntent, intent;

    @Override
    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.splash);
       UtilClass.playing = true;
       objIntent = new Intent(SplashScreen.this, PlayAudio.class);
       startService(objIntent);
       new Handler().postDelayed(csRunnable2, 5000);  
       }

   Runnable csRunnable2=new Runnable() 
    {      
       @Override
       public void run() 
       {
            intent = new Intent(SplashScreen.this, MainActivity.class);
            startActivity(intent);
            finish();
       }
   };

   public void onBackPressed()  
    {    
        objIntent = new Intent(this, PlayAudio.class);
        stopService(objIntent);
        finish();
        return;  
    }  

   @Override
    protected void onPause() {
        super.onPause();
        objIntent = new Intent(this, PlayAudio.class);
        stopService(objIntent);
        finish();
    }
}

您可以在onPauseonBackPressed 中看到我正在关闭应用程序。但它会在几秒钟后从菜单活动开始。

【问题讨论】:

  • 你的问题出在Handler().postDelayed()。这将在 5000 毫秒后执行,即使您的应用程序已隐藏。您必须取消处理程序。请参阅此问题的答案:stackoverflow.com/questions/4378533/…
  • 你可以尝试在你的onStop方法中调用handler.removeCallbacks
  • 但在我的代码中没有处理程序的名称。那该怎么做呢?

标签: android android-activity activity-finish


【解决方案1】:

suitianshi 建议的是

    public class SplashScreen extends Activity
{
    /** Called when the activity is first created. */
    TimerTask task;
    Intent objIntent, intent;
    Handler handler;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        UtilClass.playing = true;
        objIntent = new Intent(SplashScreen.this, PlayAudio.class);
        startService(objIntent);
        handler = new Handler();
        handler.postDelayed(csRunnable2, 5000);
    }

    Runnable csRunnable2 = new Runnable()
    {
        @Override
        public void run()
        {
            intent = new Intent(SplashScreen.this, MainActivity.class);
            startActivity(intent);
            finish();
        }
    };

    public void onBackPressed()
    {
        objIntent = new Intent(this, PlayAudio.class);
        stopService(objIntent);
        finish();
        return;
    }

    @Override
    protected void onPause() {
        super.onPause();
        objIntent = new Intent(this, PlayAudio.class);
        stopService(objIntent);
        finish();
    }

    @Override
    protected void onStop() {
        super.onStop();
        handler.removeCallbacks(csRunnable2);
    }
}

【讨论】:

    【解决方案2】:

    正如 Chris 所说,将您的 HANDLER 全局声明为,

     Handler handler;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        UtilClass.playing = true;
        objIntent = new Intent(SplashScreen.this, PlayAudio.class);
        startService(objIntent);
        handler = new Handler();
        handler.postDelayed(csRunnable2, 5000);
    }
    

    在这一行,

            handler.postDelayed(csRunnable2, 5000);
    

    你在 5 秒后调用你的“csRunnable2”,所以,它会在 5 秒后出现,直到除非,

    => 您完全销毁处理程序类的实例,或者,

    => 将所有引用设为 Null。

    所以,当你关闭或暂停你的活动时(如 onBackPressed()、onPause()、onStop()、onDestroy() 等),请确保调用:

     handler.removeCallbacksAndMessages(null);
    

    在您开始下一个活动之前。

    这将使所有 Handler 实例为空。

    例如,在您的代码中,您可以调用,

         public void onBackPressed()
    {
        handler.removeCallbacksAndMessages(null); 
        objIntent = new Intent(this, PlayAudio.class);
        stopService(objIntent);
        finish();
        return;
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        handler.removeCallbacksAndMessages(null);
        objIntent = new Intent(this, PlayAudio.class);
        stopService(objIntent);
        finish();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-03
      相关资源
      最近更新 更多