2014-01-28 27 views
0

我的应用程序从启动画面开始。启动屏幕可见5秒,然后开始菜单活动。但是当显示屏上出现闪屏时,我将其按回或在家中button应用程序会在后台进入,但在几秒钟后,它会自动随着菜单活动自动进入前台。我想如果用户长时间按住home或back关键应用程序。这是我到目前为止所尝试过的。如何关闭活动中的应用程序

闪屏活动 -

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

你可以看到和onBackPressed我关闭应用程序。但它从几秒钟后的菜单活动开始。

+0

你的问题就出在处理程序()。postDelayed()。这将在5000毫秒后执行,即使您的应用程序已隐藏。你必须取消处理程序。看到这个问题的答案:http://stackoverflow.com/questions/4378533/cancelling-a-handler-postdelayed-process – kasoban

+0

你可以尝试调用处理程序。[removeCallbacks](http://developer.android.com/reference/你的'onStop'方法中的android/os/Handler.html#removeCallbacks(java.lang.Runnable)) – suitianshi

+0

但是在我的代码中没有处理程序的名称。那么如何做到这一点? –

回答

0

什么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); 
    } 
} 
0

如,克里斯提到,全球的声明处理程序,

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

你在呼唤你“ csRunnable2“,5秒后,所以,它会在5秒后出现,直到除非,

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

=>使所有引用为空。

所以,当你永远是关闭或(在onBackPressed(),在onPause()的onStop()的onDestroy()等,作为。)暂停广告活动一定要拨打:

handler.removeCallbacksAndMessages(null); 

在开始下一个活动之前。

这将使所有Handler实例都为null。

对于例如,在你的代码,你可以打电话,

 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(); 
} 
相关问题