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:
| The name passed in |
The returned object |
Description |
| Window_service |
WindowManager |
To manage open Windows programs |
| Layout_inflater_service |
Layoutinflater |
Gets the view defined in the XML |
| Activity_service |
Activitymanager |
Managing the system state of an application |
|
Power_service |
Powermanger |
Service of power supply |
| Alarm_service |
Alarmmanager |
Services for the alarm clock |
|
Notification_service |
Notificationmanager |
Service for status bar |
| Keyguard_service |
Keyguardmanager |
Service for keyboard lock |
| Location_service |
Locationmanager |
Location of the service, such as GPS |
| Search_service |
Searchmanager |
Search for services |
| Vebrator_service |
Vebrator |
Mobile phone Shake Service |
| Connectivity_service |
Connectivity |
Network-connected Services |
| Wifi_service |
Wifimanager |
Wi-Fi Service |
| Telephony_service |
Teleponymanager |
Telephone service |
|
code example, use to determine if a network connection is available and determine if it is WiFi:
protected intChecknetworkinfo () {Connectivitymanager con=(Connectivitymanager) Getsystemservice (Context.connectivity_service); Networkinfo Networkinfo=Con.getactivenetworkinfo (); if(Networkinfo = =NULL|| !networkinfo.isavailable ()) { //The current network is not available return1; } BooleanWiFi =Con.getnetworkinfo (Connectivitymanager.type_wifi). isconnectedorconnecting (); if(WiFi) {//use WiFi internet access return2; } return3; }Reprint: Get Android running tasks and services
Detects if a activityupdate is at the top of the current task's stack
Public Static BooleanIstopactivy (String cmdName, Context context) {Activitymanager Manager=(Activitymanager) Context.getsystemservice (Context.activity_service); List<RunningTaskInfo> Runningtaskinfos =manager.getrunningtasks (Integer.max_value); String cmpnametemp=NULL; if(NULL!=Runningtaskinfos) {Cmpnametemp= (Runningtaskinfos.get (0). topactivity). toString (); } if(NULL==cmpnametemp) { return false; } returncmpnametemp.equals (cmdName); }
Determine if the Android app is in the foreground
Public Static BooleanIsapponforeground (Context context) {Activitymanager Activitymanager=(Activitymanager) Context.getsystemservice (Context.activity_service); String PackageName=Context.getpackagename (); List<RecentTaskInfo> apptask = Activitymanager.getrecenttasks (Integer.max_value, 1); if(Apptask = =NULL) { return false; } if(Apptask.get (0). Baseintent.tostring (). Contains (PackageName)) {return true; } return false; }
Get all the desktops installed in your Android phone
Private StaticList<string>Getallthelauncher (Context context) {List<String> names =NULL; Packagemanager PKGMGT=Context.getpackagemanager (); Intent it=NewIntent (Intent.action_main); It.addcategory (Intent.category_home); List<ResolveInfo> RA = pkgmgt.queryintentactivities (it, 0); if(Ra.size ()! = 0) {names=NewArraylist<string>(); } for(inti = 0; I < ra.size (); i++) {String PackageName=Ra.get (i). Activityinfo.packagename; Names.add (PackageName); } returnnames; }
Android Judging program front and back status
Public Static BooleanIslauncherrunnig (Context context) {Booleanresult =false; List<String> names =Getallthelauncher (context); Activitymanager Mactivitymanager=(Activitymanager) Context.getsystemservice (Context.activity_service); List<ActivityManager.RunningAppProcessInfo> applist =mactivitymanager.getrunningappprocesses (); for(Runningappprocessinfo running:applist) {if(Running.importance = =runningappprocessinfo.importance_foreground) { for(inti = 0; I < names.size (); i++) { if(Names.get (i). Equals (Running.processname)) {result=true; Break; } } } } returnresult; }
Android OS Service (Context.getsystemservice ())