自动启动蓝牙和wifi

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.cchen.connect"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />
	<uses-permission android:name="android.permission.BLUETOOTH" />
	<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
	<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
	<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />  

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <receiver android:name=".ActionReceiver">
           <intent-filter>
                <action android:name="android.intent.action.USER_PRESENT"/> 
           </intent-filter>
           <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/> 
           </intent-filter>
        </receiver>

    </application>
</manifest>
package com.cchen.connect;

import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.util.Log;

public class ActionReceiver extends BroadcastReceiver{
    static final String LOG_TAG = "ActionReceiver";
	private BluetoothAdapter blueAdapter;

    @Override
    public void onReceive(Context context, Intent intentIn) {
        Log.d(LOG_TAG, "ACTION = " + intentIn.getAction());
        String action = intentIn.getAction();
        if (Intent.ACTION_USER_PRESENT.equals(action) || Intent.ACTION_BOOT_COMPLETED.equals(action) )
        {
            blueAdapter = BluetoothAdapter.getDefaultAdapter();
            if(!blueAdapter.isEnabled()){
              blueAdapter.enable();
            }

            WifiManager wifiMgr = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
            int wifiState = wifiMgr.getWifiState();
            if(wifiState == WifiManager.WIFI_STATE_DISABLED)
            {
            	wifiMgr.setWifiEnabled(true);
            }

            Log.d(LOG_TAG, "========== UNLOCKINTENT " + blueAdapter.isEnabled() + wifiMgr.getWifiState());
        }
    }
}

相关推荐