[英]Android how to send data to a broadcast receiver using intents
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). 我正在编写一个应用程序,每当我靠近特定的兴趣点时,我都会启动一个接近警报(我从mysql数据库读取poi信息)。 When the BroadcastReceiver gets the intent, it creates a notification and everything works fine.
当BroadcastReceiver获得此意图时,它会创建一个通知,并且一切正常。 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.
问题是,当我尝试将这些参数通过意图传递给广播接收器时,会出现此错误:接收广播意图flg = 0x10时出错有其他错误。
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
If you are creating a notification, you have to create a PendingIntent which in turn holds an Intent. 如果要创建通知,则必须创建一个PendingIntent,而该PendingIntent却包含一个Intent。 Can't you set those extras in that Intent?.
您不能在Intent中设置这些额外功能吗? Also, it sounds you might benefit from the Geofencing API.
另外,听起来您可能会从Geofencing API中受益。 One more thing, you are looking for the string "ciao" as a key, shouldn't you look for the key nome?
还有一件事,您正在寻找字符串“ ciao”作为密钥,您是否不应该寻找密钥名称?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.