如何获得来电号码? [Android]

我正在尝试使用Android获取来电号码,但它返回null。

我在清单中添加用户权限:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<receiver android:name=".ServiceReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>

并使用此类:

public class ServiceReceiver extends BroadcastReceiver {

@Override
public void onReceive(final Context context, Intent intent) {
TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(new PhoneStateListener(){
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        super.onCallStateChanged(state, incomingNumber);
        System.out.println("incomingNumber : "+incomingNumber);
    }
 },PhoneStateListener.LISTEN_CALL_STATE);
}

但不起作用,有什么主意吗?

评论
呵呵
呵呵

Please note you need permission READ_CALL_LOG if you want to get incomingNumber, you can check documents here

字符串:呼叫电话号码。如果应用程序没有READ_CALL_LOG权限或运营商特权(请参阅TelephonyManager#hasCarrierPrivileges),则将传递一个空字符串作为参数。
点赞
评论