Android:来自AsyncTask的广播数据无法获取数据

时间:2011-05-20 17:42:36

标签: android

我正在使用Intent将AsyncTask线程中的数据广播回主Activity。广播收到我的活动没有问题,但我没有看到Uri数据或CharSequence数据。它们都是空的。

AsyncTask中的服务端:

   public static final String LOCATION_UPDATE = "com.locationTest.lct.LOCATION_UPDATE";
   char[] buffer = new char[10];
   buffer[0] = 't';
   buffer[1] = 'e';
   buffer[2] = 's';
   buffer[3] = 't';

   Intent intent = new Intent(LOCATION_UPDATE);
   intent.putExtra("location",buffer);
   sendBroadcast(intent);

在主要活动类的活动方面:

public static final String LOCATION_UPDATE = "com.locationTest.lct.LOCATION_UPDATE";
   public class LocationBroadcastReceiver extends BroadcastReceiver {
       public void onReceive(Context context,Intent intent)
       {
        Uri data = intent.getData();
        CharSequence location = intent.getCharSequenceExtra("location");
    }
}
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    iService = startService(new Intent(this,InputService2.class));
    IntentFilter filter = new IntentFilter(LOCATION_UPDATE);
    LocationBroadcastReceiver r = new LocationBroadcastReceiver();
    registerReceiver(r,filter);  

1 个答案:

答案 0 :(得分:0)

可能是因为您正在创建一个char数组,然后尝试将其作为char序列。试试

String location = new String(intent.getCharArrayExtra("location"));

或类似的东西,以便你从Intent中使用正确的get方法。