1

我尝试使用该代码从标准拨号盘接听电话:

        <action android:name="android.intent.action.CALL" />
        <data android:scheme="tel" />
        <category android:name="android.intent.category.DEFAULT" />
        <action android:name="android.intent.action.CALL_PRIVILEGED" />
   </intent-filter>


</activity>

一切都很好,当用户从标准电话拨号盘拨号时,我的应用程序打开了。但我没有找到解决方案,我怎样才能得到一个电话号码,哪个用户被拨打了。PhonePadActivity 活动 onCreate 块中的代码:

Intent intent = getIntent();

String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Toast.makeText(this, "Call was made to-->>" + number, 5000).show();

最后给了我一个空值:(

尝试使用广播接收器:

在清单中:

    <receiver
        android:exported="true"
        android:name="com.myapps.android.DialBroadcastReceiver" >
        <intent-filter >
            <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
        </intent-filter>
    </receiver>

<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />

这是 DialBroadcastReceiver 类:

package com.myapps.android;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class DialBroadcastReceiver extends BroadcastReceiver {
    private static final String THIS_FILE = "PhonePadActivity";

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Log.e(THIS_FILE,"In onReceive()");

        if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
             String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);

             Log.e(THIS_FILE,"Number is: "+number);

        }
    }

}

但是当用户按下拨号时,会触发日志点头

4

4 回答 4

3

这对我有用:

在清单中:

<intent-filter>
    <action android:name="android.intent.action.CALL"/>
    <category android:name="android.intent.category.DEFAULT"/>
    <action android:name="android.intent.action.CALL_PRIVILEGED"/>
    <data android:scheme="tel"/>
</intent-filter>

在活动中:

String inputURI = this.getIntent().getDataString();
if (inputURI != null) {
    Uri uri = Uri.parse(Uri.decode(inputURI));
    if (uri.getScheme().equals("tel")) {
        String calledNumber = uri.toString();
    }
}
于 2012-12-30T11:12:35.547 回答
2

您收到的号码代码不正确。代替:

Intent intent = getIntent();

String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Toast.makeText(this, "Call was made to-->>" + number, 5000).show();

和:

Uri data = getIntent().getData(); 
if (data != null && ("tel".equals(data.getScheme()))) { 
    String number = PhoneNumberUtils.getNumberFromIntent(getIntent(), this); 
    if (number != null) { 
      Toast.makeText(this, "Call was made to-->>" + number, 5000).show();
    } 
}
于 2012-12-30T12:10:43.553 回答
1
  • 添加<intent-filter android:priority="9999">到清单中的意图过滤器声明,以确保您排在第一位
  • 从清单中接收者声明的属性中删除com.myapps.android-part android:name(即它应该是android:name=".DialBroadcastReceiver":)
于 2012-12-30T11:11:01.120 回答
0

在文件中注册一个BroadcastReceiver这样的:Manifest

<!-- DIAL Receiver -->
<receiver
    android:exported="true"
    android:name="receivers.DialBroadcastReceiver" >
    <intent-filter >
        <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
    </intent-filter>
</receiver>

您需要以下权限NEW_OUTGOING_CALL

最终,您可以像这样接收广播并检索拨打的号码:

public class DialBroadcastReceiver extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {

    Log.v("DileBroadCastReceiver","In onReceive()");

    if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
      String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
      Log.v("DialBroadcast Receiver","Number is: "+number);
    }
  }
}
于 2017-01-23T13:03:58.293 回答