1.设备管理器 系统服务
// 拿到一个设备管理器
DevicePolicyManager devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
// new一个新的组件出来,用来启动注册管理器的界面
ComponentName componentName = new ComponentName(this,
MyAdminReceiver.class);
// 判断是否已经注册,没有就进行注册
if (!devicePolicyManager.isAdminActive(componentName))
{
Intent intent = new Intent(
DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
componentName);
startActivity(intent);
}
2.窗口,来电显示 系统服务
windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
listener = new MyPhoneListener();
telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
private class MyPhoneListener extends PhoneStateListener{
@Override
public void onCallStateChanged(int state, String incomingNumber) {
// TODO Auto-generated method stub
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE: //空闲状态
if(tv != null){
windowManager.removeView(tv); //移除显示归属的那个View
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK: //接通电话
if(tv !=null){
windowManager.removeView(tv); //移除显示归属的那个View
}
break;
case TelephonyManager.CALL_STATE_RINGING: //响铃状态
String address = NumberAddressService.getAddress(incomingNumber);
showLocation(address);
break;
default:
break;
}
}
3.通知栏的 系统服务
@SuppressWarnings("deprecation")
private void showNotifycation(String number){
//拿到Notifycation的管理者
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//new 一个Notifycation出来
@SuppressWarnings("deprecation")
Notification notification = new Notification(R.drawable.notification,"发现响一声",System.currentTimeMillis());
Context context = getApplicationContext();
//设置成一点就消失
notification.flags = Notification.FLAG_AUTO_CANCEL;
Intent notificationIntent = new Intent(context,NumberSecurityActivity.class);
notificationIntent.putExtra("number", number);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,notificationIntent,0);
notification.setLatestEventInfo(context, "响一声号码", number, pendingIntent);
//激活Notification
notificationManager.notify(0, notification);
}