如何从另一个广播接收器发送接收器?



我想保留我设置的通知时间,即使在BOOT_COMPLEDTED之后。我只是DeviceBootReceiver通知BOOT_COMPLETED,ReminderReceiver通知用户通知。我的代码是这样的。

设备底部接收器

public class DeviceBootReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
Toast.makeText(context, "BOOT_COMPLETED", Toast.LENGTH_SHORT).show();
SharedPreferences sharedPreferences = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
Calendar now = Calendar.getInstance();
Calendar reminder = new GregorianCalendar();
reminder.setTimeInMillis(sharedPreferences.getLong("reminder", Calendar.getInstance().getTimeInMillis()));
if (now.after(reminder)) {
reminder.add(Calendar.DATE, 1);
}
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
reminder.getTimeInMillis(),
AlarmManager.INTERVAL_DAY,
PendingIntent.getBroadcast(context, 0, new Intent(context, ReminderReceiver.class), 0));

}
}
}
}

提醒接收器

public class ReminderReceiver extends BroadcastReceiver {
private static final String REMINDER_CHANNEL = "reminder";
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "ReminderReceiver", Toast.LENGTH_SHORT).show();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(REMINDER_CHANNEL, "channel name", NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(channel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, REMINDER_CHANNEL);
PendingIntent mainIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);
builder.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Reminder")
.setContentText("This is time for you")
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setContentIntent(mainIntent)
.setPriority(NotificationCompat.PRIORITY_MAX);
notificationManager.notify(2, builder.build());
Calendar reminder = Calendar.getInstance();
reminder.add(Calendar.DATE, 1);
SharedPreferences.Editor editor = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE).edit();
editor.putLong("reminder", reminder.getTimeInMillis());
editor.apply();
}
}

DeviceBottReceiver,当我启动手机时,吐司消息"BOOT_COMPLETED">会很好地出现。但在ReminderReceiver,没有出来吐司消息"提醒接收器"。移动重启时,如何将广播接收器发送到另一个广播接收器?

我正在使用您的代码并且它正常工作。 要调试,请使用此 cmd

adb shell am broadcast -a android.intent.action.BOOT_COMPLETED -p  yourpackagename

在清单中:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name=".DeviceBootReceiver"
android:enabled="true"
android:exported="true"
android:label="BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
<receiver android:name=".ReminderReceiver"/>

相关内容

  • 没有找到相关文章

最新更新