2 回答

TA貢獻1786條經驗 獲得超11個贊
您的問題是即使您的應用程序在后臺,也會調用 startActivity。這將在調用時打開您的應用程序。因此,您需要在該部分中創(chuàng)建一些邏輯來檢查是否允許調用 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發(fā)生變化。
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);
}
}
添加回答
舉報