【问题标题】:get number, which customer dialed from standard dialpad获取客户从标准拨号盘拨打的号码
【发布时间】:2012-12-14 21:59:17
【问题描述】:

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

        <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();

最后给了我一个 null :(

尝试使用广播接收器:

在清单中:

    <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);

        }
    }

}

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

【问题讨论】:

  • 添加更多代码 - 你使用 BroadcastReceiver 还是什么?也看看这个线程,希望它会帮助你:stackoverflow.com/questions/9909153/…
  • @Boris Strandjev 查看已编辑的问题 - 我目前不使用广播接收器 :(
  • @BorisStrandjev 请查看已编辑的问题
  • 编辑只显示我链接到的帖子的复制粘贴。您确定您发布的代码正确吗?
  • @BorisStrandjev 请查看更新,有确切的代码,我正在使用....

标签: android


【解决方案1】:

这对我有用:

在清单中:

<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();
    }
}

【讨论】:

    【解决方案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();
        } 
    }
    

    【讨论】:

      【解决方案3】:
      • &lt;intent-filter android:priority="9999"&gt; 添加到清单中的intent-filter 声明中,以确保您排在第一位
      • 从清单中接收者声明的android:name 属性中删除com.myapps.android-part(即它应该是:android:name=".DialBroadcastReceiver"

      【讨论】:

      • 嗨。我无法验证优先级是否运作良好,因为我的应用程序从 C 开始,所以我首先在列表中;)但是对于高级主题来说,谢谢
      【解决方案4】:

      Manifest 文件中像这样注册BroadcastReceiver

      <!-- 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);
          }
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-25
        • 1970-01-01
        • 2012-06-07
        • 1970-01-01
        相关资源
        最近更新 更多