我有這樣的問題 - 「我的廣播」沒有收到任何信息。我做錯了什麼?Android廣播不起作用
代碼示例
我與廣播註冊活動:
主要活動
DriverCheckBroadcast broadcast = new DriverCheckBroadcast(this);
IntentFilter intFilt = new IntentFilter("MY_BROADCAST_CHECK");
// регистрируем (включаем) BroadcastReceiver
registerReceiver(broadcast, intFilt);
startService(new Intent(this, DriverCheckService.class));
了Methode是把包裹廣播
@Override
public void onResponse(Response<Example> response, Retrofit retrofit) {
Example result = response.body();
LatLng driverlatlang = new LatLng(result.getGpsLatitude(),result.getGpsLongitude());
intent.putExtra("address",customizeText(result.getAddress()));
intent.putExtra("latlng",driverlatlang);
intent.putExtra("model",result.getModel().getName());
intent.putExtra("brand",result.getModel().getIdBrand());
status = result.getStatusName();
service.sendBroadcast(intent);
}
了Methode是讓包裹在廣播
@Override
public void onReceive(Context context, Intent intent) {
activity.driverlatlang = intent.getParcelableExtra("latlang");
activity.model.setText((CharSequence) intent.getParcelableExtra("model"));
activity.brand.setText((CharSequence) intent.getParcelableExtra("brand"));
activity.address.setText((CharSequence) intent.getParcelableExtra("address"));
}
你如何創建一個'Intent'你'sendBroadcast使用'? – pskink
你正在使用錯誤的Bundle.put/Bundle.get對...問bazillion次...如果你把字符串putExtra那麼你應該使用getStringExtra – Selvin
檢查我的答案..我正在這樣做.. http:///stackoverflow.com/a/34740462/3713967 –