Android instance-Mobile security Defender (30)-complete the corresponding operation according to the instructions (alarm music and GPS tracking)

Source: Internet
Author: User

First, the goal

The corresponding operation is done according to the instructions sent by the security number.

Second, the Code implementation

1. Complete Play alarm music operation

①. Create a new folder named Raw under the Res folder and copy the audio file to the folder;

②. In the play Alarm Music Command code (that is, the else if ("#*alarm*#". Equals (body)) in the Smsreceiver class, through the Create (context) of the MediaPlayer object, int Resid (resource file ID)) method to create an MediaPlayer object, the parameter resid is the audio file under the Raw folder just now (note that the project package should be guided in the R.raw package, not the ANDROID.R package)

③. Call the Start () method of the MediaPlayer object to play audio, other setlooping (Boolean looping) methods, SetVolume (float leftvolume, float rightvolume) The method is used to set the loop playback and the left and right channel volume respectively.

The code is as follows:

 1  //  2  // toast.maketext (Context, "alarm music played", Toast.length_short). Show ();  3  MediaPlayer player = MediaPlayer. Create (context, r.raw.warning);  4  player.setlooping (false  
    
     );
     5  Player.setvolume (1.0f, 1.0f6   Player.start ();  7  abortbroadcast (); 
View Code

2, complete the acquisition of GPS location operation

①.gps location of the best use of services, so in the SRC folder under the new service package (named Com.example.mobilesafe.service), mainly for the storage of various services;

②. New Class (named Gpsservice) is used to implement the GPS service and inherit the service of the system, and then register the services in the function manifest file (androidmanifest.xml);

The registration code is as follows:

1 <service android:name= "Com.example.mobilesafe.service.GPSService" ></service>
View Code

③. In the new service class (Gpsservice), the Replication OnCreate () method and the OnDestroy () method define the Locationmanager object (named LM),

④. The OnCreate () method in the newly created service class (Gpsservice),

1). Gets the system service through the Getsystemservice (String name) method, the parameter name is Location_service (location service) and is strongly turned into Locationmanager ; (Additional knowledge points: the Getallproviders () method of the Locationmanager object can be used to get all the methods of positioning (mainly network positioning, base station positioning, and GPS positioning), which returns a list array of type string of value type. Traverse this array to get the positioning method)

2). Requestlocationupdates by Locationmanager object (variable name LM) (String provider, long mintime, float mindistance, Locationlistener listener) method update position changes, parameter provider is the positioning method, parameter two mintime how many times update (milliseconds, 0 is ignored), parameter three mindistance position change how many meters update (m units, 0 means ignore), parameter four listener position listener (interface, need to implement);

For parameter a provider (positioning mode) can be Locationmanager object (variable name LM) Getbestprovider (criteria, Boolean enabledonly (whether it is possible)) Methods to obtain the optimal positioning method, the criteria of criteria is the selection criterion of the optimal positioning method, the maximum precision (accuracy), the requirement of the Power (powerrequirement), the Setxxx method of the criteria object can be set up. Whether to pay (costallowed), Altitude Information (Altitude) and so on.

The code is as follows:

1 New Criteria (); 2         criteria.setaccuracy (criteria.accuracy_fine); 3         true ); 4         Lm.requestlocationupdates (bestprovider, 0, 0, listener);
View Code

⑤. New Class (named Mylocationlistener) implements the position listener interface (Locationlistener), implementing its four methods that are not implemented: 1.onLocationChanged (location location) When the position changes, the callback method, 2.onStatusChanged (String provider, int status, Bundle extras) is called when the state changes (positioning on or off), 3. Onproviderenabled (string provider) Call this method, 4.onProviderDisabled (string provider) when one of the three types of positioning methods can be used This method is called when one of the three types of positioning methods is not available. Define the variable and instantiate it with new.

In the callback method where the location of the onlocationchanged is changed

1). Get the latitude of the location standard (double type, add "" to string ") by the Getlatitude () method of the Position object, Getlongitude () method gets the latitude of the position standard, getaccuracy () method to obtain accuracy

Note: This is the standard latitude and longitude, the latitude and longitude of the map (Google Maps) on the coordinates are still a certain deviation, therefore, it is necessary to convert the latitude and longitude, specifically described in the "Mars coordinates" section.

2). The last coordinate position is saved to the "config" file by the Sharedpreferences object in the form of a key-value pair;

Save the code as follows:

1 // Save the latest position coordinates as key-value pairs in the config file by using the Sharedpreferences object 2             Sharedpreferences sp = getsharedpreferences ("config", mode_private); 3             Editor editor = Sp.edit (); 4             Editor.putstring ("Lastlocation", latitude+longitude+accuracy); 5             Editor.commit ();
View Code

⑤. In the OnDestroy () method of the newly created service class (Gpsservice), when the service is destroyed, the listener of the location service is canceled, that is, the OnDestroy () method of the replication activity, The listener is removed through the removeupdates (Locationlistener listener) of the Locationmanager object, and the listeners are set to null (NULL) to conserve resources.

⑥. The text message in the message receiver (Smsreceiver) class is "Get the GPS location (" #*location*# ". Equals (body))" when

1). Start the newly created GPS service class via the StartService (Intent Service) method of the Intent (Context packagecontext, class<?> CLS);

Start the GPS service code as follows:

1 // start the GPS service 2                     New Intent (context, Gpsservice.  Class); 3                     Context.startservice (INTENTGPS);
View Code

2). Obtain the latest position coordinates through the Sharedpreference object, and determine if the obtained coordinate value is empty, if NULL the description has not been acquired to the location, through the Smsmanager object's Sendtextmessage (String Destinationaddress (recipient, Sender here is the security number), String scaddress, string text (SMS content), Pendingintent sentintent, pendingintent Deliveryintent) method (the remainder is null) to send a text message back to the security number, or the location to the security number if not empty.

3). Increased access to location information: access_fine_location, Access_coarse_location, Access_mock_location.

Android instance-Mobile security Defender (30)-complete the corresponding operation according to the instructions (alarm music and GPS tracking)

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.