通过从其他手机发送短信来更改手机的网络状态

时间:2014-09-02 05:15:44

标签: android sms network-state

我想通过从其他手机发送短信来改变手机的网络状态。是否有可能这样做?

1 个答案:

答案 0 :(得分:2)

您可以使用短信接收广播接收器。当你收到某种短信时,你可以做你的工作。

Add it into your Manifest File
    <receiver
               android:name =".MyBroadcastReceiver">
            <intent-filter>
                  <action android:name="android.provider.Telephony.SMS_RECEIVED" />
              </intent-filter>
       </receiver>

    //Required permission
<action android:name="android.provider.Telephony.SMS_RECEIVED" />

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

//Here is your broadcast receiver class
public class MyBroadcastReceiver extends BroadcastReceiver{
    private static final String TAG = "MyBroadCastReceiver";
    @Override
    public void onReceive(Context context, Intent intent) {

             Bundle bndl = intent.getExtras();
            SmsMessage[] msg = null;
            String str = "";         
            if (null != bndl)
            {
                //**** You retrieve the SMS message ****
                Object[] pdus = (Object[]) bndl.get("pdus");
                msg = new SmsMessage[pdus.length];         
                for (int i=0; i<msg.length; i++){
                    msg[i] = SmsMessage.createFromPdu((byte[])pdus[i]);             
                    str += "SMS From " + msg[i].getOriginatingAddress();                   
                    str += " :\r\n";
                    str += msg[i].getMessageBody().toString();
                    str += "\n";
                }
                //---display incoming SMS as a Android Toast---
                System.out.Println(str);
            } 
        }
}