为什么我的 BroadcastReceiver returns 的 onReceive 中的 intent.getAction() 为空?
Why does intent.getAction() in onReceive of my BroadcastReceiver returns null?
我想在特定时钟时间触发 BroadcastReceiver 的 onReceive 函数。创建 IntentFilter and/or 的片段在事件触发时注册 BroadcastReceiver 不存在。这可能是 onReceive 方法中 intent.getAction 为 return null 的原因吗?我的代码有什么错误?使用 BroadcastReceiver 的 onReceive 方法的 intent 参数的正确方法是什么?
当我显式调用 sendBroadcast 方法时,一切正常。但这会立即执行 onReceive。我想让它在指定的时间执行。
AddEditReminderFragment.java
public class AddEditReminderFragment extends Fragment {
...
public static final String NOTIFICATION_INTENT_ACTION = BuildConfig.APPLICATION_ID + ".NOTIFICATION_INTENT_ACTION";
private AlarmManager alarmManager;
private AlarmReceiver alarmReceiver = new AlarmReceiver();
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
...
alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
...
}
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
...
saveReminder();
...
}
private void saveReminder() {
...
Intent alarmIntent = new Intent(getActivity(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, alarmIntent, 0);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
alarmManager.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, alarmDateTime.getTimeInMillis(), pendingIntent);
} else {
alarmManager.set(AlarmManager.RTC_WAKEUP, alarmDateTime.getTimeInMillis(), pendingIntent);
}
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(alarmReceiver, new IntentFilter(NOTIFICATION_INTENT_ACTION));
getFragmentManager().popBackStack();
}
public void onDestroyView() {
super.onDestroyView();
LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(alarmReceiver);
}
...
}
AlarmReceiver.java
public class AlarmReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.i("rahul", "Broadcast received: " + action);
if (action.equals(AddEditReminderFragment.NOTIFICATION_INTENT_ACTION)) {
sendNotification(context, intent.getStringExtra("notificationTitle"));
}
}
...
}
AndroidManifest.xml
...
<receiver android:name=".receivers.AlarmReceiver"
android:enabled="true">
<intent-filter>
<action android:name="com.example.todo.NOTIFICATION_INTENT_ACTION" />
</intent-filter>
</receiver>
...
预期 Logcat 输出显示意图操作,但实际输出如下:
Logcat 按搜索字符串过滤的详细输出 "rahul"
06-26 11:47:56.011 4321-4321/com.example.todo I/rahul: Broadcast received: null
您需要在 Intent
上设置用于创建 PendingIntent
的操作。
Intent alarmIntent = new Intent(getActivity(), AlarmReceiver.class);
alarmIntent.setAction("com.example.todo.NOTIFICATION_INTENT_ACTION");
PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, alarmIntent, 0);
Action 用于区分要执行的操作,以防您希望在单个任务中执行不同的任务BroadcastReceiver
。
我想在特定时钟时间触发 BroadcastReceiver 的 onReceive 函数。创建 IntentFilter and/or 的片段在事件触发时注册 BroadcastReceiver 不存在。这可能是 onReceive 方法中 intent.getAction 为 return null 的原因吗?我的代码有什么错误?使用 BroadcastReceiver 的 onReceive 方法的 intent 参数的正确方法是什么?
当我显式调用 sendBroadcast 方法时,一切正常。但这会立即执行 onReceive。我想让它在指定的时间执行。
AddEditReminderFragment.java
public class AddEditReminderFragment extends Fragment {
...
public static final String NOTIFICATION_INTENT_ACTION = BuildConfig.APPLICATION_ID + ".NOTIFICATION_INTENT_ACTION";
private AlarmManager alarmManager;
private AlarmReceiver alarmReceiver = new AlarmReceiver();
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
...
alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
...
}
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
...
saveReminder();
...
}
private void saveReminder() {
...
Intent alarmIntent = new Intent(getActivity(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, alarmIntent, 0);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
alarmManager.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, alarmDateTime.getTimeInMillis(), pendingIntent);
} else {
alarmManager.set(AlarmManager.RTC_WAKEUP, alarmDateTime.getTimeInMillis(), pendingIntent);
}
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(alarmReceiver, new IntentFilter(NOTIFICATION_INTENT_ACTION));
getFragmentManager().popBackStack();
}
public void onDestroyView() {
super.onDestroyView();
LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(alarmReceiver);
}
...
}
AlarmReceiver.java
public class AlarmReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.i("rahul", "Broadcast received: " + action);
if (action.equals(AddEditReminderFragment.NOTIFICATION_INTENT_ACTION)) {
sendNotification(context, intent.getStringExtra("notificationTitle"));
}
}
...
}
AndroidManifest.xml
...
<receiver android:name=".receivers.AlarmReceiver"
android:enabled="true">
<intent-filter>
<action android:name="com.example.todo.NOTIFICATION_INTENT_ACTION" />
</intent-filter>
</receiver>
...
预期 Logcat 输出显示意图操作,但实际输出如下:
Logcat 按搜索字符串过滤的详细输出 "rahul"
06-26 11:47:56.011 4321-4321/com.example.todo I/rahul: Broadcast received: null
您需要在 Intent
上设置用于创建 PendingIntent
的操作。
Intent alarmIntent = new Intent(getActivity(), AlarmReceiver.class);
alarmIntent.setAction("com.example.todo.NOTIFICATION_INTENT_ACTION");
PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, alarmIntent, 0);
Action 用于区分要执行的操作,以防您希望在单个任务中执行不同的任务BroadcastReceiver
。