BroadcastReceiver中的Android PendingIntent的onreceive为null

时间:2012-10-03 15:09:26

标签: android broadcastreceiver android-pendingintent

我有一个应用程序,我打电话给sendBroadcast()。我的接收器类如下:

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.location.LocationManager;
import android.util.Log;

public class ProximityIntentReceiver extends BroadcastReceiver {

    private static final String TAG = ProximityIntentReceiver.class
            .getSimpleName();
    private static final int NOTIFICATION_ID = 1000;

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.e(TAG, "inside prox onreceive");
        String key = LocationManager.KEY_PROXIMITY_ENTERING;
        Boolean entering = intent.getBooleanExtra(key, false);
        if (entering) {
            Log.d(getClass().getSimpleName(), "entering");
        } else {
            Log.d(getClass().getSimpleName(), "exiting");
        }
        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
                null, 0);
        Notification notification = createNotification();
        notification.setLatestEventInfo(context, "Proximity Alert!",
                "You are near your point of interest.", pendingIntent);
        notificationManager.notify(NOTIFICATION_ID, notification);
    }

    private Notification createNotification() {
        Notification notification = new Notification();
        notification.icon = R.drawable.ic_launcher;
        notification.when = System.currentTimeMillis();
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.flags |= Notification.FLAG_SHOW_LIGHTS;
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notification.defaults |= Notification.DEFAULT_LIGHTS;
        notification.ledARGB = Color.WHITE;
        notification.ledOnMS = 1500;
        notification.ledOffMS = 1500;
        return notification;
    }
}

我在下一行有一个NullPointerException

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, null, 0);

主叫服务如下。

import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

public class LocationService extends Service {

    private static final String TAG = LocationService.class.getSimpleName();
    LocationManager mlocManager;
    LocationListener mlocListener;
    NfcScannerApplication nfcscannerapplication;
    private static final String PROX_ALERT_INTENT = "com.carefreegroup.ProximityAlert";
    Intent intent;
    PendingIntent proximityIntent;
    ProximityIntentReceiver pir;

    @Override
    public void onCreate() {
        nfcscannerapplication = (NfcScannerApplication) getApplication();
        mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        mlocListener = new MyLocationListener();
        pir = new ProximityIntentReceiver();
        intent = new Intent(PROX_ALERT_INTENT);
        proximityIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
        Log.e(TAG, "Service created and location manager and listener created");
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        mlocManager.removeUpdates(mlocListener);
        unregisterReceiver(pir);
        Log.e(TAG, "Service destroyed");
        super.onDestroy();
    }

    @Override
    public void onStart(Intent intent, int startId) {
        mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
                mlocListener);
        IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT);
        registerReceiver(pir, filter);
        Log.e(TAG, "requesting location updates");
        super.onStart(intent, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;// TODO Auto-generated method stub
    }

    private class MyLocationListener implements LocationListener {

        @Override
        public void onLocationChanged(Location loc) {
            Log.e(TAG, "about to set geopoints in application object");
            nfcscannerapplication.setLat(loc.getLatitude());
            nfcscannerapplication.setLon(loc.getLongitude());
            fireLocationChangeEvent(loc.getLongitude(), loc.getLatitude());
            mlocManager.addProximityAlert(53.653480529785156,
                    -1.51961088180542, 2, -1, proximityIntent);
            sendBroadcast(intent);
        }

        @Override
        public void onProviderDisabled(String provider) {}

        @Override
        public void onProviderEnabled(String provider) {}

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {}
    }// end of MyLocationListener

    private void fireLocationChangeEvent(double lon, double lal) {
        LocalBroadcastManager.getInstance(getApplicationContext())
                .sendBroadcast(new LocationChangeIntent(lon, lal));
    }
}// end of service

为什么我在这条线上获得NullPointerException

1 个答案:

答案 0 :(得分:2)

由于您尚未创建Intent对象并在getActivity方法中放置null,因此抛出NullPointerException。我希望你想在Intent Action触发BroadcastReceiver时调用LocationService。如果是这样,那么你需要创建调用服务的Intent,如下所示,然后将Intent对象放在Pending intent中。

Intent i = new Intent(context,LocationService.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, i, 0);