两台设备之间使用短信进行通信

时间:2013-11-20 12:00:54

标签: javascript android static-methods

我希望我的程序能够从特殊号码(“+ 9856874236”)接收短信。

我希望应用程序是recive短信,如果短信包含“启用wifi”,则更改wifi启用

我正在使用此代码,但它正在崩溃?

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
         {

         }
     }

}

清单

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

2 个答案:

答案 0 :(得分:1)

要尽早拦截SMS,请使用以下代码:

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

                }
            }



        }

    }

}
}

请记住在清单中注册服务:

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

现在在onCreate和onDestroy中启动和停止服务:

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

答案 1 :(得分:0)

在您的BroadcastReceiver中使用以下代码

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

}

同样在AndroidManifest.xml文件中,您需要设置优先级以便按应用程序读取SMS,如下所示

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