即使按下主页按钮或后退按钮,Android 应用程序也会重新打开并继续下一个活动
我编写了这个应用程序,在第一个屏幕上它有一个包含的线程。所以我给它计时了 7 秒,然后它将继续进行下一个活动。
问题是,每当我按下主页按钮时,音乐就会停止,并且会转到 Android 主屏幕,但在我的计时完成后(即 7 秒),应用程序将重新出现并显示下一个活动。
我尝试将 finish();
放在 onpause();
中,但它仍然显示下一个活动。
这是实际的代码。
public class HelloWorldActivity extends Activity {
MediaPlayer mp;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
mp = MediaPlayer.create(this, R.raw.otj);
mp.start();
Thread LogoTimer = new Thread(){
public void run(){
try{
int LogoTimer = 0;
while(LogoTimer < 7000){
sleep(100);
LogoTimer = LogoTimer + 100;
}
startActivity(new Intent("com.example.HelloWorld.CLEARSCREEN"));
} catch (InterruptedException e) {
e.printStackTrace();
}
finally{
finish();
}
}
};
LogoTimer.start();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
mp.release();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
mp.pause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
}
}
I wrote this app that in the first screen it has an included Thread on it. So it has I timed it like 7 seconds then it will proceed to the next activity.
The problem is whenever I hit the home button the music will stop and it will go to android homescreen but after my timed is done which is the 7 seconds, the app will reappear and will show the next activity.
I tried putting finish();
in the onpause();
but it's still showing the next activity.
here's the actual code.
public class HelloWorldActivity extends Activity {
MediaPlayer mp;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
mp = MediaPlayer.create(this, R.raw.otj);
mp.start();
Thread LogoTimer = new Thread(){
public void run(){
try{
int LogoTimer = 0;
while(LogoTimer < 7000){
sleep(100);
LogoTimer = LogoTimer + 100;
}
startActivity(new Intent("com.example.HelloWorld.CLEARSCREEN"));
} catch (InterruptedException e) {
e.printStackTrace();
}
finally{
finish();
}
}
};
LogoTimer.start();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
mp.release();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
mp.pause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,这是运行计时器的一种非常低效的方式。请尝试这种方式:
其次,当计时器最终触发时,您开始一项新活动。原始活动是否已完成并不重要。您的 startActivity() 正在它自己的线程上运行,并且无论如何都会执行。
postDelayed() 方法可能会像您期望的那样运行。如果没有,您需要让它在运行时检查是否应该真正启动该活动。但是,我认为 Handler 附加到默认的 Looper 上,这意味着如果主要活动完成,它将停止(或者更确切地说,消息不会被发布)。
First, that's a really inefficient way to run a timer. Try this way instead:
Second, your starting a new activity when that timer eventually fires. It doesn't matter that the originating activity is finished. Your startActivity() is running on it's own thread and will execute regardless.
It's possible the postDelayed() method will function like you expect. If not you'll need to have it check when it runs whether it should really start the activity. However, I think the Handler is attached to the default Looper which means it will stop (or rather, the message won't be posted) if the main activity finishes.
应用程序仍然在后台并且线程没有被销毁,因此它将触发startActivity。
我不会真正以这种方式设置启动屏幕,或者使用线程,除非我出于某种原因希望它脱离用户界面,即使这样也有更好的选择。
出于教育目的,要解决这个问题,您需要能够在 onPause() 中安全地中止线程,一种方法是下面的
Modifed Thread
The application is still in the background and the thread is not destroyed so it will fire the startActivity.
I would not really setup a splash screen this way, or use a thread unless I wanted it off the UI for some reason, even then there are better options.
For educational purposes to take care of this you need to be able to abort the thread safely in onPause() one way to do so is below
Modifed Thread