我如何将变量从活动传递到广播接收器?

时间:2013-02-02 12:50:53

标签: android broadcastreceiver

  

可能重复:
  Pass a value from activity to broadcastreceiver and start a service from the broadcast receiver

我创建了一个应用程序,用于将来电号码发送到蓝牙,我希望将活动管理员号码从活动传递给接收者。我怎么能这样做?

  public void onResume() {
   super.onResume();

  registerReceiver(new IncomingCallReceiver(), new  intentFilter("android.intent.action.PHONE_STATE"));

   @Override
    public void onReceive(Context context, Intent intent) {
            Bundle bundle = intent.getExtras();

            String msg2 = intent.getStringExtra("admin_no");

            if(null == bundle)
                    return;

            Log.i("IncomingCallReceiver",bundle.toString());

            String state = bundle.getString(TelephonyManager.EXTRA_STATE);

            Log.i("IncomingCallReceiver","State: "+ state);

            if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))
            {
                    String phonenumber = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);

1 个答案:

答案 0 :(得分:0)

试过这样的事情?

   Intent intent = new Intent("android.intent.action.PHONE_STATE");
    intent.putExtra("admin_no","9999999"); // change number here.
    sendBroadcast(intent);