Android Service Getsystemservice
Last Update:2015-11-05
Source: Internet
Author: User
<span id="Label3"></p>http://blog.sina.com.cn/s/blog_71d1e4fc0100o8qr.htmlhttp://blog.csdn.net/bianhaohui/article/details/6220135 Android background running in a lot of service, they are systemserver on when the system starts, support the normal operation of the system, such as Mountservice monitoring for SD card installation and removal, Clipboardservice provides the Clipboard function, Packagemanagerservice provides installation removal and viewing of packages, and so on, applications can access the data provided by the service through the manager interface provided by the System. Getsystemservice is an important API for android, a method of activity that obtains the corresponding object based on the name passed in, and then translates it into the appropriate service Object. The following describes the System's corresponding Services. Name passed in | Returned Object | Description <ul> <li><li>Window_service WindowManager managing open Windows programs</li></li> <li><li>Layout_inflater_service Layoutinflater Gets the view defined in the XML</li></li> <li><li>Activity_service Activitymanager managing the System state of an application</li></li> <li><li>Power_service Powermanger power Supply Services</li></li> <li><li>Alarm_service Alarmmanager Alarm Clock Service</li></li> <li><li>Notification_service Services for Notificationmanager status bar</li></li> <li><li>Keyguard_service Keyguardmanager Keypad Lock Service</li></li> <li><li>Location_service Locationmanager location services, such as GPS</li></li> <li><li>Search_service Searchmanager Search for services</li></li> <li><li>Vebrator_service vebrator mobile phone Shake service</li></li> <li><li>Connectivity_service CONNECTIVITY network-connected Services</li></li> <li><li>Wifi_service Wifimanager Wi-fi Service</li></li> <li><li>Telephony_service Teleponymanager Telephone Service</li></li> </ul>Currently available names Is: <ul> <li><li>Window_service ("window") the top-level window manager in Which-can place custom Windows. The returned object is a Windowmanager.</li></li> <li><li>Layout_inflater_service ("layout_inflater") A layoutinflater for inflating layout resources in this Context.</li></li> <li><li>Activity_service ("activity") A Activitymanager for interacting and the global activity state of the System.</li></li> <li><li>Power_service ("power") A PowerManager for controlling power Management.</li></li> <li><li>Alarm_service ("ALARM") A Alarmmanager for receiving intents at the time of your choosing.</li></li> <li><li>Notification_service ("NOTIFICATION") A Notificationmanager for informing the user of background Events.</li></li> <li><li>Keyguard_service ("keyguard") A Keyguardmanager for controlling Keyguard.</li></li> <li><li>Location_service ("location") A Locationmanager to controlling location (e.g., GPS) Updates.</li></li> <li><li>Search_service ("search") A searchmanager for handling Search.</li></li> <li><li>Vibrator_service ("vibrator") A Vibrator for interacting with the vibrator Hardware.</li></li> <li><li>Connectivity_service ("connection") A Connectivitymanager for handling management of network Connections.</li></li> <li><li>Wifi_service ("wifi") A Wifimanager for management of Wi-fi Connectivity.</li></li> <li><li>Input_method_service ("input_method") an Inputmethodmanager for management of input METHODS.</li></li> <li><li>Ui_mode_service ("uimode") an uimodemanager for controlling UI Modes.</li></li> <li><li>Download_service ("DOWNLOAD") A Downloadmanager for requesting HTTP downloads</li></li> </ul>Note:system Services obtained via this API is closely associated with the Context in which they is obtained from. In general, does not share the service objects between various different contexts (activities, applications, Services, provi ders, Etc.)<strong><strong>an example:</strong></strong><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;">Use this piece of code when you get Mobile information from android:</p></p><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"></p></p><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"><span style="font-size: 12px;">public class Basicinfo {</span></p></p><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"></p></p><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"><span style="font-size: 12px;"><span style="line-height: 26px; font-family: Arial;">Public String Getphonenumber ()</span></span></p></p><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"><span style="font-size: 12px;"><span style="line-height: 26px; font-family: Arial;">{</span></span></p></p><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"><p style="margin:0px; padding:0px; text-align:left; line-height:26px; font-family:arial;"><span style=" font-size:12px; "><span style="line-height:26px; font-family:arial; > <span style=" line-height:26px; font-family:arial; "> <span style="line-height:26px; font-family:arial;"> <span style=" line-height:26px; font-family:arial; ">//get phone number MSISDN, most likely empty </span> </span> </span> </span> </p></p><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"><p style="margin:0px; padding:0px; text-align:left; line-height:26px; font-family:arial;"><span style=" font-size:12px; "><span style="line-height:26px; font-family:arial; > <span style=" line-height: 26px; font-family:arial; "> <span style="line-height:26px; font-family:arial;">telephonymanager TM = ( Telephonymanager) <span style="font-family:mceinline; > <strong> getsystemservice </strong> <span style=" line-height:26px; font-family:arial; "> (context.telephony_service); </span> </span> </span> </span> </p></p><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"><p style="margin:0px; padding:0px; text-align:left; line-height:26px; font-family:arial;"><span style=" font-size:12px; "><span style="line-height:26px; font-family:arial; > <span style=" line-height:26px; font-family:arial; "> <span style="line-height:26px; font-family:arial;"> <span style=" line-height:26px; font-family:arial; ">stringbuffer inf = new StringBuffer (); </span> </span> </span> </span> </p></p><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"><p style="margin:0px; padding:0px; text-align:left; line-height:26px; font-family:arial;"><span style=" font-size:12px; "><span style="line-height:26px; font-family:arial; > <span style=" line-height:26px; font-family:arial; "> <span style="line-height:26px; font-family:arial;"> <span style=" line-height:26px; font-family:arial; ">switch (tm.getsimstate ()) {//getsimstate () Gets the status of the SIM has the following 6 status </span> </span> </span> </span> </p></p><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"><p style="margin:0px; padding:0px; text-align:left; line-height:26px; font-family:arial;"><span style=" font-size:12px; "> <span style="line-height:26px; font-family:arial; > <span style=" line-height:26px; font-family:arial; "> <span style="line-height:26px; font-family:arial;"> <span style="line-height:26px; font-family:arial; "> case TelephonyManager.SIM_STATE_ABSENT:inf.append ("no card"); return inf.tostring (); </span> </span> </span> </span> </p></p><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"><p style="margin:0px; padding:0px; text-align:left; line-height:26px; font-family:arial;"><span style=" font-size:12px; "> <span style="line-height:26px; font-family:arial;"> <span style=" line-height:26px; font-family:arial; "> <span style="line-height:26px; font-family:arial;"> <span style="line-height:26px; font-family:arial; "> case TelephonyManager.SIM_STATE_UNKNOWN:inf.append ("unknown state"); return inf.tostring (); </span> </span> </span></span></span></p></p><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"><p style="margin:0px; padding:0px; text-align:left; line-height:26px; font-family:arial;"><span style=" font-size:12px; "> <span style="line-height:26px; font-family:arial;"> <span style="line-height:26px; font-family:arial; "> <span style="line-height:26px; font-family:arial;"> <span style=" line-height:26px; font-family:arial; "> case TelephonyManager.SIM_STATE_NETWORK_LOCKED:inf.append ("requires Networkpin to unlock"); return inf.tostring (); </span> </span> </span> </span> </span> </p></p><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"><p style="margin:0px; padding:0px; text-align:left; line-height:26px; font-family:arial;"><span style=" font-size:12px; "> <span style="line-height:26px; font-family:arial;"> < Span style= "line-height:26px; font-family:arial; " > <span style="line-height:26px; font-family:arial; > <span style=" line-height:26px; font-family:arial; "> case TelephonyManager.SIM_STATE_PIN_REQUIRED:inf.append ("requires PIN to unlock"); return inf.tostring (); </span> </span> </span> </p></p><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"><p style="margin:0px; padding:0px; text-align:left; line-height:26px; font-family:arial;"><span style=" font-size:12px; "> <span style="line-height:26px; font-family:arial;"> < Span style= "line-height:26px; font-family:arial; " > <span style="line-height:26px; font-family:arial; > <span style=" line-height:26px; font-family:arial; ">case TelephonyManager.SIM_STATE_PUK_REQUIRED:inf.append ("requires PUK to unlock"); return inf.tostring (); </span> </span> </span> </p></p><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"><span style="font-size: 12px;">case <span style="line-height: 26px; font-family: Arial;"> <span style="line-height: 26px; font-family: Arial;"> <span style="line-height: 26px; font-family: Arial;">TelephonyManager.SIM_STATE_READY:break; </span></span></span></span></p></p><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"><span style="font-size: 12px;">}</span></p></p><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"><span style="font-size: 12px;"></span></p></p><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"><span style="font-size: 12px;"><span style="line-height: 26px; font-family: Arial;">String PhoneNumber = Tm.getline1number ();</span></span></p></p><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"><span style="font-size: 12px;"><span style="line-height: 26px; font-family: Arial;">Return phonenumber;</span></span></p></p><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"><span style="font-size: 12px;">}</span></p></p><p style="margin: 0px; padding: 0px; text-align: left; font-family: Arial;"><p style="margin: 0px; padding: 0px; text-align: left; font-family: Arial;"></p></p><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;">When called in another activity class, there is always a process shutdown to get the phone Information. <span style="line-height: 26px; font-family: Arial;">later found</span></p></p><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"></p></p> <table style="text-align: left; color: #000000; line-height: 26px; font-family: Arial;" border="0"> <tbody> <tr> <td><p style="margin: 0px; padding: 0px;"><code><strong><span style="font-size: small;"><span style="text-decoration: underline;">getSystemService这个方法基于context,只有存在TextView控件的窗体中这个方法才会被激活~</span></span></strong></code></p></td> </tr> </tbody> </table><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"></p></p><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;">So:</p></p><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;">1. <span style="line-height: 26px; font-family: Arial;">Add a constructor with the parameter context to basicinfo:</span></p></p><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;">Public Basicinfo (context Context)</p></p><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;">{</p></p><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"><span style="line-height: 26px; font-family: Arial;">This.context = context;</span></p></p><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;">}</p></p><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"></p></p><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;">2. <span style="line-height: 26px; font-family: Arial;">Change the getphonenumber () function to:</span></p></p><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"><span style="font-size: small;">Context.getsystemservice (context.telephony_servic);</span></p></p><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"></p></p><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"><span style="font-size: small;">3. <span style="line-height: 26px; font-family: Arial;">inside the calling class Basicinfo bi = new Basicinfo (this);</span></span></p></p><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;"><p style="margin: 0px; padding: 0px; text-align: left; line-height: 26px; font-family: Arial;">Bi.getphonenumber ();</p></p><p><p>Android Service Getsystemservice</p></p></span>