Android如何使用意图将数据发送到广播接收器

时间:2013-06-29 18:57:05

标签: android notifications broadcastreceiver proximity

我正在编写一个应用程序,每当我接近特定的兴趣点时,我都会启动一个接近警报(我从一个mysql数据库中读取poi)。 当BroadcastReceiver获取意图时,它会创建一个通知,一切正常。当我点击通知时我想要一个活动开始,所以为了做到这一点,我需要向广播接收器发送一些参数(基本上是字符串),以便它可以将这些参数传递给我想要开始的活动。 问题是当我尝试通过意图将这些参数传递给广播接收器时,我得到了这个错误:错误接收广播意图flg = 0x10有额外的内容。

我以这种方式发送参数:

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

这是接收者:

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


   } 
}

我错过了什么?

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

1 个答案:

答案 0 :(得分:0)

如果要创建通知,则必须创建PendingIntent,而PendingIntent又包含Intent。你不能在那个意图中设置那些额外的东西吗?此外,听起来您可能会受益于Geofencing API。还有一件事,你正在寻找字符串“ciao”作为关键,你不应该寻找关键的nome吗?