我試圖通過意圖從服務調用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()方法中獲得正確的值。
將等待回覆。
我一個在這裏做類似的事情! http://stackoverflow.com/questions/14571564/android-pendingintent-extras-not-received-by-broadcastreceiver/14612215#14612215 – toobsco42 2013-01-31 07:53:41