简体   繁体   中英

Can not fetch intent data in service set in broadcastreceiver

I am a newbie into android development. I am developing a simple application which contains 1 activity, 1 BroadcastReceiver and 1 Notification service. Some alarms are set in activity, I want to re-set them when the device is booted/restarted.

Here is my code in MainActivity :

            //randomDates is an (Long) array of time in milliseconds 
            int len = randomDates.length;

            AlarmManager[] alarmManager = new AlarmManager[len];
            ArrayList<PendingIntent> intentArray = new ArrayList();
            String type = "A";
            Long t =  new Date().getTime();
            for (int i = 0; i < len; i++) {
                if(t <= randomDates[i]) {
                    Intent myIntent = new Intent(MainActivity.this, Receiver.class);
                    if (i%2 == 1) {
                        type = "A";
                    } else {
                        type = "P";
                    }
                    myIntent.putExtra("type", type);
                    myIntent.putExtra("time", randomDates[i]);
                    PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, i, myIntent, PendingIntent.FLAG_ONE_SHOT);
                    alarmManager[i] = (AlarmManager) getSystemService(ALARM_SERVICE);
                    alarmManager[i].set(AlarmManager.RTC, randomDates[i], pendingIntent);
                    intentArray.add(pendingIntent);
                }
            }

            sharedPref = getSharedPreferences("MyCalApp",Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPref.edit();
            editor.putBoolean("areAlarmsAlreadySet", false);
            editor.putString("alarmDates",Arrays.toString(randomDates));
            editor.commit();

Alarms are set in activity. And Receiver is registered in Manifest file to receive events. onReceive function in Receiver is as below :

public void onReceive(Context context, Intent intent) {
        if(intent.getAction() != null && intent.getAction().equalsIgnoreCase("android.intent.action.BOOT_COMPLETED")) {
                Long[] randomDates1 = //get from shared Pref;

                int len = randomDates1.length;
                AlarmManager[] alarmManager = new AlarmManager[len];
                ArrayList<PendingIntent> intentArray = new ArrayList();
                String type = "A";
                Long t = new Date().getTime();
                for (int i = 0; i < len; i++) {
                    if (t <= randomDates1[i]) {
                        Intent myIntent = new Intent(context.getApplicationContext(), NotificationService.class);
                        if (i % 2 == 1) {
                            type = "A";
                        } else {
                            type = "P";
                        }
                        myIntent.putExtra("type", type);
                        myIntent.putExtra("time", randomDates1[i]);
                        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, i, myIntent, PendingIntent.FLAG_ONE_SHOT);
                        alarmManager[i] = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
                        alarmManager[i].set(AlarmManager.RTC, randomDates1[i], pendingIntent);
                        intentArray.add(pendingIntent);
                    }
                }
            }
        }
        intent.setClass (context, NotificationService.class);
        context.startService(intent);
    }

And in Notification Service I fetch data from intent as follows:

    Bundle b = intent.getExtras();
    String type = b.getString("type");    // This is line number 38.
    Long time = intent.getLongExtra("time",1l);

I get all notifications correctly from alarms set in activity. But get NullpointerException

Caused by: java.lang.NullPointerException at com.example.android.basicnotifications.NotificationService.onStart(NotificationService.java:38)

This is where I read extras from Intent. What am I missing in this? Any help is appreciated.

PS All necessary permissions are set in manifest file.

At the end of onReceive() you do this:

    intent.setClass (context, NotificationService.class);
    context.startService(intent);

This starts your Service with an Intent that contains no "extras". That causes the NullPointerException.


Also, in onReceive() you have this code:

Intent myIntent = new Intent(context.getApplicationContext(), NotificationService.class);
...
myIntent.putExtra("type", type);
myIntent.putExtra("time", randomDates1[i]);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, i, myIntent, PendingIntent.FLAG_ONE_SHOT);

This also can't work. When you call PendingIntent.getBroadcast() this generates an Intent that will be sent as a broadcast Intent . However, you are passing it myIntent , which is for a Service , not a BroadcastReceiver .

You need to pay more attention to what you are doing.

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