简体   繁体   中英

Why does intent.getAction() in onReceive of my BroadcastReceiver returns null?

I want to trigger onReceive function of BroadcastReceiver at a particular clock time. The fragment that creates the IntentFilter and/or registers the BroadcastReceiver does not exist when the event is triggered. Can this be a reason for intent.getAction in onReceive method to return null? What is the mistake in my code? What is the correct way to make use of intent parameter of onReceive method of a BroadcastReceiver?

When I explicitly call sendBroadcast method, everything works fine. But that executes onReceive immediately. I want it to execute at a specified time.

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>
    ...

Expected the Logcat output to show the intent action but the actual output is as below:

Logcat Verbose output filtered by search string "rahul"

06-26 11:47:56.011 4321-4321/com.example.todo I/rahul: Broadcast received: null

You need to set the action on Intent which you are using to create 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 is used to differentiate the action to be taken , in case You want to perform different tasks in a Single BroadcastReceiver .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM