-1

我创建了一个简单的应用程序来获取来电号码,工作并向我显示带有 toast 的号码,但我想将此变量传递给我的主要活动进行处理...示例在呼叫管理员号码时发送短信。我尝试此代码但没有数据显示在我的用户界面中...有什么想法吗?对此有帮助吗?

   public class IncomingCallReceiver extends BroadcastReceiver {


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

            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);

                    Log.i("IncomingCallReceiver","Incomng Number: " + phonenumber);

                    String info = "Detect Calls sample application\nIncoming number: " + phonenumber;



                    Intent intentsend=new Intent("update");//Send data to update UI
                    intentsend.putExtra( "data","Admin call..."+info);
                    context.sendBroadcast(intentsend);

                    Toast.makeText(context, info, Toast.LENGTH_LONG).show();
            }

我的主要活动是......

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

     updateReceiver=new BroadcastReceiver() {
          @Override
          public void onReceive(Context context, Intent intentsend) {
              //get extras, do some stuff
            String data = intentsend.getStringExtra("data");

            textphone.setText(data);

          }
      };
      IntentFilter updateIntentFilter=new IntentFilter("update");
      registerReceiver(updateReceiver, updateIntentFilter);
}
4

2 回答 2

0

我认为当电话响起时您的活动不可见,因为电话应用程序已弹出并且您的活动处于暂停状态。

您不能在接收器内部执行逻辑吗?或者可能在服务内?

于 2013-01-26T12:45:56.510 回答
0

1)将此接收器声明为您活动的内部类。如下所示,如果 onReceive 获得相同的意图你正在广播,还添加一个条件

       class  updateReceiver extends BroadcastReceiver() {
          @Override
          public void onReceive(Context context, Intent intentsend) {
              //get extras, do some stuff
            String data = intentsend.getStringExtra("data");

            textphone.setText(data);

          }
      };
于 2013-01-26T12:47:23.503 回答