2011-04-17 104 views
3

我試圖通過意圖從服務調用BroadcastReceiver。如何通過意圖將數據從服務傳遞到Android中的BroadcastReceiver

我打電話廣播接收器在我的服務文件如下:

final Handler handler = new Handler(); 
    final Runnable r = new Runnable()  {  
    public void run()     {  

       // code here what ever is required 
       System.out.println("Runnnn"); 
       counter++; 

       Intent i = new Intent(); 
       i.setAction("Refresh"); 
       Bundle b = new Bundle(); 
       b.putInt("Counter", counter); 
       i.putExtra("Bundle", b); 
       ctx.sendBroadcast(i); 

       handler.postDelayed(this, 1000);  


       Toast.makeText(getApplicationContext(), "counter"+counter, Toast.LENGTH_LONG).show(); 



       }  

      };  
      handler.postDelayed(r, 1000); 


在廣播接收器onReceive()如下:

public void onReceive(Context context, Intent arg1) { 
    System.out.println("OnReceiveeeeee"); 

    if(arg1.getAction().equalsIgnoreCase("Refresh")) 
    { 
     System.out.println("Received Intent"); 
     Bundle b = arg1.getExtras(); 
     c=b.getInt("Counter"); 
     System.out.println("Counter in Receiver:::"+c); 
    } 
} 


但我在的onReceive獲得價值爲零。
任何幫助表示讚賞在onReceive()方法中獲得正確的值。
將等待回覆。

+0

我一個在這裏做類似的事情! http://stackoverflow.com/questions/14571564/android-pendingintent-extras-not-received-by-broadcastreceiver/14612215#14612215 – toobsco42 2013-01-31 07:53:41

回答

1

你在錯誤的方式訪問。

Bundle b = arg1.getExtras(); 

您需要訪問如下。

Bundle b = intent.getBundleExtra("Bundle"); 

=========================================== =====

您也可以編寫代碼,而無需使用束也:

在服務

i.putExtra("Counter", counter); 

在廣播接收器

intent.getIntExtra("Counter", -1); // -1 is defalut value 
+0

感謝HellBoy。我遵循你給的程序。現在它的工作正常。 – 2011-04-17 18:23:25

1

這裏是我用來播放註銷提示我所有的應用程序活動,關閉代碼片段回到登錄屏幕

logoutBroadcastReceiver lbr; 

@Override 
public void onResume(){ 
... 
// register the broadcast receiver 
IntentFilter intentfilter = new IntentFilter("com.on3x.action.DO_LOGOUT"); 
lbr = new logoutBroadcastReceiver(); 
registerReceiver(lbr,intentfilter); 
super.onResume(); 
... 
} 

// broadcast receiver grabbing the "test" bundled extra 
    public class logoutBroadcastReceiver extends BroadcastReceiver { 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      Log.d(getString(R.string.app_name), "broadcast string: " + intent.getStringExtra("string_example")); 
      Log.d(getString(R.string.app_name), "extra!: " + intent.getIntExtra("int_example",0)); 

      finish(); 
     } 
    } 


// broadcast the intent to logout when logout button clicked 
// put the extra "test" in the bundle 
    public void onClickLogout(View _view) { 
     Intent i = new Intent("com.on3x.action.DO_LOGOUT"); 
     i.putExtra("string_example", "here is a broadcasted string"); 
     i.putExtra("int_example", 100); 
     sendBroadcast(i); 
    } 

時,我希望這些代碼可以幫助你得到你的工作?

編輯:更新爲使用putExtra()getStringExtra()/getIntExtra()

+0

嗨Android_programmer,發佈後,我去了,測試它實際上與我的代碼預計工作。它實際上不是,並且返回null。我改變了廣播類似'i.putExtra(「Counter」,100);'和接收器到'intent.getIntExtra(「Counter」,0)',現在完美運行。現在更新我的完整示例... – wired00 2011-04-17 11:46:18

+0

Thanks wired00。 – 2011-04-17 18:24:29

相關問題