Android调用WI-FI的核心代码
来源:互联网 发布:暗影格斗2mac存档 编辑:程序博客网 时间:2024/05/22 19:55
1、strings.xml
2、values文件夹下新建一个color.xml
3、layout文件夹下的activity_main.xml
4、MainActivity.java
6、文件分布
<?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、文件分布
- Android调用WI-FI的核心代码
- Android Wi-Fi 获取RSSI的代码流程
- Android Wi-Fi TDLS调用流程
- Android Wi-Fi 设置带宽代码流程
- Android Wi-Fi EAP-SIM代码解析
- Android下得到Wi-Fi的SSID
- Android Wi-Fi 查看黑名单的方法
- ANdroid Wi-Fi 移植
- Android Wi-Fi WPS的Push Buttun方式的AP端调用流程
- Android Wi-Fi 设置country code的调用流程(博通芯片为base)
- Android Wi-Fi Peer-to-Peer(Android的Wi-Fi P2P对等网络)
- Android Wi-Fi工作原理
- Android Wi-Fi Direct 开发
- Android Wi-Fi工作原理
- Android Wi-Fi工作原理
- Android Wi-Fi基本操作
- Android Wi-Fi基本操作
- Android Wi-Fi WifiStateMachine解析
- 计算机视觉、机器学习相关领域论文和源代码
- 心跳包机制及Socket通信服务的心跳包
- 【Android 开发】: Android 消息处理机制之三: Handler 中 sendMessage() 源代码剖析
- 项目成功的因素
- mysql中的socket问题,当缺少socket时的暂时解决办法
- Android调用WI-FI的核心代码
- 各个进制之间的转换
- HDU--杭电--4553--约会安排--线段树
- Socket心跳包机制
- Android中Message传递参数
- 当前模式读与一致性读(自我改版)
- Opencv2系列学习笔记5(检测Harris角点)
- Python操作泄露的QQ群数据库
- gitlab使用