从锁定屏幕启动时,Android活动会打开两次



伙计们,上周我尝试了一些android开发,目前我遇到了以下错误:我正在后台运行一个带有服务的计时器。时间到了,即使手机锁定,活动也会打开。有了以下代码,虽然没有锁定,但一切都很好。但当从锁定的屏幕打开时,它总是会打开两次。:/

我在onCreate中添加了这个,使其从锁定屏幕打开。

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//allow window to be popped up while in lock screen
Window window = this.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
setContentView(R.layout.activity_entry);

我通过一个意向从服务中打开活动。

callEntryActivityIntent = new Intent(this, EntryActivity.class);
callEntryActivityIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
pendingIntent = PendingIntent.getActivity(this, 0, callEntryActivityIntent, 0);

并且当定时器结束时->startActivity(callEntryActivityIntent(;

也许有人有个主意。我对安卓系统的开发真的很陌生,两周前就开始了。

I am new too, but I guess **android activity cycle** has an answer:
+ The user opens an activity.
- onCreated() is called
- onStart() is called
- onResume() is called  
+ The user LOCKS the device 
- onPause() is called
- onDestroy() is called
- onCreate() is called
- onStart() is called
- onResume() is called 
- onPause() is called   
+ The user UNLOCKS the device
- onResume() is called
- onDestroy() is called
- onCreate() is called
- onStart() is called
- onResume() is called.
Hope this helps

最新更新