"Android Notes" remotely control your phone by sending a specific SMS message

Source: Internet
Author: User
Tags myadmin

implementation results:1. Send command #*location*#, you can remotely obtain the location of the phone (latitude and longitude), and returned as a text message. 2. Send command #*locknow*#, you can remotely lock screen and set the lock screen password.
Implementation principle: 1. Register the broadcast recipient, monitor the SMS received by the mobile phone, and intercept and process the specific SMS messages that meet the requirements. 2. Get the location via Locationmanager. 3. Use Devicepolicymanager to implement the lock screen, set the lock screen password, and other operations.
steps:

1. Create a tool class that can obtain a geographic location:

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 * * Get location Service */public class Gpsinfoprovider{private static Gpsinfoprovider instance = new Gpsinfoprovider ();p Rivate static Context context;private static Innerlocationlistener Mlistener = null;private Locationmanager manager = null ;p ublic Static Gpsinfoprovider getinstance (context context) {Gpsinfoprovider.context = Context;return instance;} Public String getLocation () {//Build location Management Object manager = (Locationmanager) context.getsystemservice (context.location_service ); Manager.requestlocationupdates (Getprovider (manager), 60*1000, Getlistener ()); Sharedpreferences sp = context.getsharedpreferences ("config", context.mode_private); return sp.getstring ("Location "," ");} public void RemoveListener () {if (manager! = null) {manager.removeupdates (Getlistener ());}} /** * Get location information source */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{@Overridepublic void onlocationchanged (location Location) {//position changed, record position to sharedpreferences in 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 ();} @Overridepublic void Onstatuschanged (String provider, int status, Bundle extRAS) {} @Overridepublic void onproviderenabled (String provider) {} @Overridepublic void onproviderdisabled (string Provider) {}}}

When the geographic location changes, the latest geo-location information is stored in the sharedpreferences through the listener, and the GetLocation methodGets the location information in the Sharedpreferences and returns.
2. After writing the tool class, configure the permissions:

<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. Create a Device management recipient (Inherit Deviceadminreceiver):

Package Cn.edu.chd.mobilesafe.receiver;import Android.app.admin.deviceadminreceiver;public Class MyAdmin extends deviceadminreceiver{}

This is actually a broadcast recipient, so it needs to be configured in the manifest file:

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

To activate the Device Manager:

/** * registered Device Manager */private void Registerdeviceadmin () {Devicepolicymanager manager = (Devicepolicymanager) This.getsystemservice (Context.device_policy_service); ComponentName madminname = new ComponentName (This,myadmin.class), if (!manager.isadminactive (mAdminName))//if inactive, {Intent Intent = new Intent (devicepolicymanager.action_add_device_admin); Intent.putextra (Devicepolicymanager.extra _device_admin, Madminname); startactivity (intent);}}

4. Next, register a broadcast receiver and listen for SMS messages received by the system.

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 S Tring TAG = "Smsreceiver"; @Overridepublic void OnReceive (context context, Intent Intent) {}}

5. Register in the manifest file and configure the permissions:

<receiver android:name= "Cn.edu.chd.mobilesafe.receiver.SmsReceiver" >            <intent-filter android: priority= ">                <action android:name=" Android.provider.Telephony.SMS_RECEIVED "/>            </ Intent-filter> </receiver>
Permissions:

<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. Logic to handle the OnReceive method:

Object[] PDUs = (object[]) Intent.getextras (). Get ("PDUs"), for (Object Pdu:pdus) {//To construct SMS messages from PDU data smsmessage SMS = SMSMESSAGE.CREATEFROMPDU ((byte[]) PDU); String content = Sms.getmessagebody (); LOG.I (TAG, "SMS content:" +content); String sender = Sms.getoriginatingaddress (), if ("#*location*#". Equals (content)) {//Stop broadcast abortbroadcast ();// Gets the location information string text = gpsinfoprovider.getinstance (context). GetLocation (); Gpsinfoprovider.getinstance (context). RemoveListener (); LOG.I (TAG, "Location:" +text); Smsmanager Smsmanager = Smsmanager.getdefault ();//Send SMS if (!text.equals ("")) {smsmanager.sendtextmessage (sender, NULL , text, NULL, NULL);} Else{smsmanager.sendtextmessage (sender, NULL, "Sorry, no location information was received.", NULL, NULL);}} else if ("#*locknow*#". Equals (content)) {Devicepolicymanager manager = (Devicepolicymanager) context.getsystemservice (context.device_policy_service);//Set the lock screen password Manager.resetpassword ("123", 0);//Lock screen Manager.locknow ();// Terminate broadcast abortbroadcast ();}}

Note: If you are a Xiaomi phone, you may not be able to intercept specific SMS messages. At this point you need to go to the SMS interface, settings, advanced settings, System SMS priority, deselect, allow third-party apps to intercept SMS notifications first.







Related Article

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.