我在发送带有附加功能的意图时遇到这样的问题:作为双精度的值被转换为整数,并且在接收意图时不能被读取为双精度。代码如下。
在 WidgetProvider 的 onUpdate 方法中创建 Intent:
Intent send = new Intent("ACTION_SEND_DATA");
send.putExtra("url", "http://some-url.com");
send.putExtra(LONGITUDE, new Double(13.65));
send.putExtra(LATITUDE, new Double(43.12));
PendingIntent sendPendingIntent = PendingIntent.getBroadcast(context, 0, send, 0);
views.setOnClickPendingIntent(R.id.Change, sendPendingIntent);
在 WidgetProvider 的 onReceive 方法中接收 Intent:
else if (intent.getAction().equals("ACTION_SEND_DATA"))
{
Log.d("WIDGET", "received ACTION_SEND_DATA intent");
String msg = intent.getStringExtra("url");
Bundle extras = intent.getExtras();
if (extras != null)
{
// Get data via the key
String url = extras.getString("url");
Double longitude = new Double(intent.getDoubleExtra(LONGITUDE, -100));
Double latitude = new Double(extras.getDouble(LATITUDE, -100));
msg = "received: " + url + " " + longitude + " " + latitude;
sendData(extras);
}
else
msg += " no extras!";
Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
}
这行:
Double longitude = new Double(intent.getDoubleExtra(LONGITUDE, -100));
Double latitude = new Double(extras.getDouble(LATITUDE, -100));
在 DDMS LogCat 中抛出警告:
什么会导致此错误?