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

时间:2020-03-05 18:26:05

标签: java android android-intent android-service lockscreen

伙计们,上周我已经尝试了一些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);

也许有人有主意。我真的是Android开发的新手,这是两周前开始的。

1 个答案:

答案 0 :(得分:0)

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