启动 ACTION_CALL 时如何将 Extra 传递给 BroadcastReceiver

2024-04-01

我正在从我的活动中发起新的呼叫。并试图传递一个额外的布尔值。

public class CallInitiatingActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + number));
        intent.putExtra("com.demoapp.CUSTOM_CALL", true);
        startActivity(intent);
    }
}

我还注册了一个 BroadcastReceiver,它监听传出呼叫:

<receiver android:name=".OutgoingCallListener" android:exported="true" >
    <intent-filter android:priority="0" >
        <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</receiver>

基本上我期望 onReceive 看到我的额外内容,但不知何故它没有通过:

public class OutgoingCallListener extends BroadcastReceiver {

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

        for (String key : extras.keySet()) {
            Log.i(Constant.LOG_TAG, key + ": " + extras.get(key));
        }
    }
}

Output:

android.phone.extra.ALREADY_CALLED:false
android.intent.extra.PHONE_NUMBER:+370652xxxxx
com.htc.calendar.event_uri:null
android.phone.extra.ORIGINAL_URI:tel:+370652xxxxx

您的自定义额外内容存在于您用来启动“呼叫”活动的 Intent 中。但它没有被复制到NEW_OUTGOING_CALL作为实际调用机制的一部分广播的意图。这两个操作是不同的,并且彼此之间仅间接相关。只有Android系统本身可以广播NEW_OUTGOING_CALL Intent.

您无法将自己的额外内容添加到此 Intent 中。你需要想出另一种方法来完成你想要完成的任何事情。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

启动 ACTION_CALL 时如何将 Extra 传递给 BroadcastReceiver 的相关文章

随机推荐