我的应用程序以启动画面开始。启动屏幕可见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
中看到我正在关闭应用。但它在几秒钟之后就开始了菜单活动。
答案 0 :(得分:0)
whatianshi建议的是
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);
}
}
答案 1 :(得分:0)
正如,克里斯提到的,全球宣称你的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秒后出现,直到除非,
=>您完全破坏了处理程序类的实例,或
=>使所有引用都为空。
所以,当你关闭或暂停你的活动时(如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();
}