[Android] mobile phone Guardian phone implementation of SMS command acquisition location

Source: Internet
Author: User

Get location

To create a new service Package

Create a new Service class for the Gpsservice class inheritance system

Register in the manifest file

Override OnCreate () method, callback when service is created

Override OnDestroy () method, callback when service is destroyed

Get the code from the previous section to this place.

Get the last position after the user moves, save to SP

Convert standard coordinates to Mars coordinates, database files in assets directory, put Modifyoffset.java under service Package

Gets the modifyoffset object, through the modifyoffset.getinstance () method, parameter: input stream, the file in the asset directory into the input stream, using getassets (). Open ("file name") Get InputStream objects,

Call the s2c () method of the modifyoffset Object to turn the standard into Chinese to get the new pointdouble object, Parameters: pointdouble objects, x, y

Gets the y of the ponitdouble object to the longitude

Gets the x of the ponitdouble object to the latitude

Save location data to SP

Receive command send location SMS

Start the service, where you receive the SMS, get to the Intent object, call the startservice () method of the Context Object

Get location information saved to SP

Send SMS,smsmanager.getdefault (). Sendtextmessage () method, send SMS to security number, parameter:sendtextmessage ( Target Phone , null ( source phone not supported ), text, sentintent, deliveryintent) the latter two parameters, delay report and delivery report, do not care to fill NULL

This permission is required Android.permission.SEND_SMS

Determine if the content is empty, if the text is being sent to the content is getting, manually let the coordinates change a bit, in order to get

Gpsservice.java

 PackageCom.qingguow.mobilesafe.service;ImportAndroid.app.Service;Importandroid.content.Intent;Importandroid.content.SharedPreferences;ImportAndroid.content.SharedPreferences.Editor;ImportAndroid.location.Criteria;Importandroid.location.Location;ImportAndroid.location.LocationListener;ImportAndroid.location.LocationManager;ImportAndroid.os.Bundle;ImportAndroid.os.IBinder; Public classGpsserviceextendsService {PrivateLocationmanager LM; PrivateLocationlistener Listener; PrivateSharedpreferences sp; @Override Publicibinder onbind (Intent arg0) {return NULL; }    //Service Creation@Override Public voidonCreate () {Super. OnCreate (); SP=getsharedpreferences ("config", mode_private); //Get location ManagerLM =(Locationmanager) Getsystemservice (Location_service); Listener=NewMylocationlistener (); Criteria Criteria=NewCriteria ();        Criteria.setaccuracy (Criteria.accuracy_fine); String provider= Lm.getbestprovider (Criteria,true); Lm.requestlocationupdates (provider,0, 0, listener); }    //Service Destruction@Override Public voidOnDestroy () {Super. OnDestroy ();        Lm.removeupdates (listener); Listener=NULL; }    Private classMylocationlistenerImplementsLocationlistener {@Override Public voidonlocationchanged (location location) {//Get LongitudeString longitude = "Longitude:" +Location.getlongitude (); String Latitude= "Latitude:" +Location.getlatitude (); String ACC= "Accuracy:" +location.getaccuracy (); //convert Mars coordinates            Try{modifyoffset offset=modifyoffset.getinstance (Getassets (). Open ("Axisoffset.dat")); Pointdouble Pinit= OFFSET.S2C (Newpointdouble (location. Getlongitude (), Location.getlatitude ())); Longitude= "Longitude:" +Pinit.x; Latitude= "Latitude:" +Pinit.y; } Catch(Exception e) {e.printstacktrace (); }            //Save DataEditor editor=Sp.edit (); Editor.putstring ("Lastlocation", longitude+latitude+ACC);        Editor.commit (); } @Override Public voidOnstatuschanged (String provider,intstatus, Bundle extras) {} @Override Public voidonproviderenabled (String provider) {} @Override Public voidonproviderdisabled (String provider) {}}}

Smsreceiver.java

 PackageCom.qingguow.mobilesafe.receiver;ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;Importandroid.content.Intent;Importandroid.content.SharedPreferences;ImportAndroid.media.MediaPlayer;ImportAndroid.telephony.SmsManager;ImportAndroid.telephony.SmsMessage;Importandroid.text.TextUtils;ImportCOM.QINGGUOW.MOBILESAFE.R;ImportCom.qingguow.mobilesafe.service.GPSService; Public classSmsreceiverextendsBroadcastreceiver {PrivateSharedpreferences sp; @Override Public voidOnReceive (Context context, Intent Intent) {SP=context.getsharedpreferences ("config", context.mode_private); //Get SMS ContentObject[] objs= (object[]) Intent.getextras (). Get ("PDUs");  for(Object obj:objs) {smsmessage SMS=SMSMESSAGE.CREATEFROMPDU ((byte[]) obj); String Body=Sms.getmessagebody (); String Sender=sms.getoriginatingaddress (); String Secsender=sp.getstring ("Secphone", "" "); //a text message that determines the security number            if(Secsender.equals (sender)) {Switch(body) { Case"#*alarm*#"://Send alarm Music//Toast.maketext (Context, "play alarm Music", 1). Show ();MediaPlayer mp=mediaplayer.create (context, r.raw.alarm);                    Mp.start ();                    Abortbroadcast ();  Break;  Case"#*location*#"://Get location informationIntent intent1=NewIntent (Context,gpsservice.class);                    Context.startservice (INTENT1); String lastlocation= Sp.getstring ("Lastlocation", "" "); //Send SMS                    if(Textutils.isempty (lastlocation)) {Smsmanager.getdefault (). Sendtextmessage (sender,NULL, "Getting location",NULL,NULL); }Else{Smsmanager.getdefault (). Sendtextmessage (sender,NULL, Lastlocation,NULL,NULL); } System.out.println ("Get location Message" +lastlocation);                    Abortbroadcast ();  Break; default:                     Break; }            }        }    }}

[Android] mobile phone Guardian phone implementation of SMS command acquisition location

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.