Question

I'm writing an app in which I launch a proximity alert every time I'm near a specific point of interest(I read the poi from a mysql db). When the BroadcastReceiver gets the intent, it creates a notification and everything works fine. When I click on the notification I would like an activity to start, so to do this I need to send some parameters (basically strings) to the broadcast receiver, so that it could pass this parameters to the activity I want to start. The problem is that when I try to pass these parameters through the intent to the broadcast receiver I get this error: error receiving broadcast intent flg=0x10 has extras.

I sent the parameters in this way:

 private void addProximityAlert(Annuncio a){
   double latitudine = a.lat; 
   double longitudine = a.lon; 
   Intent intent = new Intent(PROX_ALERT_INTENT);
   intent.putExtra("nome", nome); 
   PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent,0);
   locationManager.addProximityAlert(latitudine, longitudine, RAGGIO, ALERT_EXPIRATION, pi); 
   IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT); 
   registerReceiver(new ProximityIntentReceiver(),filter);
}

this is the receiver:

public class ProximityIntentReceiver extends BroadcastReceiver{  
   public void onReceive(Context context, Intent intent){
       String nome = intent.getExtras().getString("nome");
       Log.v("nome", nome);


   } 
}

What am I missing?

06-29 22:42:46.103: E/AndroidRuntime(3723): FATAL EXCEPTION: main
06-29 22:42:46.103: E/AndroidRuntime(3723): java.lang.RuntimeException: Error receiving broadcast Intent { act=com.carmen.progettosmp.PROXIMITY_ALERT flg=0x10 (has extras) } in com.carmen.progettosmp.ProximityIntentReceiver@427a2f58
06-29 22:42:46.103: E/AndroidRuntime(3723):     at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:795)
06-29 22:42:46.103: E/AndroidRuntime(3723):     at android.os.Handler.handleCallback(Handler.java:615)
06-29 22:42:46.103: E/AndroidRuntime(3723):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-29 22:42:46.103: E/AndroidRuntime(3723):     at android.os.Looper.loop(Looper.java:153)
06-29 22:42:46.103: E/AndroidRuntime(3723):     at android.app.ActivityThread.main(ActivityThread.java:4987)
06-29 22:42:46.103: E/AndroidRuntime(3723):     at java.lang.reflect.Method.invokeNative(Native Method)
06-29 22:42:46.103: E/AndroidRuntime(3723):     at java.lang.reflect.Method.invoke(Method.java:511)
06-29 22:42:46.103: E/AndroidRuntime(3723):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
06-29 22:42:46.103: E/AndroidRuntime(3723):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
06-29 22:42:46.103: E/AndroidRuntime(3723):     at dalvik.system.NativeStart.main(Native Method)
06-29 22:42:46.103: E/AndroidRuntime(3723): Caused by: java.lang.NullPointerException: println needs a message
06-29 22:42:46.103: E/AndroidRuntime(3723):     at android.util.Log.println_native(Native Method)
06-29 22:42:46.103: E/AndroidRuntime(3723):     at android.util.Log.v(Log.java:117)
06-29 22:42:46.103: E/AndroidRuntime(3723):     at com.carmen.progettosmp.ProximityIntentReceiver.onReceive(ProximityIntentReceiver.java:29)
06-29 22:42:46.103: E/AndroidRuntime(3723):     at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:785)
06-29 22:42:46.103: E/AndroidRuntime(3723):     ... 9 more
Was it helpful?

Solution

If you are creating a notification, you have to create a PendingIntent which in turn holds an Intent. Can't you set those extras in that Intent?. Also, it sounds you might benefit from the Geofencing API. One more thing, you are looking for the string "ciao" as a key, shouldn't you look for the key nome?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top