我按照here
提出的方案我制作了一些应该在主窗口中找回网络的代码
这是搜索并返回WiFi网络的代码(我在网上发现并稍微修改了一下)
我必须提到我是Android中的新手,但我想学习,我盯着读取Android的傻瓜,我可能在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>
答案 0 :(得分:1)
确保你有
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
在manifest.xml
当然,请确保您在网络设备上进行测试,并启用了WiFi。
答案 1 :(得分:0)
您提供给Activity
的xml文件并非专为此而设计。
您需要提供包含RelativeLayout
,LinearLayout
等的xml布局文件...
阅读http://developer.android.com/training/basics/firstapp/building-ui.html