当前位置:   article > 正文

getSystemService的使用示例_getsystemservice用法

getsystemservice用法

1.设备管理器 系统服务

  1. // 拿到一个设备管理器
  2. DevicePolicyManager devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
  3. // new一个新的组件出来,用来启动注册管理器的界面
  4. ComponentName componentName = new ComponentName(this,
  5. MyAdminReceiver.class);
  6. // 判断是否已经注册,没有就进行注册
  7. if (!devicePolicyManager.isAdminActive(componentName))
  8. {
  9. Intent intent = new Intent(
  10. DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
  11. intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
  12. componentName);
  13. startActivity(intent);
  14. }

2.窗口,来电显示 系统服务

  1. windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
  2. listener = new MyPhoneListener();
  3. telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
  4. telephonyManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
  1. private class MyPhoneListener extends PhoneStateListener{
  2. @Override
  3. public void onCallStateChanged(int state, String incomingNumber) {
  4. // TODO Auto-generated method stub
  5. super.onCallStateChanged(state, incomingNumber);
  6. switch (state) {
  7. case TelephonyManager.CALL_STATE_IDLE: //空闲状态
  8. if(tv != null){
  9. windowManager.removeView(tv); //移除显示归属的那个View
  10. }
  11. break;
  12. case TelephonyManager.CALL_STATE_OFFHOOK: //接通电话
  13. if(tv !=null){
  14. windowManager.removeView(tv); //移除显示归属的那个View
  15. }
  16. break;
  17. case TelephonyManager.CALL_STATE_RINGING: //响铃状态
  18. String address = NumberAddressService.getAddress(incomingNumber);
  19. showLocation(address);
  20. break;
  21. default:
  22. break;
  23. }
  24. }

3.通知栏的 系统服务

  1. @SuppressWarnings("deprecation")
  2. private void showNotifycation(String number){
  3. //拿到Notifycation的管理者
  4. NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  5. //new 一个Notifycation出来
  6. @SuppressWarnings("deprecation")
  7. Notification notification = new Notification(R.drawable.notification,"发现响一声",System.currentTimeMillis());
  8. Context context = getApplicationContext();
  9. //设置成一点就消失
  10. notification.flags = Notification.FLAG_AUTO_CANCEL;
  11. Intent notificationIntent = new Intent(context,NumberSecurityActivity.class);
  12. notificationIntent.putExtra("number", number);
  13. PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,notificationIntent,0);
  14. notification.setLatestEventInfo(context, "响一声号码", number, pendingIntent);
  15. //激活Notification
  16. notificationManager.notify(0, notification);
  17. }




声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/589007
推荐阅读
相关标签
  

闽ICP备14008679号