1.必须要有别人程序的源代码.
private static final String TAG = "AddressService";
private TelephonyManager manager;
private MyPhoneListener listener;
private WindowManager wManager;
private TextView tv;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
/**
* 服务第一次被创建的时候调用的方法
* 服务被初始化时调用的方法
*/
@Override
public void onCreate() {
super.onCreate();
listener = new MyPhoneListener();
manager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
wManager = (WindowManager) this.getSystemService(WINDOW_SERVICE);
manager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
}
/**
* 服务停止的时候调用
*/
@Override
public void onDestroy() {
super.onDestroy();
manager.listen(listener, PhoneStateListener.LISTEN_NONE);
listener = null;
}
private class MyPhoneListener extends PhoneStateListener{
/**
* 电话状态发生改变的时候调用的方法
*/
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
if(null != tv){
wManager.removeView(tv);
tv = null;
}
break;
case TelephonyManager.CALL_STATE_RINGING: // 零响状态
Log.i(TAG,"来电号码为"+ incomingNumber);
String address = NumberAddressService.getAddress(incomingNumber);
Log.i(TAG,"归属地为"+ address);
showLocation(address);
break;
case TelephonyManager.CALL_STATE_OFFHOOK: //接通电话状态
break;
}
}
}
/**
* 在窗体上显示出来位置信息
* @param address
*/
public void showLocation(String address) {
WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
params.format = PixelFormat.TRANSLUCENT;
params.type = WindowManager.LayoutParams.TYPE_TOAST;
params.setTitle("Toast");
tv = new TextView(AddressService.this);
tv.setText("号码归属地为:"+address);
wManager.addView(tv, params);
}
}
/**
* 在窗体上显示出来位置信息----参考Toast定义的
* @param address
*/
public void showLocation(String address) {
WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
params.format = PixelFormat.TRANSLUCENT;
params.type = WindowManager.LayoutParams.TYPE_TOAST;
params.setTitle("Toast");
TextView tv = new TextView(AddressService.this);
tv.setText("号码归属地为:"+address);
WindowManager manager = (WindowManager)
this
.getSystemService(
WINDOW_SERVICE
);
manager.addView(tv, params);
}