通过getSystemServices获取手机管理大全

来源:互联网 发布:手机淘宝无法安装 编辑:程序博客网 时间:2024/05/18 02:53

getSystemServiceAndroid很重要的一个API,它是Activity的一个方法,根据传入的NAME来取得对应的Object,然后转换成相应的服务对象。以下介绍系统相应的服务。

传入的Name

返回的对象

说明

WINDOW_SERVICE

WindowManager

管理打开的窗口程序

LAYOUT_INFLATER_SERVICE

LayoutInflater

取得xml里定义view

ACTIVITY_SERVICE

ActivityManager

管理应用程序的系统状态

POWER_SERVICE

PowerManger

电源服务

ALARM_SERVICE

AlarmManager

闹钟服务

NOTIFICATION_SERVICE

NotificationManager

状态栏服务

KEYGUARD_SERVICE

KeyguardManager

键盘锁服务

LOCATION_SERVICE

LocationManager

位置的服务,如GPS

SEARCH_SERVICE

SearchManager

搜索服务

VEBRATOR_SERVICE

Vebrator

手机震动服务

CONNECTIVITY_SERVICE

Connectivity

网络连接服务

WIFI_SERVICE

WifiManager

Wi-Fi服务

TELEPHONY_SERVICE

TeleponyManager

电话服务

 

示例1:图书《Android精彩编程200例》,实例006状态栏的服务例子


获取通知管理器并创建通知对象。代码如下:

01  //获取通知管理器,用于发送通知02  Notif icationManager notif icationManager =03  (Notif icationManager) getSystemService(NOTIFICATION_SERVICE);04  Notif icationCompat.Builder notif ication = new05  Notif icationCompat.Builder(MainActivity.this); //创建一个Notif ication对象

设置通知的相关参数与通知的发送时间。代码如下:

01  notification.setAutoCancel(true);02  //设置显示在状态栏的通知提示信息03  notification.setTicker("Android课程第一季上线啦!");04  //设置通知的小图标05  notification.setSmallIcon(R.mipmap.ic_launcher);06  //设置下拉列表中的大图标07notification.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));08  //设置通知内容的标题09  notification.setContentTitle("Android入门第一季!");10  //设置通知内容11  notification.setContentText("点击查看详情!");12  //设置发送时间13  notification.setWhen(System.currentTimeMillis());

设置从通知栏跳转至通知的详细内容界面,并进行通知的发送。代码如下:

01  //创建一个启动其他Activity的Intent02  Intent intent = new Intent(MainActivity.this,03  MessageActivity.class);04  PendingIntent pi = PendingIntent.getActivity(05  MainActivity.this, 0, intent, 0);06  //设置通知栏单击跳转07  notification.setContentIntent(pi);08  //发送通知09  notificationManager.notify(NOTIFYID, notification.build());

示例2:常用功能,获取屏幕高度宽度,代码如下:

01//获取屏幕管理器02WindowManagermWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE); 03//获取宽度04width = mWindowManager.getDefaultDisplay().getWidth();05//获取高度06height = mWindowManager.getDefaultDisplay().getHeight();  

本文摘自明日科技出版的《Android 精彩编程200例》,转载请注明出处!!!


原创粉丝点击