【问题标题】:When pressed the call button, can't get number按下通话按钮时,无法获取号码
【发布时间】:2014-08-21 12:05:38
【问题描述】:

拨打号码时,号码不会显示在 Toast 消息中。这是我的代码:

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final String outgoing = "android.intent.action.NEW_OUTGOING_CALL" ;
    IntentFilter intentFilter = new IntentFilter(outgoing);
    BroadcastReceiver OutGoingCallReceiver = new BroadcastReceiver()
    {
        @Override
        public void onReceive(Context context, Intent intent) 
        {
            // TODO Auto-generated method stub
            String outgoingno = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
            Toast.makeText(context, "outgoingnum =" + outgoingno,Toast.LENGTH_LONG).show();
        }
    };

}

顺便说一下,当按下呼叫按钮时,上面的应用程序开始运行。我给予这样的所有权限:

 <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <receiver android:name=".OutgoingCallReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
        </intent-filter>
    </receiver>

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.NEW_OUTGOING_CALL" />

            <action android:name="android.intent.action.CALL_PRIVILEGED" />

            <category android:name="android.intent.category.DEFAULT" />

            <data android:scheme="tel" />

            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

【问题讨论】:

    标签: android android-activity numbers broadcastreceiver phone-call


    【解决方案1】:

    你必须注册接收者:

    registerReceiver(OutGoingCallReceiver, intentFilter);
    

    不要忘记在关机时取消注册。

    您还可能需要在 Java 代码和清单中将 Intent.CATEGORY_DEFAULT 添加到您的意图过滤器:

    Java:

    IntentFilter intentFilter = new IntentFilter(outgoing);
    intentFilter.addCategory(Intent.CATEGORY_DEFAULT);
    

    AndroidManifest.xml:

    <receiver android:name=".OutgoingCallReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>
    

    编辑:

    如果您希望您的应用不拦截呼叫而只是作为替代拨号器工作,那么您不需要 NEW_OUTGOING_CALL 操作,而是必须在 Activity 的 onStart() 中处理 CALL_PIVILEGED 操作,大致如下:

    @Override
    public void onStart() {
        super.onStart();
        Intent intent = getIntent();
        if (intent == null)
            return;
    
        String action = intent.getAction();
        if (action.equals("android.intent.action.CALL_PRIVILEGED") || action.equals("android.intent.action.CALL")) {
            android.net.Uri contentUri = intent.getData();
            intent.setData(null);
            String outgoingno = contentUri.getSchemeSpecificPart().replaceAll("\\s","");
            Toast.makeText(context, "outgoingnum =" + outgoingno,Toast.LENGTH_LONG).show();
    }
    

    此外,您可能还需要覆盖 onNewIntent

    请注意,在某些手机上,您可能需要挂钩 android.intent.action.CALL。同样在某些手机(如 HTC)上,这不会完全起作用,因为它们具有不会触发意图的自定义拨号器应用程序。

    【讨论】:

    • 还是一样的兄弟。我无法收到 Toast 消息。
    • 忘记了主要内容:registerReceiver。更新了答案。
    • 嗨,这个代码在调用者启动时工作。看那张照片:postimg.org/image/r3eecdyzt
    • 如果用户选择第二个-我的应用-我想在那个时候得到号码。
    • 那么你就不需要 NEW_OUTGOING_CALL 了,检查更新的答案。
    猜你喜欢
    • 2017-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-26
    • 2021-11-18
    • 1970-01-01
    相关资源
    最近更新 更多