First, common system services
Background service is systemservice on system boot
1, Mountservice: monitor whether there is SD card installation and removal.
2, Clipboardservice: Provide the function of shearing board.
3, Packagemanagerservice: To provide the installation of software package removal and review.
4, power, network connection status and so on.
Second, call the system service:
Getsystemservice () Method: A method of activity to obtain a system service object, pass in a string, and return a system service object.
Third, the commonly used system services:
| The name passed in |
The returned object |
Description |
| Window_service |
WindowManager |
To manage open Windows programs |
| Layout_inflater_service |
Layoutlnflater |
Gets the view defined in the XML |
| Activity_service |
Activitymanager |
Managing the system state of an application |
| Power_service |
PowerManager |
Power supply Service |
| Alarm_service |
Alarmmanager |
Alarm Clock Service |
| Notification_service |
Notificationmanager |
Status bar Service |
| Keyguard_service |
Keyguardmanager |
Keypad Lock Service |
Example: Get network connection status and turn off WiFi on
Main.xml
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"> <ButtonAndroid:onclick= "DoClick"Android:id= "@+id/btn_network"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Network is connected" /> <ButtonAndroid:onclick= "DoClick"Android:id= "@+id/btn_wifi"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "WiFi switch" /></LinearLayout>
Androidmanifest.xml System Permissions
<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.example.systemservicedemo"Android:versioncode= "1"Android:versionname= "1.0" > <USES-SDKandroid:minsdkversion= "8"android:targetsdkversion= "+" /> <uses-permissionAndroid:name= "Android.permission.ACCESS_NETWORK_STATE"/> <uses-permissionAndroid:name= "Android.permission.ACCESS_WIFI_STATE"/> <uses-permissionAndroid:name= "Android.permission.CHANGE_WIFI_STATE"/> <ApplicationAndroid:allowbackup= "true"Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/apptheme" > <ActivityAndroid:name= "Com.example.systemservicedemo.MainActivity"Android:label= "@string/app_name" > <Intent-filter> <ActionAndroid:name= "Android.intent.action.MAIN" /> <categoryAndroid:name= "Android.intent.category.LAUNCHER" /> </Intent-filter> </Activity> </Application></Manifest>
Main.java: Background code
PackageCom.example.systemservicedemo;Importandroid.app.Activity;ImportAndroid.content.Context;ImportAndroid.net.ConnectivityManager;ImportAndroid.net.NetworkInfo;ImportAndroid.net.wifi.WifiManager;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); } Public voidDoClick (View v) {Switch(V.getid ()) { Caser.id.btn_network:if(Isnetworkconnected (mainactivity. This) {toast.maketext (mainactivity). This, "Network connection OK", Toast.length_short). Show (); }Else{toast.maketext (mainactivity). This, "Network Connection Failed", Toast.length_short). Show (); } Break; CaseR.id.btn_wifi:wifimanager (); Break; } } /*** Detect if the network is connected *@paramContext *@return */ Private Booleanisnetworkconnected (Context context) {if(Context! =NULL) {Connectivitymanager Manager=(Connectivitymanager) Context.getsystemservice (Connectivity_service); Networkinfo Info=Manager.getactivenetworkinfo (); if(Info! =NULL){ returninfo.isavailable (); } } return false; } Private voidWifimanager () {Wifimanager WiFi= (Android.net.wifi.WifiManager) mainactivity. This. Getsystemservice (Wifi_service); if(wifi.iswifienabled ()) {wifi.setwifienabled (false); Toast.maketext (mainactivity. This, "WiFi is Off", Toast.length_short). Show (); }Else{wifi.setwifienabled (true); Toast.maketext (mainactivity. This, "WiFi is turned on", Toast.length_short). Show (); } }}
There are many kinds of system services, others need to be used to study, mainly through the Getsystemservice method to obtain different system service objects.
Android Learning (15) System services