我尝试使用该代码从标准拨号盘接听电话:
<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);
}
}
}
但是当用户按下拨号时,会触发日志点头