为什么我的BroadcastReceiver的onReceive中的intent.getAction()返回null?

时间:2019-06-26 10:03:41

标签: android android-intent broadcastreceiver intentfilter

我想在特定的时钟时间触发BroadcastReceiver的onReceive函数。触发事件时,创建IntentFilter和/或注册BroadcastReceiver的片段不存在。这可能是onReceive方法中的intent.getAction返回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

1 个答案:

答案 0 :(得分:1)

您需要在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);

操作用于区分要执行的操作,以防您要在一个BroadcastReceiver中执行不同的任务。