0

I want to make something like alarm activity, but it only triggered over certain push notification. I've been pulling my hair to achieve that, combining answer here and there(and many more that i couldn't refer here). Now I already can show the activity over the lock screen, but i'm stuck on getting the onClick event listener to dismiss the alarm.

So far here's my codes:

it's onCreate of my Activity:

 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"));

the Activity on manifest:

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

and it's where i call my Activity from my notificationService

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

am I did it wrong?

I only test it on my phone though (4.4.2), so i don't even know the activity will shown on other device. But really, can someone help me to get over this please?

CimCim
  • 1
  • 1
  • Does this show up in the log when you click the button? Log.w("button", "Clicked") – HomeIsWhereThePcIs Oct 11 '17 at 21:55
  • I believe it is in your windowmanager flags. If I recall, the WindowManager.LayoutParams.TYPE_SYSTEM_ALERT will not allow you to receive those onTouch events. If might be worth trying WindowManager.LayoutParams.TYPE_PHONE instead. Per the documentation https://developer.android.com/reference/android/view/WindowManager.LayoutParams.html you should use WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY Edit: Reference https://stackoverflow.com/a/9101977/2104990 – miversen33 Oct 11 '17 at 21:56
  • @Xenolion Yes, a service may start an Activity at any time. – Gabe Sechan Oct 11 '17 at 22:11
  • Ooooh I did not know @GabeSechan – Xenolion Oct 11 '17 at 22:14
  • @HomeIsWhereThePcIs nope, it's not showing anything – CimCim Oct 12 '17 at 19:37
  • @T-he-game yeah i know i should use WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY instead, but my phone is kitkat, as far as i know that flag is newer flag only for android O if im not mistaken. but for the TYPE_PHONE ill try it first – CimCim Oct 12 '17 at 19:37
  • @T-he-game nope, the onClick still not detected, but thanks for the suggestion – CimCim Oct 12 '17 at 19:46

1 Answers1

0

thank you for the suggestion guys, my bad i haven't search the related topic thoroughly, so i found the answer over here:

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

so i just add

     WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH

to my flags above, and voila i get the click event.

CimCim
  • 1
  • 1