简体   繁体   English

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

[英]Android activity opens twice when starting from lock screen

Hey guys last weeks I have tried myself with some android development and I'm currently stuck on the following error: Im running a timer with a service in background.嘿伙计们,上周我尝试了一些 android 开发,但我目前遇到了以下错误:我在后台运行带有服务的计时器。 When the time is up an activity shall open, even when the phone is locked.时间一到,活动就会打开,即使手机被锁定。 With the following code, while not locked everything is fine.. But when opening from locked screen it will always open up twice.. :/使用以下代码,虽然没有锁定一切都很好..但是从锁定屏幕打开时,它总是会打开两次..:/

I have added this in the onCreate to make it open from lock screen.我在 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);

And I am opening the activity from the service via an intent.我通过意图从服务中打开活动。

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

and when timer is finished -> startActivity(callEntryActivityIntent);当计时器完成时 -> startActivity(callEntryActivityIntent);

maybe someone has an idea.也许有人有想法。 I'm really really new to android development, started this two weeks ago.我对 android 开发真的很陌生,两周前就开始了。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM