【发布时间】: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();
}
}
您可以在onPause 和onBackPressed 中看到我正在关闭应用程序。但它会在几秒钟后从菜单活动开始。
【问题讨论】:
-
你的问题出在Handler().postDelayed()。这将在 5000 毫秒后执行,即使您的应用程序已隐藏。您必须取消处理程序。请参阅此问题的答案:stackoverflow.com/questions/4378533/…
-
你可以尝试在你的
onStop方法中调用handler.removeCallbacks -
但在我的代码中没有处理程序的名称。那该怎么做呢?
标签: android android-activity activity-finish