Wifi扫描应用程序保持崩溃而不显示主要活动

时间:2013-04-19 11:56:11

标签: android android-wifi

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

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);
}

}

2 个答案:

答案 0 :(得分:0)

确保您已添加

uses-permission android:name =“android.permission.CHANGE_WIFI_STATE” uses-permission android:name =“android.permission.ACCESS_WIFI_STATE”

您的Manifest xml文件中的权限。

答案 1 :(得分: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);
} }

的Manifest.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>