【发布时间】: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