communication between two device using sms

2019-01-29 11:20发布

问题:

I want my program to be able to receive sms from a special number("+9856874236").

I want application to be recive SMS ,if the SMS is contain "enable wifi" ,then change wifi to enable

i 'm using this code but it working crash??

MainActivity

package com.example.sms;

import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Bundle;

import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {
    static TextView messageBox;


    static String x="";
    static Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         messageBox=(TextView)findViewById(R.id.messageBox);
         check();

    }


    //How to used other or many activity



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }


     public static void updateMessageBox(String msg,String from)
        {
            messageBox.append(msg);
            //Get parent phone number from database
            //check parent number with ...
            if(msg.equals("enable wifi"))
            {
                x="yes";
                MainActivity objSampleClass = new MainActivity();  
                objSampleClass.check();  
            }
            //context.startActivity(new Intent(context, MainActivity.class));

            //Intent i=new Intent(context, p1.class);
            //context.startActivity(i);

          }

     public void check()
     {

         if(x.equals("yes"))
         {
             WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
              wifi.setWifiEnabled(true);
         }
         else
         {

         }
     }

}

manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.sms"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.sms.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name="com.example.sms.TextMessageReceiver" >
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>


        <activity
            android:name="com.example.sms.Test"
            android:label="@string/title_activity_test" >
        </activity>

    </application>

    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" >

    </uses-permission>
    <uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" >
    </uses-permission>

</manifest>

回答1:

To intercept the SMS as early as possible I use the following code:

public class Communicator extends Service {

private final String TAG = this.getClass().getSimpleName();

private SMSReceiver mSMSreceiver;
private IntentFilter mIntentFilter;

@Override
public void onCreate()
{
    super.onCreate();


    Log.i(TAG, "Communicator started");
    //SMS event receiver
    mSMSreceiver = new SMSReceiver();
    mIntentFilter = new IntentFilter();
    mIntentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
    mIntentFilter.setPriority(2147483647);
    registerReceiver(mSMSreceiver, mIntentFilter);

    Intent intent = new Intent("android.provider.Telephony.SMS_RECEIVED");
    List<ResolveInfo> infos = getPackageManager().queryBroadcastReceivers(intent, 0);
    for (ResolveInfo info : infos) {
        Log.i(TAG, "Receiver name:" + info.activityInfo.name + "; priority=" + info.priority);
    }
}

@Override
public void onDestroy()
{
    super.onDestroy();

    // Unregister the SMS receiver
    unregisterReceiver(mSMSreceiver);
}

@Override
public IBinder onBind(Intent arg0) {
    return null;
}

private class SMSReceiver extends BroadcastReceiver
{
    private final String TAG = this.getClass().getSimpleName();

    @Override
    public void onReceive(Context context, Intent intent)
    {
        Bundle extras = intent.getExtras();


        String strMessage = "";

        if ( extras != null )
        {
            Object[] smsextras = (Object[]) extras.get( "pdus" );

            for ( int i = 0; i < smsextras.length; i++ )
            {
                SmsMessage smsmsg = SmsMessage.createFromPdu((byte[])smsextras[i]);

                String strMsgBody = smsmsg.getMessageBody().toString();
                String strMsgSrc = smsmsg.getOriginatingAddress();

                strMessage += "SMS from " + strMsgSrc + " : " + strMsgBody;                    

                Log.i(TAG, strMessage);

                if (strMsgBody.contains("SomeText")) {


                            // perform code

                    this.abortBroadcast();

                }
            }



        }

    }

}
}

Remember to register the service in manifest:

<service android:name="yourpackage.Communicator" />

Now in onCreate and onDestroy start and stop the service:

startService(new Intent(this, Communicator.class));
stopService(new Intent(this, Communicator.class));


回答2:

In your BroadcastReceiver use following code,

Uri uri = Uri.parse("content://sms/");

ContentResolver contentResolver = getContentResolver();

String phoneNumber = "+9856874236";
String sms = "address='"+ phoneNumber + "'";
Cursor cursor = contentResolver.query(uri, new String[] { "_id", "body" }, "read = 0 and address='" + sms, null,   null);

if ( cursor.getCount() > 0 ) 
{
    // perform action to start Wifi Connection

}

Also in AndroidManifest.xml file you need to set Priority for reading SMS by your application as follows,

<receiver android:name="com.example.sms.TextMessageReceiver" >
    <intent-filter android:priority="1000">
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />
    </intent-filter>
</receiver>