[Android] 掃描附近WIFI

import java.util.List;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class Wifi3Activity extends Activity {
    /** Called when the activity is first created. */
	private TextView wifiText;  
    private WifiManager wifiManager;  
    private StringBuilder sb;  
    private List<ScanResult> wifiList;  
    private WifiReceiver wifiReceiver; 
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        sb = new StringBuilder();  
        wifiText = (TextView) findViewById(R.id.wifi);  
        wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);  
        wifiReceiver = new WifiReceiver();  
        registerReceiver(wifiReceiver, new IntentFilter(  
                WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));  
        wifiManager.startScan();  
        wifiText.setText("\nStarting Scan\n");
        
        
    }
    
    @Override  
    public boolean onCreateOptionsMenu(Menu menu) {  
        menu.add(0, 0, 0, "Refresh");  
        return super.onCreateOptionsMenu(menu);  
    }
    
    @Override  
    public boolean onMenuItemSelected(int featureId, MenuItem item) {  
        wifiManager.startScan();  
        wifiText.setText("Starting Scan");  
        return super.onMenuItemSelected(featureId, item);  
  
    }  
  
    @Override  
    protected void onPause() {  
        unregisterReceiver(wifiReceiver);  
        super.onPause();  
    }  
      
    @Override  
    protected void onResume() {  
        registerReceiver(wifiReceiver, new IntentFilter(  
                WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));  
        super.onResume();  
    }  
      
    private final class WifiReceiver extends BroadcastReceiver {  
        @Override  
        public void onReceive(Context context, Intent intent) {  
            sb = new StringBuilder();  
            wifiList = wifiManager.getScanResults();  
            for (int i = 0; i < wifiList.size(); i++) {  
                sb.append(new Integer(i + 1).toString() + ".");  
                sb.append((wifiList.get(i)).toString()).append("\n\n");  
            }  
            wifiText.setText(sb.toString());  
        }  
    } 
    
    
}

 

布局文件

 

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<LinearLayout 
    android:orientation="vertical"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    >  
<TextView   
    android:id="@+id/wifi"  
    android:layout_width="fill_parent"   
    android:layout_height="wrap_content"   
    android:text="@string/hello"  
    />  
</LinearLayout>
</ScrollView>

 

Manifest

 

 

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="wifi3.com"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".Wifi3Activity"
            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.ACCESS_WIFI_STATE"></uses-permission>
   <uses-permission android:name="android.permission.ACCESS_CHECKIN_PROPERTIES"></uses-permission>  
   <uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>  
   <uses-permission android:name="android.permission.INTERNET"></uses-permission>  
   <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>  
   <uses-permission android:name="android.permission.MODIFY_PHONE_STATE"></uses-permission> 
</manifest>

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *