dak
dak

Reputation: 89

AlarmManager awakes Activity from Sleep but the Activity Closes Undesirably

I have an AlarmManager setting an alarm:

Intent intent = new Intent(mContext, AwakeActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent pendingIntent = PendingIntent.getActivity(mContext, PENDING_INTENT_ID, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(mContext.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + futureTime, pendingIntent);

and the alarm works as expected. When the alarm goes off I have AwakeActivity open. This also works as expected. In AwakeActivity I have:

@Override
public void onCreate(Bundle savedInstanceState)
{
    // main
    super.onCreate(savedInstanceState);

    // inflate
    setContentView(R.layout.awake);

    Window window = this.getWindow();
    window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); //relates to IInputConnectionWrapper issue - but not cause of instant close of app
    window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
    window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
    window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}

Here is my Manifest definition:

<activity 
        android:name=".AwakeActivity"
        android:label="@string/activityAwake"
        android:configChanges="keyboardHidden|orientation"
        android:theme="@android:style/Theme.NoTitleBar"
        android:screenOrientation="portrait"
        android:launchMode="singleInstance"
        android:noHistory="true"
    >
    </activity>

The issue I'm having is that when AwakeActivity starts up it doesn't stay open. It instantly closes. But... this only happens if the phone is in sleep mode, if I leave the screen on and AwakeActivity opens via the alarm - all is well.

The other but... is that this happens on my Droid X running 2.3.4 and not my Galaxy Nexus 4.2.2. I've been at this for a while and feel as though I've done things properly.

What am I missing here? Any ideas?

Upvotes: 3

Views: 1072

Answers (1)

joelreeves
joelreeves

Reputation: 1954

I myself was running into a similar issue. On some devices the device would wake up and on others the activity would close. Have you tried using a WakeLock to see if the same thing happens?

So declare a global wakelock variable:

PowerManager.WakeLock wakelock;

In your onCreate method do something like:

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakelock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
                | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_BRIGHT_WAKE_LOCK
                | PowerManager.ON_AFTER_RELEASE, "tag");
wakelock.acquire();

Now you just have to release the wakelock, say in your onPause method:

@Override
protected void onPause() {
    super.onPause();      
    wakelock.release();
}

Also you need to include this in your manifest file:

<uses-permission android:name="android.permission.WAKE_LOCK"/>

Upvotes: 3

Related Questions