2011-04-17 105 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

相关问题