0

我正在编写一个应用程序来扫描可用的无线网络,但是,当我运行我的应用程序时,它甚至在显示主要活动之前就崩溃了。我也在寻找如何使用扫描结果,但我不确定我使用它的方式是否正确。我阅读了有关广播接收器的信息,但没有找到任何有关如何使用它的示例。这是我的代码,非常感谢任何帮助

package com.example.wizer2;

import java.util.List;

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

public class Wizer extends Activity {

WifiManager Wifi;
BroadcastReceiver receiver;
List<ScanResult> results;


Button bt1; //Current status
TextView t1; //for current status result


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_wizer);

    //GUI

    bt1=(Button)findViewById(R.id.b1);
    t1=(TextView)findViewById(R.id.t1);

     // to enable wifi service
    Wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);

    bt1.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {

            WifiInfo info = Wifi.getConnectionInfo();
            if (info.getBSSID()==null)
            {
                t1.setText("You are currently not connected to any wireless network.\n");
            }
            else
            {
                t1.append("Current Status:\n");
                t1.append("Network Name :"+info.getSSID().toString()+"\n");
                t1.append("RSSI :"+info.getRssi()+"\n");

            }


        }
    });
    Wifi.startScan();
    // Register Broadcast Receiver
    if (receiver == null)
        receiver = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {
                results =Wifi.getScanResults();
//              Intent I1 =new Intent();


            }
        };
        registerReceiver(receiver, new IntentFilter( WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_wizer, menu);
    return true;
}
public void onStop() {
    unregisterReceiver(receiver);
}

}

4

2 回答 2

0

除了在清单文件中添加权限外,我只使用了您的代码而没有更改任何内容。它为我工作检查代码

public class MainActivity extends Activity {  WifiManager Wifi;
BroadcastReceiver receiver;
List<ScanResult> results; @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //GUI
    bt1=(Button)findViewById(R.id.btn1);
    t1=(TextView)findViewById(R.id.txt1);
     // to enable wifi service
    Wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    bt1.setOnClickListener(new View.OnClickListener() {   public void onClick(View arg0) {
            WifiInfo info = Wifi.getConnectionInfo();
            if (info.getBSSID()==null)
            {
                t1.setText("You are currently not connected to any wireless network.\n");
            }
            else
            {
                t1.append("Current Status:\n");
                t1.append("Network Name :"+info.getSSID().toString()+"\n");
                t1.append("RSSI :"+info.getRssi()+"\n");

            }
        }
    });  Wifi.startScan();
    // Register Broadcast Receiver
    if (receiver == null)
        receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                results =Wifi.getScanResults();
    //                Intent I1 =new Intent();
            }
        };
        registerReceiver(receiver, new IntentFilter( WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
}
/*@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_wizer, menu);
    return true;
}*/
public void onStop() {
    unregisterReceiver(receiver);
} }

清单.xml

<?xml version="1.0" encoding="utf-8"?>  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.textoverlay"
android:versionCode="1"
android:versionName="1.0" >  <uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >  <activity
        android:name="com.example.textoverlay.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>

于 2013-04-19T13:10:31.400 回答
0

确保您已添加

使用权限 android:name="android.permission.CHANGE_WIFI_STATE" 使用权限 android:name="android.permission.ACCESS_WIFI_STATE"

清单 xml 文件中的权限。

于 2013-04-19T12:25:37.830 回答