Thorny84
Thorny84

Reputation: 335

Notification with AlertManager

My intention is to create a simple alarm after 8 second, but I can not talk to the class that extends BroadcastReceiver.
File AndroidManifest:

<receiver android:name=".AlertNotification" />

File first.java:

long time = new GregorianCalendar().getTimeInMillis() + 8 * 1000;
Log.d( "alarm", "started "+ Long.toString( time ) );
Intent NotificationAlert = new Intent( context, AlertNotification.class );
AlarmManager alarmManager = ( AlarmManager ) context.getSystemService( Context.ALARM_SERVICE );
alarmManager.set(
               AlarmManager.ELAPSED_REALTIME_WAKEUP,
               time,
               PendingIntent.getBroadcast( context, 0, NotificationAlert, PendingIntent.FLAG_UPDATE_CURRENT )
);

File AlertNotification.java:

public class AlertNotification extends BroadcastReceiver{

    @Override
    public void onReceive( Context context, Intent intent ){

        PendingIntent pIntent = PendingIntent.getActivity(
                context,
                0,
                new Intent( context, PersonalPage.class ),
                0
        );

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder( context );

        mBuilder.setSmallIcon( R.drawable.shopify )
                .setContentTitle( "title" )
                .setContentText( "date" )
                .setDefaults( NotificationCompat.DEFAULT_SOUND )
                .setContentIntent( pIntent );

        NotificationManager mNotificationManager = ( NotificationManager )
                context.getSystemService( Context.NOTIFICATION_SERVICE );

        Log.d( "alarm", "true" );

        mNotificationManager.notify( 0, mBuilder.build() );

    }
}

The log alarm into first.java file is executed.
Log alarm into AlertNotification.java not.

RESOLVED

Upvotes: 0

Views: 311

Answers (1)

ray an
ray an

Reputation: 1288

Use RTC_WAKEUP instead of ELAPSED_REALTIME_WAKEUP.

If you want to use ELAPSED_REALTIME_WAKEUP use following code

alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
        SystemClock.elapsedRealtime() + 
        8 * 1000, pendingIntent);

SystemClock.elapsedRealtime() returns milliseconds since boot time.

Upvotes: 1

Related Questions