Intent .putExtra return null with PendingIntent

Intent .putExtra return null with PendingIntent

此应用程序允许我启动服务以提醒我饮酒。如果我单击通知,我想查看该服务是否为 运行,但我没有使用 Intent.putExtra 方法获得任何输出。

我的 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);
    }
}

我认为可以使用 sharedpreferences 解决这个问题,但这应该不是一个好的解决方案。

编辑:

我的广播接收器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);

}

}

代替 onCreate() 和 putExtra() 中的代码,您可以在每种情况下创建两个挂起的意图,并且每个意图使用不同的广播接收器。在该广播接收器中,您可以打印单独的 toast 消息。

MyBroadcastRecieverStart.java

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

Toast.makeText(this, "Reminder service started.", Toast.LENGTH_SHORT).show();

}
}

MyBroadcastReceiverStop.java

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

Toast.makeText(this, "Reminder service ended.", Toast.LENGTH_SHORT).show();

}
}

SendNotification 方法 -

public void sendNotification(View view) {

if (!isServiceRunning) {
    isServiceRunning = true;

    alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    mainText.setText("Service is running.");
    Intent i = new Intent(this, MyBroadcastReceiverStart.class);


    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;

    mainText.setText("Service paused.");
    Intent i = new Intent(this, MyBroadcastReceiverStop.class);


    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);
    alarmManager.cancel(pendingIntent);
}
}

如果你在 else 块中写 alarmManager.cancel(pendingIntent) ,它会取消一个不存在的意图,因为你已经在 if 块中创建了意图,如果执行了 else 则它不会被创建,所以我也在 else 块中创建了意图。

您正在将 "extra" 添加到传递给 AlarmManagerIntent。此 "extra" 将包含在 onReceive() 中传递给您的 BroadcastReceiver 的 Intent 中,但您不会在那里对其进行任何操作。

当您点击 Notification 时,它将启动 drinkReminder,我希望它是一个 Activity


您在评论中还说:

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?

这是您设置闹钟的代码:

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

当您调用setRepeating()时,第二个参数指定闹钟第一次响起的时间,第三个参数指定重复间隔。您已经告诉警报管理器您希望第一个警报立即触发,然后每 10 分钟触发一次。

如果你想让第一个闹钟在你按下按钮后10分钟触发,你需要调整第二个参数,让它代表未来10分钟的时间,像这样:

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