我正在制作这里提到的程序
我做了一些代码,应该在主窗口中返回找到的网络
这是搜索并返回 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>