getSystemService的使用示例

来源:互联网 发布:java程序性能优化 编辑:程序博客网 时间:2024/05/22 07:54

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{@Overridepublic void onCallStateChanged(int state, String incomingNumber) {// TODO Auto-generated method stubsuper.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);//激活NotificationnotificationManager.notify(0, notification);}




0 0