android通过编程方式添加额外的和调用

android通过编程方式添加额外的和调用,android,Android,我正试图通过我的应用程序通过输入额外数据拨打电话,但在通话过程中我无法接收到这些数据。下面是我拨打电话和接听电话的代码,请解释一下我的错误 String uri = "tel:"+my_name; Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse(uri)); callIntent.putExtra("data", "mynewdata"); callIntent.addFla

我正试图通过我的应用程序通过输入额外数据拨打电话,但在通话过程中我无法接收到这些数据。下面是我拨打电话和接听电话的代码,请解释一下我的错误

String uri = "tel:"+my_name;
        Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse(uri));
        callIntent.putExtra("data", "mynewdata");
        callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        _context.startActivity(callIntent);
和呼叫接收器

public class PhoneStateReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        final Bundle bundle = intent.getExtras();
        if(bundle != null) {
            showToast(intent.getExtras().getString("data"));
        } 

        if (intent.getAction().equals("android.intent.action.PHONE_STATE")) { 
            String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
            Log.d(TAG,"PhoneStateReceiver**Call State=" + state);
            if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
                if(recieveCall){

                }
                Log.d(TAG,"PhoneStateReceiver**Idle");
            } else if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) { 

            } else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
            }
        } else if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) { 
            // Outgoing call
            String outgoingNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
            Log.d(TAG,"PhoneStateReceiver **Outgoing call " + outgoingNumber);

            setResultData(null); // Kills the outgoing call

        } else {
            Log.d(TAG,"PhoneStateReceiver **unexpected intent.action=" + intent.getAction());
        }
          }
    }
我正在完美地接收呼叫,甚至广播也在完美地工作,但我没有从intent“intent.getExtras().getBoolean(“data”)”获取数据。

这里您需要创建一个java文件(让我们将其命名为Constants.java):

现在查看我在项目中所做的更改以及注释

String uri = "tel:"+my_name;
        Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse(uri));
        Constants.data = true; /*see change, set data in Constants.data*/
        Constants.str = "some data to pass..."; /*see change*/
        callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        _context.startActivity(callIntent);
现在在广播接收器中查看所做的更改

public class PhoneStateReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        final Bundle bundle = intent.getExtras();
        if(bundle != null && Constants.data) { /*changes done, get boolean from the Constants.data*/
            showToast("hi i have recieve");
            showToast(Constants.str); /*changes done*/
        } 

        if (intent.getAction().equals("android.intent.action.PHONE_STATE")) { 
            String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
            Log.d(TAG,"PhoneStateReceiver**Call State=" + state);
            if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
                if(recieveCall){

                }
                Log.d(TAG,"PhoneStateReceiver**Idle");
            } else if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) { 

            } else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
            }
        } else if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) { 
            // Outgoing call
            String outgoingNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
            Log.d(TAG,"PhoneStateReceiver **Outgoing call " + outgoingNumber);

            setResultData(null); // Kills the outgoing call

        } else {
            Log.d(TAG,"PhoneStateReceiver **unexpected intent.action=" + intent.getAction());
        }
          }
    }

希望这解决了如何传递数据的问题

实际上,您正在将数据传递给Intent.ACTION\u调用,并且只能使用getIntent()在Intent.ACTION\u调用中获取数据。您正在接收的广播是针对某个不包含您提供的意图数据的其他线程的。你在广播中接收到的意图与你传递的意图不同。您需要以其他方式传递数据
intent.getStringExtra(“数据”)
Vijay,这也不起作用,因为她传递给intent.ACTION\u调用的意图与她在BroadCastReceiver中得到的意图不同。@RahulRaina这是同一个意图让我试试这个,等等。有关android编程的更多帮助,你也可以访问我的博客:
public class PhoneStateReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        final Bundle bundle = intent.getExtras();
        if(bundle != null && Constants.data) { /*changes done, get boolean from the Constants.data*/
            showToast("hi i have recieve");
            showToast(Constants.str); /*changes done*/
        } 

        if (intent.getAction().equals("android.intent.action.PHONE_STATE")) { 
            String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
            Log.d(TAG,"PhoneStateReceiver**Call State=" + state);
            if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
                if(recieveCall){

                }
                Log.d(TAG,"PhoneStateReceiver**Idle");
            } else if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) { 

            } else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
            }
        } else if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) { 
            // Outgoing call
            String outgoingNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
            Log.d(TAG,"PhoneStateReceiver **Outgoing call " + outgoingNumber);

            setResultData(null); // Kills the outgoing call

        } else {
            Log.d(TAG,"PhoneStateReceiver **unexpected intent.action=" + intent.getAction());
        }
          }
    }