邻近位置的多个兴趣点

时间:2013-07-18 11:08:25

标签: android android-intent

我试图在我的应用中实施近距离警报通知服务,并且我希望在我收到通知的任何位置附近有一个POI(兴趣点)的集合。我不断收到此错误

07-18 12:56:27.069: W/dalvikvm(12518): threadid=1: thread exiting with uncaught exception  (group=0x41ee1930)
07-18 12:56:27.079: E/AndroidRuntime(12518): FATAL EXCEPTION: main
07-18 12:56:27.079: E/AndroidRuntime(12518): java.lang.RuntimeException: Error receiving broadcast Intent { act=com.phlok.proximityalarm.broadcast flg=0x10 (has extras) } in com.phlok.multiplealert.ProximityIntentReceiver@42268478
07-18 12:56:27.079: E/AndroidRuntime(12518):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:768)
07-18 12:56:27.079: E/AndroidRuntime(12518):    at android.os.Handler.handleCallback(Handler.java:725)
07-18 12:56:27.079: E/AndroidRuntime(12518):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-18 12:56:27.079: E/AndroidRuntime(12518):    at android.os.Looper.loop(Looper.java:137)
07-18 12:56:27.079: E/AndroidRuntime(12518):    at android.app.ActivityThread.main(ActivityThread.java:5231)
07-18 12:56:27.079: E/AndroidRuntime(12518):    at java.lang.reflect.Method.invokeNative(Native Method)
07-18 12:56:27.079: E/AndroidRuntime(12518):    at java.lang.reflect.Method.invoke(Method.java:511)
07-18 12:56:27.079: E/AndroidRuntime(12518):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
07-18 12:56:27.079: E/AndroidRuntime(12518):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
07-18 12:56:27.079: E/AndroidRuntime(12518):    at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:106)
07-18 12:56:27.079: E/AndroidRuntime(12518):    at dalvik.system.NativeStart.main(Native Method)
07-18 12:56:27.079: E/AndroidRuntime(12518): Caused by: java.lang.NullPointerException
07-18 12:56:27.079: E/AndroidRuntime(12518):    at com.phlok.multiplealert.ProximityIntentReceiver.onReceive(ProximityIntentReceiver.java:35)
07-18 12:56:27.079: E/AndroidRuntime(12518):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:758)
07-18 12:56:27.079: E/AndroidRuntime(12518):    ... 10 more

这是我的代码:

public class ProximityAlertService extends Service {


private static final String TAG = "ProximityAlertService";
private static final long POINT_RADIUS = 100; // in Meters
private static final long PROX_ALERT_EXPIRATION = -1; // It will never expire
private static final String PROX_ALERT_INTENT = "com.phlok.proximityalarm.broadcast";
ArrayList <LatLonPair> positions;


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Toast.makeText(this,"Start",Toast.LENGTH_LONG).show();

    createPositions();
    registerIntents();
    Log.d(TAG,"adding...");
    return super.onStartCommand(intent, flags, startId);

}


private void setProximityAlert(double  latitude, double longitude, final long eventID, int requestCode) {

    LocationManager locationManager= (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Bundle extras = new Bundle();
    extras.putLong("name", eventID);
    extras.putInt("id", requestCode);
    Intent intent = new Intent(PROX_ALERT_INTENT);
    intent.putExtra(PROX_ALERT_INTENT, extras);
    PendingIntent proximityIntent = PendingIntent.getBroadcast(this, requestCode , intent, PendingIntent.FLAG_CANCEL_CURRENT);
    locationManager.addProximityAlert(
            latitude, // the latitude of the central point of the alert region
            longitude, // the longitude of the central point of the alert region
            POINT_RADIUS, // the radius of the central point of the alert region, in meters
            PROX_ALERT_EXPIRATION, // time for this proximity alert, in milliseconds, or -1 to indicate no expiration 
            proximityIntent // will be used to generate an Intent to fire when entry to or exit from the alert region is detected
            );
    requestCode++; 
    Log.d(TAG,String.valueOf(latitude));
    IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT); 
    registerReceiver(new ProximityIntentReceiver(), filter);
}

private void createPositions() {
    positions = new ArrayList<LatLonPair>();
    positions.add(new LatLonPair(53.4010626,-6.1804462));
    positions.add(new LatLonPair(53.4513078,-6.1537115));
    positions.add(new LatLonPair(53.4265686,-6.1715556));
    Log.d(TAG,"Locations added...");
}

private void registerIntents() {
    for(int i = 0; i < positions.size(); i++) {
        LatLonPair latLon = positions.get(i);
        setProximityAlert(latLon.getLatitude(),latLon.getLongitude(),i+1,i);
        Log.d(TAG,"Intenets Registered");
    }
}

那是BroadcastReceiver Code

public class ProximityIntentReceiver extends BroadcastReceiver {

//  private static final int NOTIFICATION_ID = 1000;


@SuppressWarnings("deprecation")
@Override
public void onReceive(Context context, Intent intent) {

    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);

    Intent notificationIntent = new Intent(context, ProximityAlertService.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
    Notification notification = createNotification();
    notification.setLatestEventInfo(context, "Phlok Alert!", "Omar,You are near" + intent.getBundleExtra("com.phlok.multiplealert.").get("name"), pendingIntent);
    notificationManager.notify( intent.getBundleExtra("com.phlok.multiplealert.").getInt("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;
}

}

非常感谢任何帮助

谢谢

1 个答案:

答案 0 :(得分:0)

检查包名是否在onReceive()方法“com.phloc.multiple alert。”)