1

I have set up a notification which will be shown 4 hours after the App being destroyed.

But as I am destroying the App, the notification is coming immediately and also after 4 hours.

Here's my Main Activity's onDestroy():

@Override
    public void onDestroy() {
        super.onDestroy();


        Intent myIntent = new Intent(this, AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, myIntent,0);

        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
        //alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 3600*1000*4, 3600*1000*4, pendingIntent);
    }

and here's my AlarmReceiver.class:

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent mainIntent = new Intent(context, Main.class);

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

        android.app.Notification noti = new NotificationCompat.Builder(context)
            .setAutoCancel(true)
            .setContentIntent(PendingIntent.getActivity(context, 0, mainIntent,
                              PendingIntent.FLAG_UPDATE_CURRENT))
            .setContentTitle("Title")
            .setContentText("It's been so Long!!!")
            .setSubText("Please return back to App & Learn more Duas.")
            .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
            .setVibrate(new long[] {700,700,700,700})
            .setSmallIcon(R.drawable.ic_launcher)
            .setTicker("Important Notification")
            .setWhen(System.currentTimeMillis())
            .build();

        notificationManager.notify(0, noti);
    }

}

I want that Notification should be shown after every 4 hours & not immediately of App being destroyed.

Any help would be highly Appreciated.

Thanks in Advance.

Hammad Nasir
  • 153
  • 1
  • 10

1 Answers1

1

It is because of small mistake

just change this line

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 3600*1000*4, 3600*1000*4, pendingIntent);

TO this

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, (System.currentTimeMillis()+(3600*1000*4)), 3600*1000*4, pendingIntent);
Biraj Zalavadia
  • 28,348
  • 10
  • 61
  • 77
  • Please can you help with this one also: [link]http://stackoverflow.com/questions/24971487/how-to-make-things-done-on-preferences-being-changed – Hammad Nasir Jul 27 '14 at 08:54