收到特定消息时打开Wi-Fi

时间:2014-12-24 18:40:01

标签: android sms broadcastreceiver wifi android-wifi

我希望在发送短信" on_location"时启用Wi-Fi和GPS收到了。我制作以下代码,但收到短信时应用程序终止。你能帮我吗?提前谢谢。

public class SmsSaveContact extends BroadcastReceiver {
//private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
//private static final String TAG = "SMSBroadcastReceiver";
@Override
public void onReceive(Context context, Intent intent) {
    //abortBroadcast();
    Bundle myBundle = intent.getExtras();
    SmsMessage [] messages = null;
    String strMessage = "";

    if (myBundle != null)
    {
        Object [] pdus = (Object[]) myBundle.get("pdus");
        messages = new SmsMessage[pdus.length];

        for (int i = 0; i < messages.length; i++)
        {
            messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
            strMessage += "SMS From: " + messages[i].getOriginatingAddress();
            strMessage += " : ";
            strMessage += messages[i].getMessageBody();
            //strMessage += "\n";
        }
        if(strMessage.contains("on_location")){
            WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            if(!wifiManager.isWifiEnabled()){

                   wifiManager.setWifiEnabled(true);

                 }else{

                     wifiManager.setWifiEnabled(true);

                   }    
        }
    }
}

}

更新了Log Cat:

  

12-24 20:55:47.846:W / dalvikvm(7356):threadid = 1:线程退出,未捕获异常(group = 0x41805c08)   12-24 20:55:47.896:E / AndroidRuntime(7356):致命异常:主要   12-24 20:55:47.896:E / AndroidRuntime(7356):进程:com.example.cameraservice,PID:7356   12-24 20:55:47.896:E / AndroidRuntime(7356):java.lang.RuntimeException:在com.example.cameraservice中接收广播Intent {act = android.provider.Telephony.SMS_RECEIVED flg = 0x8000010(has extras)}时出错.SmsSaveContact @ 42abde80   12-24 20:55:47.896:E / AndroidRuntime(7356):在android.app.LoadedApk $ ReceiverDispatcher $ Args.run(LoadedApk.java:782)   12-24 20:55:47.896:E / AndroidRuntime(7356):在android.os.Handler.handleCallback(Handler.java:733)   12-24 20:55:47.896:E / AndroidRuntime(7356):在android.os.Handler.dispatchMessage(Handler.java:95)   12-24 20:55:47.896:E / AndroidRuntime(7356):在android.os.Looper.loop(Looper.java:157)   12-24 20:55:47.896:E / AndroidRuntime(7356):在android.app.ActivityThread.main(ActivityThread.java:5356)   12-24 20:55:47.896:E / AndroidRuntime(7356):at java.lang.reflect.Method.invokeNative(Native Method)   12-24 20:55:47.896:E / AndroidRuntime(7356):at java.lang.reflect.Method.invoke(Method.java:515)   12-24 20:55:47.896:E / AndroidRuntime(7356):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1265)   12-24 20:55:47.896:E / AndroidRuntime(7356):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)   12-24 20:55:47.896:E / AndroidRuntime(7356):at dalvik.system.NativeStart.main(Native Method)   12-24 20:55:47.896:E / AndroidRuntime(7356):引起:java.lang.SecurityException:WifiService:用户10373和当前进程都没有android.permission.ACCESS_WIFI_STATE。   12-24 20:55:47.896:E / AndroidRuntime(7356):在android.os.Parcel.readException(Parcel.java:1465)   12-24 20:55:47.896:E / AndroidRuntime(7356):在android.os.Parcel.readException(Parcel.java:1419)   12-24 20:55:47.896:E / AndroidRuntime(7356):在android.net.wifi.IWifiManager $ Stub $ Proxy.getWifiEnabledState(IWifiManager.java:1174)   12-24 20:55:47.896:E / AndroidRuntime(7356):在android.net.wifi.WifiManager.getWifiState(WifiManager.java:1279)   12-24 20:55:47.896:E / AndroidRuntime(7356):在android.net.wifi.WifiManager.isWifiEnabled(WifiManager.java:1291)   12-24 20:55:47.896:E / AndroidRuntime(7356):at com.example.cameraservice.SmsSaveContact.onReceive(SmsSaveContact.java:37)   12-24 20:55:47.896:E / AndroidRuntime(7356):在android.app.LoadedApk $ ReceiverDispatcher $ Args.run(LoadedApk.java:772)   12-24 20:55:47.896:E / AndroidRuntime(7356):... 9更多

2 个答案:

答案 0 :(得分:1)

logcat日志中的

  

引起:java.lang.SecurityException:WifiService:用户10373和当前进程都没有android.permission.ACCESS_WIFI_STATE。 12-24 20:55:47.896:

为mainfist.xml文件添加了接收消息权限和wifi状态权限?

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

答案 1 :(得分:0)

<receiver android:name=".SmsSaveContact">

        <intent-filter android:priority="999">

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

...........

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