2016-01-12 82 views
-1

我有這樣的問題 - 「我的廣播」沒有收到任何信息。我做錯了什麼?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")); 
} 
+0

你如何創建一個'Intent'你'sendBroadcast使用'? – pskink

+0

你正在使用錯誤的Bundle.put/Bundle.get對...問bazillion次...如果你把字符串putExtra那麼你應該使用getStringExtra – Selvin

+0

檢查我的答案..我正在這樣做.. http:///stackoverflow.com/a/34740462/3713967 –

回答

0

您需要在發送廣播時設置操作。

試試這個

Intent intent = new Intent(); 
intent.setAction("MY_BROADCAST_CHECK"); 

因爲你的接收器方面鍵入:MY_BROADCAST_CHECK但是當你發送廣播你沒有給任何,這就是爲什麼你的接收機沒有得到任何信息。

-1

你應該使用Bundle這樣intent接收數據:

@Override 
public void onReceive(Context context, Intent intent) { 
// TODO Auto-generated method stub 
Bundle extras = intent.getExtras(); 
if (extras != null) { 

    Object value1=extras.get("latlang"); 
    Object value2=extras.get("model"); 
    Object value3=extras.get("brand"); 
    Object value4=extras.get("address"); 
} 

} 

然後settext值的相應。

0

創建意圖和發送廣播這樣的意圖過濾器的

Intent i = new Intent("a"); 
Bundle bundle = new Bundle(); 
bundle.putString(USER_ID, user_id); 
i.putExtras(bundle); 
mContext.sendBroadcast(i); 

聲明

IntentFilter filter = new IntentFilter(); 
filter.addAction("a"); 
filter.addAction("b"); 
filter.addAction("c"); 

接收廣播

registerReceiver(new BroadcastReceiver() { 
       @Override 
       public void onReceive(Context context, Intent intent) { 
        try { 
         if(intent.getAction().equals("a")) 
         goToPage(new a()); 
         if(intent.getAction().equals("b")) 
         { 
          // you can call other event and function here 
          Fragment fragment = new b(); 
          Bundle bundle = intent.getExtras(); 
          fragment.setArguments(bundle); 
          goToPage(fragment); 
         } 
         if(intent.getAction().equals("c")) 
         { 
          // you can call other event and function here 
          Fragment fragment = new c(); 
          Bundle bundle = intent.getExtras(); 
          fragment.setArguments(bundle); 
          goToPage(fragment); 
         } 
        } catch (Exception e) { 
         e.printStackTrace(); 
        } 

       } 
      }, filter);