Search code examples
javaandroidandroid-intentandroid-pendingintent

Intent .putExtra return null with PendingIntent


This application allows me to start a service to remind me drinking. If i click the notification i want to see if the service is running or not, but i don't get any output with the Intent.putExtra method.

My MainActivity class:

Boolean isServiceRunning = false;
AlarmManager alarmManager;
PendingIntent pendingIntent;
TextView mainText;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mainText = findViewById(R.id.mainTextView);

    Bundle extras = getIntent().getExtras();
    if (extras == null) {
            mainText.setText("Service is paused");
    } else {
        mainText.setText("Service is running");
    }

}

public void sendNotification(View view) {

    if (!isServiceRunning) {
        isServiceRunning = true;
        Toast.makeText(this, "Reminder service started.", Toast.LENGTH_SHORT).show();
        alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

        mainText.setText("Service is running.");
        Intent i = new Intent(this, reciever.class);
        i.putExtra("isServiceRunning", isServiceRunning.booleanValue());

        pendingIntent = PendingIntent.getBroadcast(this, 100, i, PendingIntent.FLAG_UPDATE_CURRENT);

        Calendar calendar = Calendar.getInstance();
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * 10, pendingIntent);
    } else {
        isServiceRunning = false;
        Toast.makeText(this, "Reminder service ended.", Toast.LENGTH_SHORT).show();
        mainText.setText("Service paused.");
        alarmManager.cancel(pendingIntent);
    }
}

I think its possible to solve this problem with sharedpreferences, but this shouldn't be a good resolution.

EDIT:

My broadcast reciever class:

public class reciever BroadcastReceiver {

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

    Intent notificationIntent = new Intent(context, drinkReminder.class);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(drinkReminder.class);
    stackBuilder.addNextIntent(notificationIntent);

    PendingIntent pendingIntent = stackBuilder.getPendingIntent(100, PendingIntent.FLAG_UPDATE_CURRENT);


    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
    Notification notification = new NotificationCompat.Builder(context, CHANNEL_1_ID)
            .setContentTitle("Title")
            .setContentText("description")
            .setSmallIcon(R.drawable.icon_waterdrop)
            .setTimeoutAfter(30000)
            .setContentIntent(pendingIntent)
            .setAutoCancel(true)
            .build();

    notificationManager.notify(0, notification);

}

}


Solution

  • You are adding the "extra" to the Intent that you pass to AlarmManager. This "extra" will be in the Intent that gets delivered to your BroadcastReceiver in onReceive(), but you don't do anything with it there.

    When you click on the Notification, it will launch drinkReminder, which I hope is an Activity.


    You also said in a comment:

    Could you tell me please one more thing. Sometimes if i press the button to start the service, a notification appers after about 10 seconds, after this it shows all 10 minutes like it should. How can i fix this?

    Here is your code for setting the alarm:

    Calendar calendar = Calendar.getInstance();
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 
        calendar.getTimeInMillis(), 1000 * 60 * 10, pendingIntent);
    

    When you call setRepeating(), the 2nd parameter specifies the first time the alarm should go off, and the 3rd parameter specifies the repeat interval. You've told alarm manager that you want the first alarm to trigger immediately and then every 10 minutes after that.

    If you want the first alarm to trigger 10 minutes after you press the button, you need to adjust the second parameter so it represents a time that is 10 minutes in the future, like this:

    Calendar calendar = Calendar.getInstance();
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 
        calendar.getTimeInMillis() + (1000 * 60 * 10), 1000 * 60 * 10, pendingIntent);