杀闪屏上的意图 [英] Kill intent on splash screen
问题描述
这是我的开机画面,如果我preSS的家庭或意向时,启动应用程序崩溃,在多任务logcat中/ appswitch按钮是致命EXEPTION:螺纹1277。我可以杀/删除此意图时,播放器preSS home键?
公共类闪屏延伸活动{
私有静态诠释loadingTime = 1000;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow()。setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
。getWindow()addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); setVolumeControlStream(AudioManager.STREAM_MUSIC); 的setContentView(R.layout.loading_screen); 新的处理程序()。postDelayed(新的Runnable(){
@覆盖
公共无效的run(){
意图I =新意图(SplashScreen.this,MainActivity.class);
startActivity(ⅰ);
完();
}
},loadingTime);
}
}
以下code轨道是否 SplashActivity
至少部分地显示。如果是的话,它将继续 MainActivity
。如果不是(活动由$ P $完成pssing后退按钮,活动由pressing Home按钮停止)什么也不会发生。
该解决方案使用片段
其实如此的定时$ P $跨越例如pserved屏幕方向的变化(这将始终以指定的时间,无论多少次,你旋转你的设备 - 定时器不复位)。
公共类SplashActivity延伸活动{
//音轨时的活性至少部分可见(例如,对话下)
私人布尔mStarted = FALSE; @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState); //您当前code
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow()。setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
。getWindow()addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
的setContentView(R.layout.activity_startup); 如果(savedInstanceState == NULL){
//第一次的onCreate,创建一个开始倒计时片段
getFragmentManager()
.beginTransaction()
。新增(SplashFinishFragment.newInstance(),SplashFinishFragment.TAG)
。承诺();
}其他{
//片段已屏幕旋转之后,从第一次的onCreate成立
}
} @覆盖
保护无效调用onStart(){
//活性变得至少部分可见
mStarted = TRUE; super.onStart();
} @覆盖
保护无效的onStop(){
//活性不再可见
mStarted = FALSE; super.onStop();
} 公共布尔isStarted2(){
//存在已经隐藏方法isStarted()在框架
//你不能用它并不能覆盖它
返回mStarted;
} 公共静态类SplashFinishFragment扩展片段{
私有静态最后弦乐TAG = SplashFinishFragment.class.getSimpleName(); 私有静态最终诠释DELAY = 1000; //一秒的延迟 私有静态最后的处理程序mHandler =新的处理程序(); //一个主线程反正 私人最终可运行mRunnable =新的Runnable(){
@覆盖
公共无效的run(){
如果(getActivity()== NULL){
//这绝不应该发生,没有任何活动,所以没有片段
Log.e(TAG,不活动!);
返回;
} SplashActivity一个=(SplashActivity)getActivity(); 如果(a.isStarted2()|| a.isChangingConfigurations()){
//如果活动甚至部分可见的或现在的问题是旋转屏幕,继续
意图I =新意图(一,SettingsActivity.class);
a.startActivity(ⅰ);
} //在任何情况下接近飞溅
a.finish();
}
}; 公共静态的newInstance SplashFinishFragment(){
返回新SplashFinishFragment();
} @覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState); //倒计时将继续(不复位)跨屏幕旋转
setRetainInstance(真); //尝试运行指定的时间后的主要活动
mHandler.postDelayed(mRunnable,DELAY);
} @覆盖
公共无效的onDestroy(){
//如果片段被破坏(如活动关闭)不启动的主要活动
mHandler.removeCallbacks(mRunnable); super.onDestroy();
}
}
}
这是一个虚拟的Galaxy S2测试。它的工作原理时,家庭或后退按钮pssed $ P $。当最近访问的应用按钮pssed $ P $它不工作。我不知道你的使用情况,但我个人期望的应用继续启动,而我最近浏览的应用程序。
this is my Splash Screen, If I press home or multitasking/appswitch button when Intent is started app crash, in logcat is FATAL EXEPTION: Thread-1277. Can I kill/delete this Intent when player press home button?
public class SplashScreen extends Activity {
private static int loadingTime = 1000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
setContentView(R.layout.loading_screen);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
finish();
}
}, loadingTime);
}
}
The following code tracks whether the SplashActivity
is at least partially showing. If yes, it will continue to MainActivity
. If not (activity is finished by pressing Back button, activity is stopped by pressing Home button) nothing happens.
This solution uses Fragment
s so the timing is preserved across e.g. screen orientation changes (it will always take specified time no matter how many times you rotate your device - the timer will not reset).
public class SplashActivity extends Activity {
// tracks when the activity is at least partially visible (e.g. under a dialog)
private boolean mStarted = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// your current code
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
setContentView(R.layout.activity_startup);
if (savedInstanceState == null) {
// first time onCreate, create fragment which starts countdown
getFragmentManager()
.beginTransaction()
.add(SplashFinishFragment.newInstance(), SplashFinishFragment.TAG)
.commit();
} else {
// fragment already set up from first onCreate after screen rotation
}
}
@Override
protected void onStart() {
// the activity becomes at least partially visible
mStarted = true;
super.onStart();
}
@Override
protected void onStop() {
// the activity is no longer visible
mStarted = false;
super.onStop();
}
public boolean isStarted2() {
// there is already hidden method isStarted() in the framework
// you can't use it and are not allowed to override it
return mStarted;
}
public static class SplashFinishFragment extends Fragment {
private static final String TAG = SplashFinishFragment.class.getSimpleName();
private static final int DELAY = 1000; // one second delay
private static final Handler mHandler = new Handler(); // one main thread anyway
private final Runnable mRunnable = new Runnable() {
@Override
public void run() {
if (getActivity() == null) {
// this should never happen, there is no activity, so no fragment
Log.e(TAG, "No activity!");
return;
}
SplashActivity a = (SplashActivity) getActivity();
if (a.isStarted2() || a.isChangingConfigurations()) {
// if activity is even partially visible or is rotating screen right now, continue
Intent i = new Intent(a, SettingsActivity.class);
a.startActivity(i);
}
// in any case close splash
a.finish();
}
};
public static SplashFinishFragment newInstance() {
return new SplashFinishFragment();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// the countdown will continue (not reset) across screen rotations
setRetainInstance(true);
// try running the main activity after specified time
mHandler.postDelayed(mRunnable, DELAY);
}
@Override
public void onDestroy() {
// if the fragment gets destroyed (e.g. activity closes) do not launch main activity
mHandler.removeCallbacks(mRunnable);
super.onDestroy();
}
}
}
This was tested on a virtual Galaxy S2. It works when Home or Back button is pressed. It doesn't work when Recent Apps button is pressed. I don't know your use case but personally I would expect the app to continue launching while I browse recent apps.
这篇关于杀闪屏上的意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!