/**
* 号码归属地服务
*
* 2017-10-24 下午4:50:15
*/
public class AddressService extends Service {
private TelephonyManager tel;
private MyphoneStateListener listener;
private CustomToast customToast;
private MyOutGoingCall myOutGoingCall;
@Override
public IBinder onBind(Intent intent) {
return null;
}
private class MyOutGoingCall extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
String address = AddressDao.getAddress(context, number);
if (!TextUtils.isEmpty(address)) {
customToast.showToast(address);
}
}
}
@Override
public void onCreate() {
super.onCreate();
customToast = new CustomToast(this);
myOutGoingCall = new MyOutGoingCall();
IntentFilter filter = new IntentFilter();
filter.addAction("android.intent.action.NEW_OUTGOING_CALL");
registerReceiver(myOutGoingCall, filter);
tel = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
listener = new MyphoneStateListener();
tel.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
}
private class MyphoneStateListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
customToast.hideToast();
break;
case TelephonyManager.CALL_STATE_RINGING:
String address = AddressDao.getAddress(AddressService.this, incomingNumber);
if (!TextUtils.isEmpty(address)) {
customToast.showToast(address);
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
break;
}
super.onCallStateChanged(state, incomingNumber);
}
}
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(myOutGoingCall);
tel.listen(listener, PhoneStateListener.LISTEN_NONE);
}
}