开发者

Android: Data from Broadcast from AsyncTask not getting data through

开发者 https://www.devze.com 2023-03-07 03:27 出处:网络
I am using an Intent to Broadcast data from a AsyncTask thread back to a the main Activity.The Broadcast is received my the Activity with no problem, but I am not seeing the Uri data or CharSequence d

I am using an Intent to Broadcast data from a AsyncTask thread back to a the main Activity. The Broadcast is received my the Activity with no problem, but I am not seeing the Uri data or CharSequence data. They are both null.

On Service side in 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);

On activity side in main Activity class:

public static final String LOCATION_UPDATE = "com.locationTest.lct.LOCATION_UPDATE";
   public class Loc开发者_开发知识库ationBroadcastReceiver 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);  


It could be because you're creating a char array and then trying to get it as a char sequence. Try with

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

or something similar so that you're using the correct get method from the Intent.

0

精彩评论

暂无评论...
验证码 换一张
取 消