2013-07-18 31 views
2

我正在嘗試在我的應用中實現鄰近提醒通知服務,並且當我在附近的任何位置收到通知時,我想擁有一個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 [email protected] 
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"); 
    } 
} 

這是廣播接收器代碼

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

}

任何幫助將非常感激

謝謝

+0

請張貼整個堆棧跟蹤。 –

+0

完成,謝謝你的興趣 – Mero

+0

''NullPointerException'在'ProximityIntentReceiver.java:35'(這是35行?) –

回答

0

檢查包名稱是onRecieve()方法 「com.phlok.multiplealert。」)

+0

我知道什麼問題我得到一個空值因爲我嘗試訪問Bundle上的元素,但由於某種原因它返回null。我不知道爲什麼! – Mero