0

I use the ALARM_SERVICE with the following BroadcastReceiver, in oreder to start my OnReceiveActivity:

public class AlarmReciever extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        try {

            Intent i = new Intent();
            i.setClassName("com.test", "co.test.OnReceiveActivity");
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );

            i.addFlags(
                    WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED +
                    WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                    WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON +
                    WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                     );

            context.startActivity(i);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

When I try it without any of the specified WindowManager.LayoutParams flags (while the device is awake and unlocked) - everything works as expected, meaning onReceive() is called, and starts OnReceiveActivity successfully. however, when the flags are present, it doesn't work, neither when the device is asleep and looked, nor when its awake and unlocked.

The following permission were specified at the manifest file:

<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
nobatlinux
  • 347
  • 2
  • 10

2 Answers2

3

Those are not Intent flags and cannot be used with addFlags() on Intent.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • It seemed to work for [this](http://stackoverflow.com/questions/20113161/start-activity-screen-even-if-screen-is-locked-in-android) guy.. In any case, how could I add them? @commonsware – nobatlinux Mar 11 '15 at 12:23
  • @nobatlinux: In the activity being started, pass those flags to `getWindow().addFlags()`. – CommonsWare Mar 11 '15 at 12:41
  • yep just got it :) added an answer for future seekers. – nobatlinux Mar 11 '15 at 12:47
0

As @CommonsWare commented, WindowManager.LayoutParams flags are not Intent flags.

The right way to it is within the onCreate() of OnReceiveActivity, using getWindow() function as follow:

    getWindow().addFlags(
            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED +
            WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD +
            WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON +
            WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
    );

The way I did it before was used in many examples that I've seen, which makes it quite odd.

nobatlinux
  • 347
  • 2
  • 10
  • @notbatlinux Instead of writing new answer you should try to edit the answer from which you got hint of the solution and accept that answer. – VishalKale Aug 17 '16 at 10:49