Android instance-Mobile security Defender (36)-Confirm CheckBox Check status based on service open

Source: Internet
Author: User

First, the goal

1, according to whether the service is running in the background to determine the checkbox's selected state;

2. The "Turn on Caller ID display attribution" feature in the "Setting Center" function is selected when exiting the program, and the feature is checked when you turn off the "to display" service in Task Manager

Second, the Code implementation

1. Create a new package under the src file (named com.example.mobilesafe.utils) for storing various tool classes;

2, under the tool package new class (named Serviceutils, no need to inherit any class), used to verify whether a service is running (open) In the new Class (Serviceutils), create a static method with a Boolean type of return value (named Isservicerunning, which verifies that the service is working correctly), and the incoming parameter two is the context Because the system service is required through the context in the method) and specifies the service name to verify whether it is functioning correctly (a string-type parameter), in the static method of the validation service:

(1) Get the system service by the Getsystemservice (String name) method of the context, which gets the ACTIVITY and service state by the parameter name (that is, the name parameter value is Context.activity_service). Strong turn, the method returns the Activitymanager object (named AM);

(2) Gets the information collection object of the running service through the getrunningservices (int maxnum) method of the Activitymanager object (AM) list<runningserviceinfo> (named Serviceinfos);

(3) through the enhanced for Loop Traversal Service information collection (Serviceinfos), in the Enhanced for loop:

① returns a String object (named name) by getting the service component from the Service information object Runningserviceinfo (named info) and obtaining the service name from the component's GetClassName ();

② The IF statement to determine whether the incoming service name (that is, the second passed parameter of the static method in 2) equals the (equal () method) Gets the service name equal to return true;

(4) returns false;

Whether the new service is running the class and static validation method code is as follows:

1  Public classServiceutils {2     3     /**4 * Verify that a service is still working5      * @paramcontext: Contexts that need to be associated6      * @paramserviceName: Service full path name that needs to be inspected7      * @return8      */9      Public Static Booleanisservicerunning (Context context,string serviceName) {Ten         //Verify that the service is running correctly OneActivitymanager am =(Activitymanager) Context.getsystemservice (context.activity_service); Alist<runningserviceinfo> Serviceinfos = am.getrunningservices (100); -          for(Runningserviceinfo info:serviceinfos) { -String name =info.service.getClassName (); the             if(servicename.equals (name)) { -                 return true; -             } -         } +         return false; -     } +}
View Code

3, in the "Set Center" page Code (settingactivity) in the "set whether to turn on the caller number attribution display function" template through the new Intent (This,showcomingphoneadd.class) After instantiating a intent object (named Showaddintent), verify that a service is in the normal operating method isservicerunning (context context, 2) by using the New tool Class (Serviceutils). String ServiceName) (that is, a new static method) verifies that the specified service is running, and that the parameter context context is the Set center Class (Settingactivity.this), the parameter ServiceName service name is the service that is passed in when the intent is instantiated (use the full path name, which is Com.example.mobilesafe.service.ShowComingPhoneAd D). The method returns a value of type Boolean (named Isservicerunning).

4, through the if...else ... The statement and isservicerunning conditions determine whether the specified service is running, if the Set Radio box is selected if it is running, and if it is not running, set the radio box to blank state;

New part of the code in the Settings Center page:

1 //(This sentence is the statement before the new code) Showaddintent = new Intent (this,showcomingphoneadd.class);2 //determine if the "Caller ID Attribution Service (SHOWCOMINGPHONEADD)" is running based on whether the custom service is running the test method3         Booleanidservicerunning = serviceutils.isservicerunning (settingactivity. This, "Com.example.mobilesafe.service.ShowComingPhoneAdd");4         if(idservicerunning) {5             //service is running, setting display function is selected6Setting_show_incoming_add.setchecked (true);7}Else{8             //The service is not running, setting the display function to a blank state9Setting_show_incoming_add.setchecked (false);Ten}
View Code

Android instance-Mobile security Defender (36)-Confirm CheckBox Check status based on service open

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.