通过getSystemServices获取手机管理大全
getSystemService是Android很重要的一個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 = new 05 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的Intent 02 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 //獲取屏幕管理器 02 WindowManagermWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE); 03 //獲取寬度 04 width = mWindowManager.getDefaultDisplay().getWidth(); 05 //獲取高度 06 height = mWindowManager.getDefaultDisplay().getHeight();本文摘自明日科技出版的《Android 精彩編程200例》,轉載請注明出處!!!
轉載于:https://www.cnblogs.com/mrxy/p/7988162.html
總結
以上是生活随笔為你收集整理的通过getSystemServices获取手机管理大全的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一些常见的兼容问题
- 下一篇: 绝对简单,就是将我自己的工作量估算乘2!