android – 如何从来电获取电话号码?

前端之家收集整理的这篇文章主要介绍了android – 如何从来电获取电话号码?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
Android中有来电时,如何获取电话号码?

解决方法

做一个广播接收器说ServiceReceiver在Manifest中分配它的动作.
<receiver android:name=".ServiceReceiver" >
    <intent-filter>
        <action android:name="android.intent.action.PHONE_STATE" />
    </intent-filter>
</receiver>

将PhoneStateListener添加到TelephonyManager,PhoneStateListener具有覆盖onCallStateChanged()和Incoming number参数.而已.

ServiceReceiver.Java

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

猜你在找的Android相关文章