Android调用WI-FI的核心代码

来源:互联网 发布:暗影格斗2mac存档 编辑:程序博客网 时间:2024/05/22 19:55
1、strings.xml

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="app_name">Wi-Fi</string>    <string name="action_settings">Settings</string>    <string name="hello_world">Hello world!</string>    <string name="str_checked">开启Wi-Fi</string>    <string name="str_uncheck">关闭Wi-Fi</string>    <string name="str_start_wifi_failed">尝试开启WIFI,服务失败</string>    <string name="str_start_wifi_done">尝试开启WIFI,服务成功</string>    <string name="str_stop_wifi_failed">尝试关闭WIFI,服务失败</string>    <string name="str_stop_wifi_done">尝试开启WIFI,服务成功</string>    <string name="str_wifi_enabling">WIFI启动过程中……</string>    <string name="str_wifi_disabling">WIFI关闭过程中</string>    <string name="str_wifi_disabled">WIFI已经关闭</string>    <string name="str_wifi_unknow">WIFI未知状态……</string></resources>

2、values文件夹下新建一个color.xml

<?xml version="1.0" encoding="utf-8"?><resources>    <drawable name="darkgray">#808080</drawable>    <drawable name="white">#FFFFFF</drawable>    <drawable name="blue">#0000FF</drawable>    <drawable name="black">#000000</drawable></resources>

3、layout文件夹下的activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <TextView        android:id="@+id/myTextView1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="@string/hello_world"         android:textSize="30sp"/>    <CheckBox        android:id="@+id/myCheckBox1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/myTextView1"        android:layout_centerHorizontal="true"        android:layout_marginTop="67dp"        android:text="CheckBox" />    </RelativeLayout>

4、MainActivity.java

package com.ydonghao.wi_fi;import android.net.wifi.WifiManager;import android.os.Bundle;import android.app.Activity;import android.content.Context;import android.util.Log;import android.view.Menu;import android.view.View;import android.widget.CheckBox;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity {private TextView mTextView01;private CheckBox mCheckBox01;private WifiManager mWifiManager01;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mTextView01 = (TextView) findViewById(R.id.myTextView1);mCheckBox01 = (CheckBox) findViewById(R.id.myCheckBox1);mWifiManager01 = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);if(mWifiManager01.isWifiEnabled()){if(mWifiManager01.getWifiState() == WifiManager.WIFI_STATE_ENABLED){mCheckBox01.setChecked(true);mCheckBox01.setText(R.string.str_uncheck);} else {mCheckBox01.setChecked(false);mCheckBox01.setText(R.string.str_checked);}} else {mCheckBox01.setChecked(false);mCheckBox01.setText(R.string.str_checked);}mCheckBox01.setOnClickListener(new CheckBox.OnClickListener(){@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubif(mCheckBox01.isChecked() == false){try{if(mWifiManager01.isWifiEnabled()){if(mWifiManager01.setWifiEnabled(false)){mTextView01.setText(R.string.str_stop_wifi_done);}else{mTextView01.setText(R.string.str_stop_wifi_failed);}} else {switch(mWifiManager01.getWifiState()) {case WifiManager.WIFI_STATE_ENABLING:mTextView01.setText(getResources().getText(R.string.str_stop_wifi_failed)+":"+getResources().getText(R.string.str_wifi_enabling));break;case WifiManager.WIFI_STATE_DISABLING:mTextView01.setText(getResources().getText(R.string.str_stop_wifi_failed)+":"+getResources().getText(R.string.str_wifi_disabling));break;case WifiManager.WIFI_STATE_DISABLED:mTextView01.setText(getResources().getText(R.string.str_stop_wifi_failed)+":"+getResources().getText(R.string.str_wifi_disabled));break;case WifiManager.WIFI_STATE_UNKNOWN:default:mTextView01.setText(getResources().getText(R.string.str_stop_wifi_failed)+":"+getResources().getText(R.string.str_wifi_unknow));break;}mCheckBox01.setText(R.string.str_checked);}} catch (Exception e) {Log.i("HIPPO", e.toString());e.printStackTrace();}} else if(mCheckBox01.isChecked() == true){try{if(!mWifiManager01.isWifiEnabled() && mWifiManager01.getWifiState()!=WifiManager.WIFI_STATE_ENABLING){if(mWifiManager01.setWifiEnabled(true)){switch(mWifiManager01.getWifiState()){case WifiManager.WIFI_STATE_ENABLING:mTextView01.setText(getResources().getText(R.string.str_wifi_enabling));break;case WifiManager.WIFI_STATE_ENABLED:mTextView01.setText(getResources().getText(R.string.str_start_wifi_done));break;default:mTextView01.setText(getResources().getText(R.string.str_start_wifi_failed)+":"+getResources().getText(R.string.str_wifi_unknow));break;}}else{mTextView01.setText(R.string.str_start_wifi_failed);}}else{switch(mWifiManager01.getWifiState()){case WifiManager.WIFI_STATE_ENABLING:mTextView01.setText(getResources().getText(R.string.str_start_wifi_failed)+":"+getResources().getText(R.string.str_wifi_enabling));break;case WifiManager.WIFI_STATE_DISABLING:mTextView01.setText(getResources().getText(R.string.str_start_wifi_failed)+":"+getResources().getText(R.string.str_wifi_disabling));break;case WifiManager.WIFI_STATE_DISABLED:mTextView01.setText(getResources().getText(R.string.str_start_wifi_failed)+":"+getResources().getText(R.string.str_wifi_disabled));break;case WifiManager.WIFI_STATE_UNKNOWN:default:mTextView01.setText(getResources().getText(R.string.str_start_wifi_failed)+":"+getResources().getText(R.string.str_wifi_unknow));break;}}mCheckBox01.setText(R.string.str_uncheck);}catch(Exception e){Log.i("Errors", e.toString());e.printStackTrace();}} }});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}public void mMakeTextToast(String str, boolean isLong){if(isLong == true){Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show();}else{Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();}}@Overrideprotected void onResume(){super.onResume();try{switch(mWifiManager01.getWifiState()){case WifiManager.WIFI_STATE_ENABLED:mTextView01.setText(getResources().getText(R.string.str_wifi_enabling));break;case WifiManager.WIFI_STATE_ENABLING:mTextView01.setText(getResources().getText(R.string.str_wifi_enabling));break;case WifiManager.WIFI_STATE_DISABLING:mTextView01.setText(getResources().getText(R.string.str_wifi_disabling));break;case WifiManager.WIFI_STATE_DISABLED:mTextView01.setText(getResources().getText(R.string.str_wifi_disabled));break;case WifiManager.WIFI_STATE_UNKNOWN:default:mTextView01.setText(getResources().getText(R.string.str_wifi_unknow));break;}}catch(Exception e){e.getStackTrace();}}}

5、AndroidManifest.xml的权限赋予

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.ydonghao.wi_fi"    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.ydonghao.wi_fi.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>    </application><uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/><uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/><uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/><uses-permission android:name="android.permission.INTERNET"/><uses-permission android:name="android.permission.WAKE_LOCK"/></manifest>

6、文件分布






原创粉丝点击