Java 为什么每次启动屏幕更改时,我的应用程序总是从后台打开?
我创建了一个有3个启动屏幕的应用程序。它们在10秒后出现和消失,下一个将取代它,直到第三个结束,然后打开主要活动,应用程序将按其应该的方式工作 问题是,如果用户在任何启动屏幕期间将应用程序发送到后台,10秒后,即使用户正在使用另一个应用程序并显示下一个启动屏幕或主要活动,应用程序也会回到前台 我查看了代码,似乎找不到任何可以解释这一点的东西。在Android Studio升级到3.5之前,它运行良好,我不知道为什么会出现这个问题Java 为什么每次启动屏幕更改时,我的应用程序总是从后台打开?,java,android,android-studio,splash-screen,Java,Android,Android Studio,Splash Screen,我创建了一个有3个启动屏幕的应用程序。它们在10秒后出现和消失,下一个将取代它,直到第三个结束,然后打开主要活动,应用程序将按其应该的方式工作 问题是,如果用户在任何启动屏幕期间将应用程序发送到后台,10秒后,即使用户正在使用另一个应用程序并显示下一个启动屏幕或主要活动,应用程序也会回到前台 我查看了代码,似乎找不到任何可以解释这一点的东西。在Android Studio升级到3.5之前,它运行良好,我不知道为什么会出现这个问题 public class loadScreen extends
public class loadScreen extends AppCompatActivity {
private int SLEEP_TIMER = 3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_load_screen);
LogoLauncher logoLauncher = new LogoLauncher();
logoLauncher.start();
}
private class LogoLauncher extends Thread{
public void run(){
try{
sleep(3000 * SLEEP_TIMER);
}catch(InterruptedException e)
{
e.printStackTrace();
}
Intent intent = new Intent(loadScreen.this, createdby.class);
startActivity(intent);
loadScreen.this.finish();
}
}
@Override
public void onBackPressed() {
}
}
我预计,如果用户返回应用程序时,应用程序将在启动屏幕序列期间被发送到后台,它将从用户离开它的位置恢复。您的问题是,即使您的应用程序位于后台,也会调用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();
}
你的问题是,即使你的应用程序在后台,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();
}
即使用户正在使用该应用程序,该应用程序也会回到前台
另一个应用程序
即使你的应用程序进入后台,你的线程仍在运行!,
解决办法是,
您必须在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);
}
}
SplashActivity 1仅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);
}
}
即使用户正在使用该应用程序,该应用程序也会回到前台
另一个应用程序
即使你的应用程序进入后台,你的线程仍在运行!,
解决办法是,
您必须在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);
}
}
SplashActivity 1仅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);
}
}
你会建议我怎么做?谢谢,朋友。你是一个活生生的储蓄者。你会建议我怎么做?谢谢,朋友。你是一个活生生的储蓄者。我该如何实施你的建议?我该如何实施你的建议?