第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

wifiManager.getScanResult() 返回空值

wifiManager.getScanResult() 返回空值

我正在搜索以掃描可用的 wifi 網(wǎng)絡(luò),但方法 getScanResults() 返回空列表。我包含了所有需要的權(quán)限:android.permission.ACCESS_COARSE_LOCATIONandroid.permission.CHANGE_WIFI_STATEandroid.permission.ACCESS_FINE_LOCATIONandroid.permission.ACCESS_WIFI_STATE主要活動(dòng)類(lèi)是:public class Home extends Activity {`    Context context;    WifiManager wifiManager = null;    WiFiReceiver wifiReceiver = null;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.home);        context = this;        wifiManager = (WifiManager)         context.getSystemService(Context.WIFI_SERVICE);        wifiReceiver = new WiFiReceiver(wifiManager);        IntentFilter intentFilter = new IntentFilter();        intentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);        registerReceiver(wifiReceiver, intentFilter);        wifiManager.startScan();        List<ScanResult> results = wifiReceiver.results;    }    @Override    protected void onDestroy() {        super.onDestroy();        unregisterReceiver(wifiReceiver);    }}廣播接收器是:public class WiFiReceiver extends BroadcastReceiver {`    public List<ScanResult> results;    private WifiManager wifiManager;    public WiFiReceiver(WifiManager wifiManager) {        this.wifiManager = wifiManager;    }    @Override    public void onReceive(Context context, Intent intent) {        boolean success = intent.getBooleanExtra(WifiManager.EXTRA_RESULTS_UPDATED, false);        if (success) {            results = wifiManager.getScanResults();            Log.e("wiFi Manager", "Done");        } else {            Log.e("wiFi Manager", "Scan failure");        }    }}
查看完整描述

2 回答

?
慕尼黑8549860

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超11個(gè)贊

問(wèn)題是您假設(shè)startScan()會(huì)立即產(chǎn)生結(jié)果,但實(shí)際上它只按照所說(shuō)的進(jìn)行,開(kāi)始掃描。您在被觸發(fā)之前正在訪問(wèn) thenresults變量,這就是為什么它始終為 null(您的日志記錄應(yīng)該確認(rèn)這一點(diǎn))。onReceiveWiFiReceiver

您需要做的是在結(jié)果準(zhǔn)備就緒時(shí)使用回調(diào)來(lái)獲取結(jié)果,就像此處的代碼一樣。請(qǐng)注意onReceive方法調(diào)用scanSuccess()和結(jié)果是如何只在調(diào)用時(shí)訪問(wèn)的,scanSuccess()而不是在調(diào)用后立即訪問(wèn)的startScan()。還要注意他們?nèi)绾瓮ㄟ^(guò)檢查布爾startScan()返回來(lái)檢查開(kāi)始掃描是否真的成功


查看完整回答
反對(duì) 回復(fù) 2022-12-21
?
蝴蝶刀刀

TA貢獻(xiàn)1801條經(jīng)驗(yàn) 獲得超8個(gè)贊

從 API 級(jí)別 23(Android 6.0 Marshmallow)開(kāi)始,我們需要向用戶端請(qǐng)求運(yùn)行時(shí)許可。特別是 ACCESS_FINE_LOCATION。在wifiManager.startScan()之前需要檢查權(quán)限是否被授予,如果沒(méi)有授予權(quán)限,則需要調(diào)用requestPermissions()。


這是一個(gè)例子:


public void startScanningWifi(){if ( Build.VERSION.SDK_INT >= 23){


    if (ActivityCompat.checkSelfPermission(context, Manifest.

            permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED

            && ActivityCompat.checkSelfPermission(context,

            Manifest.permission.ACCESS_COARSE_LOCATION) !=

            PackageManager.PERMISSION_GRANTED ){

        

        requestPermissions(new String[]{

                        Manifest.permission.ACCESS_FINE_LOCATION},

                REQUEST_CODE_ASK_PERMISSIONS);

        Log.i(TAG, "User location NOT ENABLED, waiting for permission");


    }else{

        //Start scanning for wifi

    }}

您還需要在活動(dòng)中包含此方法


    @Override

    public void onRequestPermissionsResult(int requestCode,

                                           String[] permissions, int[] grantResults) {

        switch (requestCode) {

            case REQUEST_CODE_ASK_PERMISSIONS:

                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                //start scanning

                    wifiManager.startScan();

                } else {

                    // Permission for location Denied

                    Toast.makeText( this,"Well cant help you then!" ,

                            Toast.LENGTH_SHORT)

                            .show();

                }

                break;

            default:

                super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        }

    }

也不要忘記在您的活動(dòng)中聲明這一點(diǎn)


private final int REQUEST_CODE_ASK_PERMISSIONS = 1;


查看完整回答
反對(duì) 回復(fù) 2022-12-21
  • 2 回答
  • 0 關(guān)注
  • 512 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)