标签:
1.创建一个能够获取地理位置的工具类:
package cn.edu.chd.mobilesafe.engine; import android.content.Context; 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; /** * @author Rowandjj * *获取位置服务 */ public class GPSInfoProvider { private static GPSInfoProvider instance = new GPSInfoProvider(); private static Context context; private static InnerLocationListener mListener = null; private LocationManager manager = null; public static GPSInfoProvider getInstance(Context context) { GPSInfoProvider.context = context; return instance; } public String getLocation() { //构建位置管理对象 manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); manager.requestLocationUpdates(getProvider(manager), 60*1000, 50, getListener()); SharedPreferences sp = context.getSharedPreferences("config",Context.MODE_PRIVATE); return sp.getString("location",""); } public void removeListener() { if(manager != null) { manager.removeUpdates(getListener()); } } /** * 获取位置信息源 */ private String getProvider(LocationManager manager) { Criteria c = new Criteria(); c.setAccuracy(Criteria.ACCURACY_FINE); c.setAltitudeRequired(false); c.setCostAllowed(true); c.setPowerRequirement(Criteria.POWER_MEDIUM); c.setSpeedRequired(true); return manager.getBestProvider(c,true); } private InnerLocationListener getListener() { if(mListener == null) { mListener = new InnerLocationListener(); } return mListener; } private class InnerLocationListener implements LocationListener { @Override public void onLocationChanged(Location location) { //位置发生变化时,记录位置到SharedPreferences中 String lat = ""+location.getLatitude(); String lon = ""+location.getLongitude(); SharedPreferences sp = context.getSharedPreferences("config",Context.MODE_PRIVATE); Editor editor = sp.edit(); editor.putString("location",lat+"##"+lon); editor.commit(); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } } }
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
package cn.edu.chd.mobilesafe.receiver; import android.app.admin.DeviceAdminReceiver; public class MyAdmin extends DeviceAdminReceiver { }
<receiver android:name="cn.edu.chd.mobilesafe.receiver.MyAdmin" android:permission="android.permission.BIND_DEVICE_ADMIN" > <meta-data android:name="android.app.device_admin" android:resource="@xml/device_admin" /> <intent-filter> <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" /> </intent-filter> </receiver>
/** *注冊设备管理者 */ private void registerDeviceAdmin() { DevicePolicyManager manager = (DevicePolicyManager) this.getSystemService(Context.DEVICE_POLICY_SERVICE); ComponentName mAdminName = new ComponentName(this,MyAdmin.class); if(!manager.isAdminActive(mAdminName))//假设未激活,激活之 { Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mAdminName); startActivity(intent); } }
package cn.edu.chd.mobilesafe.receiver; import cn.edu.chd.mobilesafe.engine.GPSInfoProvider; import android.app.admin.DevicePolicyManager; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.telephony.SmsManager; import android.telephony.SmsMessage; import android.util.Log; /** * @author Rowandjj * */ public class SmsReceiver extends BroadcastReceiver { private static final String TAG = "SmsReceiver"; @Override public void onReceive(Context context, Intent intent) { } }
<receiver android:name="cn.edu.chd.mobilesafe.receiver.SmsReceiver" > <intent-filter android:priority="1000" > <action android:name="android.provider.Telephony.SMS_RECEIVED" /> </intent-filter> </receiver>权限:
<uses-permission android:name="android.permission.SEND_SMS" /> <uses-permission android:name="android.permission.READ_SMS" /> <uses-permission android:name="android.permission.WRITE_SMS" /> <uses-permission android:name="android.permission.RECEIVE_SMS" />
Object[] pdus = (Object[]) intent.getExtras().get("pdus"); for(Object pdu : pdus) { //从pdu数据中构造短信信息 SmsMessage sms = SmsMessage.createFromPdu((byte[]) pdu); String content = sms.getMessageBody(); Log.i(TAG,"短信内容:"+content); String sender = sms.getOriginatingAddress(); if("#*location*#".equals(content)) { //终止广播 abortBroadcast(); //获取位置信息 String text = GPSInfoProvider.getInstance(context).getLocation(); GPSInfoProvider.getInstance(context).removeListener(); Log.i(TAG,"LOCATION:"+text); SmsManager smsmanager = SmsManager.getDefault(); //发送短信 if(!text.equals("")) { smsmanager.sendTextMessage(sender, null, text, null, null); }else { smsmanager.sendTextMessage(sender, null,"抱歉,没有收到位置信息.", null, null); } }else if("#*locknow*#".equals(content)) { DevicePolicyManager manager = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE); //设置锁屏password manager.resetPassword("123",0); //锁屏 manager.lockNow(); //终止广播 abortBroadcast(); } }
标签:
原文地址:http://www.cnblogs.com/bhlsheji/p/4246346.html