2 回答
TA贡献1786条经验 获得超11个赞
您的问题是即使您的应用程序在后台,也会调用 startActivity。这将在调用时打开您的应用程序。因此,您需要在该部分中创建一些逻辑来检查是否允许调用 startActivity 方法。
编辑:用于检查活动的待启动的代码。试试这个!
private static final String PENDING_LAUNCH_KEY = "PENDING_LAUNCH";
private boolean pendingLaunch;
private boolean activityPaused;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
pendingLaunch = savedInstanceState.getBoolean(PENDING_LAUNCH_KEY);
}
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_load_screen);
if (!pendingLaunch) {
LogoLauncher logoLauncher = new LogoLauncher();
logoLauncher.start();
}
}
@Override
protected void onResume() {
activityPaused = false;
if (pendingLaunch) {
pendingLaunch = false;
startAndFinish();
}
}
@Override
protected void onPause() {
activityPaused = true;
}
@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
outState.putBoolean(PENDING_LAUNCH_KEY, pendingLaunch);
super.onSaveInstanceState(outState, outPersistentState);
}
private class LogoLauncher extends Thread{
public void run(){
try{
sleep(3000 * SLEEP_TIMER);
}catch(InterruptedException e)
{
e.printStackTrace();
}
if (activityPaused) pendingLaunch = true;
else startAndFinish();
}
}
private void startAndFinish() {
Intent intent = new Intent(loadScreen.this, createdby.class);
startActivity(intent);
finish();
}
TA贡献1802条经验 获得超4个赞
即使用户正在使用另一个应用程序,该应用程序也会将自己带回前台
即使您的应用程序进入后台,您的线程仍在运行!解决方案是,您必须在方法中使用terminate
该线程onPause
它将从他们离开的地方恢复。
飞溅活动
public class SplashActivity extends AppCompatActivity {
private Handler handler = null;
private long SPLASH_TIMEOUT = 5000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
handler = new Handler();
}
private Runnable splashRunnable = new Runnable() {
@Override
public void run() {
Intent mySuperIntent = new Intent(SplashActivity.this, SplashActivity1.class);
startActivity(mySuperIntent);
finish();
}
};
@Override
protected void onPause() {
super.onPause();
handler.removeCallbacks(splashRunnable);
}
@Override
protected void onResume() {
super.onResume();
handler.postDelayed(splashRunnable, SPLASH_TIMEOUT);
}
}
SplashActivity1与SplashActivity 相同的代码只会intent发生变化。
import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class SplashActivity1 extends AppCompatActivity {
private Handler handler = null;
private long SPLASH_TIMEOUT = 5000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash1);
handler = new Handler();
}
private Runnable splashRunnable = new Runnable() {
@Override
public void run() {
Intent mySuperIntent = new Intent(SplashActivity1.this,
MainActivity.class);
startActivity(mySuperIntent);
finish();
}
};
@Override
protected void onPause() {
super.onPause();
handler.removeCallbacks(splashRunnable);
}
@Override
protected void onResume() {
super.onResume();
handler.postDelayed(splashRunnable, SPLASH_TIMEOUT);
}
}
添加回答
举报