2

I'm trying to receive intent even if application isn't running and if screen is off.

Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calSet.getTimeInMillis(), pendingIntent);

AlarmReceiver class:

public class AlarmReceiver extends WakefulBroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("try", "try");
    }
}

Problem: I have Log only if app is running and screen is on. If I turn off the screen, there is no Log, and when I turn it on, the Log appears.

Where am I wrong with my code? Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.enjoyalarm.enjoyalarm">

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

    <application
        android:allowBackup="true"
        android:debuggable="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".AlarmReceiverA"
            android:label="@string/title_activity_alarm_receiver"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="com.enjoyalarm.enjoyalarm.AlarmReceiverA" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <receiver android:name=".AlarmReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
    </application>

</manifest>
Tryam
  • 185
  • 9

1 Answers1

0

The behaviour of AlarmManager.set() has changed since API 19. Android can reschedule alarms so as not to unnecessarily wake the device to conserve battery life. You can target your app for API 18 or lower and Android will wake the device up to trigger your alarm at the correct time.

If you need exact wakeups and you can't target your app for API 18 or lower (because you need features that are only available in API 19 or higher), there are other recommended methods. Look at the online developer documentation for AlarmManager:

Note: Beginning with API 19 (KITKAT) alarm delivery is inexact: the OS will shift alarms in order to minimize wakeups and battery use. There are new APIs to support applications which need strict delivery guarantees; see setWindow(int, long, long, PendingIntent) and setExact(int, long, PendingIntent). Applications whose targetSdkVersion is earlier than API 19 will continue to see the previous behavior in which all alarms are delivered exactly when requested.

David Wasser
  • 85,616
  • 15
  • 182
  • 239