按下通话键时,无法获得号码

时间:2014-08-21 12:05:38

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

拨打号码时,号码不会显示在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>

1 个答案:

答案 0 :(得分:1)

你必须注册接收者:

registerReceiver(OutGoingCallReceiver, intentFilter);

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

此外,您可能必须将Intent.CATEGORY_DEFAULT添加到Java代码和清单中的intent过滤器中:

<强>爪哇:

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操作,而是必须在您的活动的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),这完全没有用,因为他们有自定义拨号器应用程序,不会触发意图。