Search code examples
androidbroadcastreceiveralarmmanager

getting null action in the broadcast Receiver


When I trying to call the broadcast receiver using the alarm, getting the null action.

    String action = k2.getAction();

getting action null.

How I am calling the AlarmReceiver:

@SuppressLint("NewApi")
    private void setAlarm(String remCategory,Calendar calNow, Context context, int request_code) {

        init(context);
        Log.i("Inside setAlarm,","Yes");

        Log.i("setAlarm Category",remCategory);
        Intent intent = new Intent(context, AlarmReceiver.class);
        intent.putExtra("Rem_cat",remCategory);
        intent.putExtra("Rem_calling_class",UpdateTables.this.getClass().getSimpleName());

        long alarmtime=calNow.getTimeInMillis();

        System.out.println("Reminder Time is "+calNow.get(Calendar.HOUR_OF_DAY)+" "+calNow.get(Calendar.MINUTE));
        Log.i("Target",Long.valueOf(alarmtime).toString());

        //final int _id = (int) System.currentTimeMillis();
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, request_code, intent, PendingIntent.FLAG_ONE_SHOT);

        int currentapiVersion = android.os.Build.VERSION.SDK_INT;
        if (currentapiVersion < android.os.Build.VERSION_CODES.KITKAT){
            alarmManager.set(AlarmManager.RTC_WAKEUP, alarmtime, pendingIntent);
        } else {
            if (currentapiVersion < android.os.Build.VERSION_CODES.M) {
                alarmManager.setExact(AlarmManager.RTC_WAKEUP, alarmtime, pendingIntent);
            } else {
                alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, alarmtime, pendingIntent);
            }
        }
    }

Alarm Receiver

public class AlarmReceiver extends BroadcastReceiver {

    public  String TAG=AlarmReceiver.this.getClass().getSimpleName();
    @Override
    public void onReceive(Context k1, Intent k2) {
        // TODO Auto-generated method stub

        Log.i("Alarm Received","Yes");

        String action = k2.getAction();

        String reminder_cat=k2.getExtras().getString("Rem_CAT");
        String calling_class=k2.getExtras().getString("Rem_calling_class");

        Calendar calendar=Calendar.getInstance();

        System.out.println(TAG+ calendar.getTime());
        System.out.println(TAG+" calling class "+calling_class);
        Intent i=new Intent(k1,AlertDialogActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        i.putExtra("Rem_CAT",reminder_cat);

        Log.d(TAG,reminder_cat);
        k1.startActivity(i);
    }

}

Manifest:

 <receiver android:name=".AlarmReceiver"
            android:enabled="true"
            android:process=":remote">

            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>

        </receiver>

Solution

  • In your AlarmReceiver, the key values being retrieved are not the same.

    Change this line:

    String reminder_cat=k2.getExtras().getString("Rem_CAT");
    

    to this:

    String reminder_cat=k2.getExtras().getString("Rem_cat");
    

    This should let you get the correct intent extras and will allow the receiver to not fail.