gpt4 book ai didi

android - Wifi扫码

转载 作者:行者123 更新时间:2023-11-29 18:18:17 24 4
gpt4 key购买 nike

这是我的代码:

package hu.wifx.droid;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
import java.util.List;
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;

public class WifXActivity extends Activity {

TextView mText;
WifiManager mWifi;
List<ScanResult> wifiList;
StringBuilder _sb = new StringBuilder();


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button btn1 = (Button) findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View arg0)
{
// TODO Auto-generated method stub
StartTester();
}
});
}

public void StartTester()
{
IntentFilter i = new IntentFilter();
i.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);

mText = (TextView) findViewById(R.id.mainText);
mWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
registerReceiver(new BroadcastReceiver(){
public void onReceive(Context c, Intent i) {
WifiManager w = (WifiManager)c.getSystemService(Context.WIFI_SERVICE);
wifiList = w.getScanResults();

_sb = new StringBuilder();

for(int m = 0; m < wifiList.size(); m++)
{
_sb.append((wifiList.get(m)).toString());
_sb.append("\\n");
}

mText.setText(_sb);
}
}, i);

WifiManager WM = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WM.setWifiEnabled(true);
WM.startScan();
}
}

没有错误,但是当我启动模拟器并点击 button1:The application has意外地停了下来。为什么?

最佳答案

请检查您是否在 list 中添加了这些权限:

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

关于android - Wifi扫码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6967397/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com