频道栏目
首页 > 资讯 > Android > 正文

Android WiFi管理(WIFI_SERVICE)

11-11-25        来源:[db:作者]  
收藏   我要投稿

 

 

 

<?xml version="1.0" encoding="utf-8"?> 

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 

    package="com.entel.research" 

    android:versionCode="1" 

    android:versionName="1.0" > 

 

    <uses-sdk android:minSdkVersion="7" /> 

 

    <application 

        android:icon="@drawable/ic_launcher" 

        android:label="@string/app_name" android:debuggable="true"> 

        <activity 

            android:label="@string/app_name" 

            android:name=".WifiManagerActivity" > 

            <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.ACCESS_WIFI_STATE" /> 

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

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

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

 

</manifest> 

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.entel.research"

    android:versionCode="1"

    android:versionName="1.0" >

 

    <uses-sdk android:minSdkVersion="7" />

 

    <application

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name" android:debuggable="true">

        <activity

            android:label="@string/app_name"

            android:name=".WifiManagerActivity" >

            <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.ACCESS_WIFI_STATE" />

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

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

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

 

</manifest>

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

    android:layout_width="fill_parent" 

    android:layout_height="fill_parent" 

    android:orientation="vertical" > 

 

    <TextView 

        android:layout_width="fill_parent" 

        android:layout_height="wrap_content" 

        android:text="@string/hello" /> 

 

    <Button 

        android:id="@+id/wifiManager_open" 

        android:layout_width="fill_parent" 

        android:layout_height="wrap_content" 

        android:text="打开WiFi" /> 

 

    <Button 

        android:id="@+id/wifiManager_close" 

        android:layout_width="fill_parent" 

        android:layout_height="wrap_content" 

        android:text="关闭WiFi" /> 

 

    <Button 

        android:id="@+id/wifiManager_check" 

        android:layout_width="fill_parent" 

        android:layout_height="wrap_content" 

        android:text="显示WiFi状态" /> 

 

    <Button 

        android:id="@+id/wifiManager_WIFI_SETTINGS" 

        android:layout_width="fill_parent" 

        android:layout_height="wrap_content" 

        android:text="WiFi网络设置" /> 

 

    <Button 

        android:id="@+id/threeGManager_State" 

        android:layout_width="fill_parent" 

        android:layout_height="wrap_content" 

        android:text="显示3G边境状态" /> 

     

    <Button 

        android:id="@+id/wifiManager_WIRELESS_SETTINGS" 

        android:layout_width="fill_parent" 

        android:layout_height="wrap_content" 

        android:text="无线网络配置" /> 

 

</LinearLayout> 

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >

 

    <TextView

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="@string/hello" />

 

    <Button

        android:id="@+id/wifiManager_open"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="打开WiFi" />

 

    <Button

        android:id="@+id/wifiManager_close"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="关闭WiFi" />

www.2cto.com

    <Button

        android:id="@+id/wifiManager_check"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="显示WiFi状态" />

 

    <Button

        android:id="@+id/wifiManager_WIFI_SETTINGS"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="WiFi网络设置" />

 

    <Button

        android:id="@+id/threeGManager_State"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="显示3G边境状态" />

   

    <Button

        android:id="@+id/wifiManager_WIRELESS_SETTINGS"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="无线网络配置" />

 

</LinearLayout>

 

 

 

 

package com.entel.research; 

 

import android.app.Activity; 

import android.content.Context; 

import android.content.Intent; 

import android.net.ConnectivityManager; 

import android.net.NetworkInfo.State; 

import android.net.wifi.WifiManager; 

import android.os.Bundle; 

import android.provider.Settings; 

import android.view.View; 

import android.view.View.OnClickListener; 

import android.widget.Button; 

import android.widget.Toast; 

 

public class WifiManagerActivity extends Activity 

    @Override 

    public void onCreate(Bundle savedInstanceState) 

    { 

        super.onCreate(savedInstanceState); 

        setContentView(R.layout.main); 

 

        Button wifiManager_open = (Button) findViewById(R.id.wifiManager_open); 

        Button wifiManager_close = (Button) findViewById(R.id.wifiManager_close); 

        Button wifiManager_check = (Button) findViewById(R.id.wifiManager_check); 

        Button wifiManager_WIFI_SETTINGS = (Button) findViewById(R.id.wifiManager_WIFI_SETTINGS); 

        Button wifiManager_WIRELESS_SETTINGS = (Button) findViewById(R.id.wifiManager_WIRELESS_SETTINGS); 

         

        Button threeGManager_State = (Button) findViewById(R.id.threeGManager_State); 

 

        final WifiManager wifiManager = (WifiManager) WifiManagerActivity.this 

                .getSystemService(Context.WIFI_SERVICE); 

        final ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 

 

        wifiManager_open.setOnClickListener(new OnClickListener() 

        { 

            public void onClick(View v) 

            { 

                wifiManager.setWifiEnabled(true); 

                Toast.makeText(WifiManagerActivity.this, "Wifi开启", 

                        Toast.LENGTH_SHORT).show(); 

            } 

        }); 

         

        wifiManager_close.setOnClickListener(new OnClickListener() 

        { 

            public void onClick(View v) 

            { 

                if (wifiManager.isWifiEnabled()) 

                { 

                    wifiManager.setWifiEnabled(false); 

                    Toast.makeText(WifiManagerActivity.this, "Wifi关闭", 

                            Toast.LENGTH_SHORT).show(); 

                } 

                Toast.makeText(WifiManagerActivity.this, "Wifi关闭", 

                        Toast.LENGTH_SHORT).show(); 

            } 

        }); 

         

        wifiManager_check.setOnClickListener(new OnClickListener() 

        { 

            public void onClick(View v) 

            { 

                String result = null; 

                switch (wifiManager.getWifiState()) 

                { 

                    case WifiManager.WIFI_STATE_DISABLED: 

                        result = "WIFI已关闭"; 

                        break; 

                    case WifiManager.WIFI_STATE_DISABLING: 

                        result = "WIFI正在关闭中"; 

                        break; 

                    case WifiManager.WIFI_STATE_ENABLED: 

                        result = "WIFI已启用"; 

                        break; 

                    case WifiManager.WIFI_STATE_ENABLING: 

                        result = "WIFI正在启动中"; 

                        break; 

                    case WifiManager.WIFI_STATE_UNKNOWN: 

                        result = "未知WIFI状态"; 

                        break; 

                } 

                Toast.makeText(WifiManagerActivity.this, result, Toast.LENGTH_SHORT) 

                        .show(); 

            } 

        }); 

         

        wifiManager_WIFI_SETTINGS.setOnClickListener(new OnClickListener() 

        { 

            public void onClick(View v) 

            { 

                startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS)); 

            } 

        }); 

         

        threeGManager_State.setOnClickListener(new OnClickListener() 

        { 

            public void onClick(View v) 

            { 

                State mobile = conMan.getNetworkInfo( 

                        ConnectivityManager.TYPE_MOBILE).getState(); 

                Toast.makeText(WifiManagerActivity.this, mobile.toString(), 

                        Toast.LENGTH_SHORT).show(); 

            } 

        }); 

         

        wifiManager_WIRELESS_SETTINGS.setOnClickListener(new OnClickListener() 

        { 

            public void onClick(View v) 

            { 

                startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS)); 

            } 

        }); 

    } 

}   

 

摘自 宇宙神帝

相关TAG标签
上一篇:台积电:绝大多数7nm客户都会转向6nm_IT新闻_博客园
下一篇:最后一页
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站