0

我正在制作这里提到的程序

我做了一些代码,应该在主窗口中返回找到的网络

这是搜索并返回 WiFi 网络的代码(我在网上找到并稍作修改)

我必须提到我是Android的大新手,但我想学习,我盯着看Android for dummy,我可能在XML中做错了,所以我把它放在这里,你可以看看有没有其他的错误。如果您需要,我将发送更多更新。

package com.vulisha.settosilent;

import java.util.ArrayList;
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 WifiTester extends Activity {
    TextView mainText,outputText;
    WifiManager mainWifi;
    WifiReceiver receiverWifi;
    public List<ScanResult> wifiList;
    public StringBuilder sb = new StringBuilder();
    public List<String> wifiNetworks = new ArrayList<String>();


    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       mainText = (TextView) findViewById(R.id.TextView1);
       mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
       receiverWifi = new WifiReceiver();
       registerReceiver(receiverWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
       mainWifi.startScan();
       mainText.setText("\\nStarting Scan...\\n");
       outputText.setText(sb);
       for(int i = 0; i < wifiList.size(); i++)
        { wifiNetworks.add((wifiList.get(i)).toString()); }
        mainText.setText(sb);
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, 0, 0, "Refresh");
        return super.onCreateOptionsMenu(menu);
    }

    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        mainWifi.startScan();
        mainText.setText("Starting Scan");
        return super.onMenuItemSelected(featureId, item);
    }

    protected void onPause() {
        unregisterReceiver(receiverWifi);
        super.onPause();
    }

    protected void onResume() {
        registerReceiver(receiverWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
        super.onResume();
    }

    class WifiReceiver extends BroadcastReceiver {
        public void onReceive(Context c, Intent intent) {
            sb = new StringBuilder();
            wifiList = mainWifi.getScanResults();
            for(int i = 0; i < wifiList.size(); i++){
                sb.append(new Integer(i+1).toString() + ".");
                sb.append((wifiList.get(i)).toString());
                sb.append("\\n");
            }

        }
    }
}

这里是 main.XML//// 编辑:它不是 main.xml 它是 fragment_main_dummy.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity$DummySectionFragment" >

    <TextView
            android:id="@+id/section_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    <TextView android:text="@+id/TextView1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"

              android:layout_below="@+id/section_label"
              android:layout_alignLeft="@+id/textView"></TextView>
    <TextView android:text="@+id/wifiList"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"

              android:layout_marginTop="23dp"
              android:layout_below="@+id/section_label"
              android:layout_alignLeft="@+id/section_label"
              android:id="@+id/textView"></TextView>
    <TextView android:text="@+id/wifiNetworks"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"

              android:id="@+id/wifiNetworks"
              android:layout_below="@+id/textView"
              android:layout_alignLeft="@+id/textView"
              android:layout_marginTop="13dp"></TextView>
</RelativeLayout>
4

2 回答 2

1

确保你有

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

在你的manifest.xml

当然,请确保您在启用 WiFi 的网络设备上进行测试。

于 2013-06-14T13:50:26.920 回答
0

您提供给的 xml 文件Activity不是为此而设计的。你需要提供一个xml布局文件RelativeLayoutLinearLayout等等......阅读http://developer.android.com/training/basics/firstapp/building-ui.html

于 2013-06-14T14:24:55.943 回答