[Android] mobile guard mobile phones enable location for text message instructions, android guard
Location
Create a service package
Create a GPSService class that inherits the system's Service class
Register in the configuration file
Override the onCreate () method and call back when the service is created.
Override the onDestroy () method and call back when the service is destroyed.
Take the code from the previous section to this place.
Get the last position after the user moves and save it to the SP
Convert the standard coordinates to the Mars coordinates. Put the database files under the assets Directory and put ModifyOffset. java under the service package.
Get the ModifyOffset object, and use the ModifyOffset. getInstance () method, parameter: input stream; convert the files in the asset directory to the input stream, and use getAssets (). open ("file name") to get the InputStream object,
Call the s2c () method of the ModifyOffset object and convert the standard value to "China" to obtain a new PointDouble object. The parameter is a PointDouble object, x, y.
Obtain y of the longitude PonitDouble object
Obtains the x of the PonitDouble object of the latitude.
Save location data to SP
Receive command sending location text message
Start the service, get the Intent object where the SMS is received, and call the startService () method of the Context object.
Obtain the location information saved in the SP.
Send SMS, SmsManager. getDefault (). sendTextMessage () method, which sends a text message to a security number. parameters: sendTextMessage (target mobile phone, null (not supported by the source mobile phone), text, sentIntent, deliveryIntent, delay report and delivery report, do not care to fill in null
This permission is required for android. permission. SEND_SMS
Determine whether the content is empty. If the content of the text message is being obtained, manually change the coordinates to obtain
GPSService. java
package com.qingguow.mobilesafe.service;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
public class GPSService extends Service {
private LocationManager lm;
private LocationListener listener;
private SharedPreferences sp;
@Override
public IBinder onBind (Intent arg0) {
return null;
}
// service creation
@Override
public void onCreate () {
super.onCreate ();
sp = getSharedPreferences ("config", MODE_PRIVATE);
// Get location manager
lm = (LocationManager) getSystemService (LOCATION_SERVICE);
listener = new MyLocationListener ();
Criteria criteria = new Criteria ();
criteria.setAccuracy (Criteria.ACCURACY_FINE);
String provider = lm.getBestProvider (criteria, true);
lm.requestLocationUpdates (provider, 0, 0, listener);
}
// service destruction
@Override
public void onDestroy () {
super.onDestroy ();
lm.removeUpdates (listener);
listener = null;
}
private class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged (Location location) {
// get longitude
String 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 (new PointDouble (location
.getLongitude (), location.getLatitude ()));
longitude = "longitude:" + pinit.x;
latitude = "latitude:" + pinit.y;
} catch (Exception e) {
e.printStackTrace ();
}
//save data
Editor editor = sp.edit ();
editor.putString ("lastlocation", longitude + latitude + acc);
editor.commit ();
}
@Override
public void onStatusChanged (String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled (String provider) {
}
@Override
public void onProviderDisabled (String provider) {
}
}
}
SmsReceiver. java
package com.qingguow.mobilesafe.receiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.text.TextUtils;
import com.qingguow.mobilesafe.R;
import com.qingguow.mobilesafe.service.GPSService;
public class SmsReceiver extends BroadcastReceiver {
private SharedPreferences sp;
@Override
public void onReceive (Context context, Intent intent) {
sp = context.getSharedPreferences ("config", Context.MODE_PRIVATE);
// Get SMS content
Object [] 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", "");
// Just a SMS with a secure 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 information
Intent intent1 = new Intent (context, GPSService.class);
context.startService (intent1);
String lastLocation = sp.getString ("lastlocation", "");
//send messages
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;
}
}
}
}
}