-1

I'm trying to make my application use AlarmManager to send a notification after 5 seconds, but the pendingIntent doesn't seem to get called. Heres my code

In onCreate in MainActivity:

Long alertTime = new GregorianCalendar().getTimeInMillis()+5*1000;
    Intent alertIntent = new Intent(this, Notification.class);
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, alertTime , PendingIntent.getBroadcast(this, 1, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));

Notification.class:

public class Notification extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    PendingIntent notificIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);
    NotificationCompat.Builder mBuilder =  new NotificationCompat.Builder(context);
            mBuilder.setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("Title")
            .setTicker("Ticker")
            .setContentText("Text");
    mBuilder.setContentIntent(notificIntent);
    mBuilder.setDefaults(NotificationCompat.DEFAULT_VIBRATE);
    mBuilder.setAutoCancel(true);
    NotificationManager mNotificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(1, mBuilder.build());

}

}

Android Manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wouter.lifetracker"
android:versionCode="1"
android:versionName="1.0" android:installLocation="auto">

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="21" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />

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

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver
        android:name=".Notification"
        >
        </receiver>

</application>

i have searched for hours but i can't find out why nothing is happening

Wolion
  • 1
  • 1

2 Answers2

1

See: https://developer.android.com/reference/android/app/AlarmManager.html#set(int, long, android.app.PendingIntent)

Note: Beginning in API 19, the trigger time passed to this method is treated as inexact: the alarm will not be delivered before this time, but may be deferred and delivered some time later. The OS will use this policy in order to "batch" alarms together across the entire system, minimizing the number of times the device needs to "wake up" and minimizing battery use. In general, alarms scheduled in the near future will not be deferred as long as alarms scheduled far in the future.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
1

only 5 seconds?

try this:

new Handler().postDelayed(new Runnable() {
                public void run() {
                showNotification(); //your notification code called after 5 seconds
                }
            }, 5 * 1000); // 5 seconds...

OR TRY:

long alertTime = System.currentTimeMillis() + (5 * 1000); // <-- try this
Intent alertIntent = new Intent(this, Notification.class);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, alertTime , PendingIntent.getBroadcast(this, 1, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));
Lucas
  • 431
  • 3
  • 7
  • Thanks for your reply Yes it's only 5 seconds so that i can easily check wether it works or not, in the end it's supposed to go off once a day. I tried your suggestion and it still doesn't work. I'm testing this app on my phone so maybe it is just a problem with my phone? – Wolion Jan 26 '16 at 15:55