getSystemService的使用示例

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);
	}




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值