0

I want to tell the user he has something to do every 5 minutes. I receive a notification every 5 minutes. But my problem is that I don't know where I can stop it. (I want to stop it when the user has click on the notification).

Here is my code in my Activity :

            Calendar cal = Calendar.getInstance();
        cal.add(Calendar.HOUR, heure);
        cal.add(Calendar.MINUTE, minute);

        Intent intent = new Intent(this, MyBroadcastReceiver.class);

        PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager am = (AlarmManager) this
                .getSystemService(Context.ALARM_SERVICE);

        am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), minute*60*1000+heure*3600*1000,  sender);

and here is my code for my BroadcastReceiver :

public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

    Log.d("BroadcastReceiver", "debut receive");

    Intent resultIntent = new Intent(context,
            PiecesPasEnvoyesActivity.class);

    resultIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent resultPendingIntent = PendingIntent.getActivity(context,
            0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    Notification.Builder mBuilder = new Notification.Builder(context)
    .setSmallIcon(R.drawable.courier_blanc)
    .setContentTitle("Messages à envoyer")
    .setContentText("Vous avez des messages à envoyer");

    mBuilder.setAutoCancel(true);
    mBuilder.setPriority(Notification.PRIORITY_MAX);
    mBuilder.setContentIntent(resultPendingIntent);

    int mNotificationId = 001;

    NotificationManager mNotifyMgr = (NotificationManager) context
            .getSystemService(Application.NOTIFICATION_SERVICE);


    mNotifyMgr.notify(mNotificationId, mBuilder.build());

    }
odiiil
  • 266
  • 1
  • 2
  • 9

1 Answers1

1

In your notification add a pending intent and have this intent stop the receiver.

    Intent notificationIntent = new Intent(this, ActivityToStopReciever.class);

    notificationIntent.setFlags(Notification.FLAG_AUTO_CANCEL);
    PendingIntent intent = PendingIntent.getActivity(getApplicationContext(), 0,
                    notificationIntent, 0);

Notification.Builder mBuilder = new Notification.Builder(context)
    .setSmallIcon(R.drawable.courier_blanc)
    .setContentTitle("Messages à envoyer")
    .setContentIntent(intent)
    .setContentText("Vous avez des messages à envoyer");

In the ActivityToStopReciever.class take a look at this link to cancel the notification

http://developer.android.com/reference/android/app/NotificationManager.html#cancel(int

and look here for removing the alarm

Android AlarmManager problem with setting & resetting an alarm

Community
  • 1
  • 1
CoMag
  • 36
  • 7