Android,活动已经锁定屏幕,但不会得到任何onCLick事件

问题描述:

我想做类似警报活动,但它只是通过某些推送通知触发。 我一直在拉我的头发来实现这一点,结合回答herethere(还有更多,我不能在这里提到)。现在我已经可以在锁定屏幕上显示活动,但是我一直在让onClick事件监听器关闭闹钟。Android,活动已经锁定屏幕,但不会得到任何onCLick事件

到目前为止,这里是我的代码:

这是我活动的onCreate:

Window window = getWindow(); 
    window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD 
      | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON 
      | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON 
      | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED 
      | WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); 
    setContentView(R.layout.alarm); 
    ButterKnife.bind(this); 
    PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE); 
    PowerManager.WakeLock wakeLock = pm.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "Alarm"); 
    wakeLock.acquire(); 
    dismissButton.setOnClickListener(v -> Log.w("button", "Clicked")); 

在清单中的活动:

<activity android:name=".{package}.Alarm" 
     android:showOnLockScreen="true" 
     android:theme="@style/AppTheme.WhiteAccent" 
     android:screenOrientation="sensorPortrait"/> 

,并在那里我打电话给我的活动从我notificationService

Intent intent = new Intent(this, MewsAlarm.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     intent.putExtra("judul", data.get("title")); 
     startActivity(intent); 

我做错了吗?

我只在我的手机上测试它(4.4.2),所以我甚至不知道该活动将显示在其他设备上。但是,真的,有人可以帮我解决这个问题吗?

+0

当您点击按钮时会在日志中显示吗? Log.w(“按钮”,“点击”) – HomeIsWhereThePcIs

+0

我相信这是在你的windowmanager标志。如果我记得,WindowManager.LayoutParams.TYPE_SYSTEM_ALERT将不允许您接收这些onTouch事件。如果可能值得尝试使用WindowManager.LayoutParams.TYPE_PHONE。 根据文档https://developer.android.com/reference/android/view/WindowManager.LayoutParams.html,您应该使用WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY 编辑:参考https://*.com/a/9101977/ 2104990 –

+0

@ Xenolion是的,服务可以随时启动一项活动。 –

感谢你的建议的家伙,我的坏我还没有彻底搜索有关的话题,所以我发现在这里的答案:

https://*.com/a/4482868/7852331

,所以我只需要添加

 WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH 

以上我的国旗,瞧,我得到了点击事件。