关于获取来电显示的idea

当我们在运营商开启来电显示的时候:获取电话号码就易如反掌;

public class GetPhoneNumService extends Service {
private TelephonyManager telephonyManager;


@Override
public IBinder onBind(Intent intent) {
return null;
}


@Override
public void onCreate() {
super.onCreate();
Toast.makeText(getApplicationContext(), "Incoming number ",1).show();
MyPhoneStateListener phoneListener=new MyPhoneStateListener(getApplicationContext()); //我们派生的类
telephonyManager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
telephonyManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}


class MyPhoneStateListener extends PhoneStateListener {
   Context context;
   
   public MyPhoneStateListener(Context context) {
super();
this.context = context;
}


@Override
   public void onCallStateChanged(int state,String incomingNumber){
       Toast.makeText(context, "Incoming number "+incomingNumber,1).show(); //incomingNumber就是来电号码
   }
}
}

别忘了加上权限:

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

但是,当我们没有开启来电显示的时候呢?

这个时候,因为运营商没有将手机号码,发给我们,所以是获取不到了(这个没有亲身实践,只是从网上查到的;因为本人的手机都是有来显的,悲剧!!!)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值