android连接wifi教学视频,mars老师[Android开发视频教学]01_23 WIFI网络操作

这篇博客展示了如何在Android平台上通过Java代码控制手机的WiFi功能,包括启动、关闭和检查WiFi网卡的状态。主要涉及Android的WifiManager类及其相关方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://blog.csdn.net/wwj_748/article/details/7746319

35ef3c7fa457e0ce021541d5fdec8598.png

布局文件:main.xml

[html] view plaincopyprint?

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/hello"

/>

android:id="@+id/startWifi"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="启动手机的wifi网卡"

/>

android:id="@+id/stopWifi"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="关闭手机的wifi网卡"

/>

android:id="@+id/checkWifi"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="检查当前wifi网卡状态"

/>

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/hello"

/>

android:id="@+id/startWifi"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="启动手机的wifi网卡"

/>

android:id="@+id/stopWifi"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="关闭手机的wifi网卡"

/>

android:id="@+id/checkWifi"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="检查当前wifi网卡状态"

/>

在manifest文件设置权限:AndroidManifest.xml

[html] view plaincopyprint?

package="mars.wifi" android:versionCode="1" android:versionName="1.0">

package="mars.wifi" android:versionCode="1" android:versionName="1.0">

==>WifiActivity.java

[java] view plaincopyprint?

package mars.wifi;

import android.app.Activity;

import android.content.Context;

import android.net.wifi.WifiManager;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.Toast;

public class WifiActivity extends Activity {

/** Called when the activity is first created. */

private Button startButton = null;

private Button stopButton = null;

private Button checkButton = null;

private WifiManager wifiManager = null;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

startButton = (Button)findViewById(R.id.startWifi);

stopButton = (Button)findViewById(R.id.stopWifi);

checkButton = (Button)findViewById(R.id.checkWifi);

startButton.setOnClickListener(new StartWifiListener());

stopButton.setOnClickListener(new StopWifiListener());

checkButton.setOnClickListener(new CheckWifiListener());

}

class StartWifiListener implements OnClickListener{

@Override

public void onClick(View v) {

wifiManager = (WifiManager)WifiActivity.this.getSystemService(Context.WIFI_SERVICE);

wifiManager.setWifiEnabled(true);

System.out.println("wifi state --->" + wifiManager.getWifiState());

Toast.makeText(WifiActivity.this, "当前Wifi网卡状态为" + wifiManager.getWifiState(), Toast.LENGTH_SHORT).show();

}

}

class StopWifiListener implements OnClickListener{

@Override

public void onClick(View arg0) {

// TODO Auto-generated method stub

wifiManager = (WifiManager)WifiActivity.this.getSystemService(Context.WIFI_SERVICE);

wifiManager.setWifiEnabled(false);

System.out.println("wifi state --->" + wifiManager.getWifiState());

Toast.makeText(WifiActivity.this, "当前Wifi网卡状态为" + wifiManager.getWifiState(), Toast.LENGTH_SHORT).show();

}

}

class CheckWifiListener implements OnClickListener{

@Override

public void onClick(View v) {

wifiManager = (WifiManager)WifiActivity.this.getSystemService(Context.WIFI_SERVICE);

System.out.println("wifi state --->" + wifiManager.getWifiState());

Toast.makeText(WifiActivity.this, "当前Wifi网卡状态为" + wifiManager.getWifiState(), Toast.LENGTH_SHORT).show();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值