码迷,mamicode.com
首页 > 移动开发 > 详细

【安卓笔记】通过发送特定的短信远程控制手机

时间:2015-01-24 20:04:45      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:

实现效果:
1.发送指令#*location*#,能够远程获取到手机的地理位置(经纬度),并以短信的形式返回。
2.发送指令#*locknow*#,能够远程锁屏并设置锁屏password。

实现原理:
1.注冊广播接受者,监听手机收到的短信,并对符合要求的特定短信进行拦截和处理。
2.通过LocationManager获取地理位置。
3.使用DevicePolicyManager实现锁屏、设置锁屏password等操作。

步骤:

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)
		{
		}
	}
}

当地理位置发生改变时,将通过监听器把最新的地理位置信息存到SharedPreferences中,getLocation方法从SharedPreferences中获取位置信息,并返回。

2.写好工具类后,配置权限:

 <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" />

3.创建一个设备管理接受者(继承DeviceAdminReceiver):

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);
		}
	}

4.接下来,注冊一个广播接收者,监听系统收到的短信。

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)
	{
	
	}	
}

5.在清单文件里注冊,并配置权限:

 <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" />

6.处理onReceive方法的逻辑:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!