获取号码,客户从标准拨号盘拨打

时间:2012-12-30 09:54:26

标签: android

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

                                  

        <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 block:

Intent intent = getIntent();

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

终于给了我一个空:(

尝试使用brodacast接收器:

清单中的

    <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 个答案:

答案 0 :(得分: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();
    }
}

答案 1 :(得分: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();
    } 
}

答案 2 :(得分:1)

  • <intent-filter android:priority="9999">添加到清单中的intent-filter声明中,以确保您排在第一位
  • 从清单中接收者声明的com.myapps.android属性中删除android:name - 部分(即它应该是:android:name=".DialBroadcastReceiver"

答案 3 :(得分: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);
    }
  }
}