Android: отсканируйте, чтобы получить список WiFi - просмотр списка не открывается

Я сделал программу для разработки приложения для отображения отсканированных точек доступа WIFI. Мое приложение работает без ошибок, но, к сожалению, оно не дает никаких результатов, хотя кажется, что я все сделал. Я прилагаю все свои файлы и был бы признателен, если бы кто-нибудь мог указать, что мне не хватает. Здоровья, ребята!

MainActivity.java

package com.example....;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.TextView;
import android.content.Intent;
import android.content.IntentFilter;
import android.widget.ToggleButton;
import android.view.Menu;
import android.view.MenuItem;
import java.util.List;
import java.util.ArrayList;
import android.net.wifi.ScanResult;

public class MainActivity extends AppCompatActivity {

    ToggleButton toggleButton;
    TextView textView;
    ListView list;
    String len[];

    public WifiManager wifi;
    WifiReceiver receiverWifi;

    StringBuilder sb  = new StringBuilder();
    private final Handler handler = new Handler();
    private BroadcastReceiver receiver;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);


        textView = (TextView) findViewById(R.id.textView);
        list = (ListView)findViewById(R.id.listview);
        receiverWifi = new WifiReceiver();
        wifi.startScan();
        registerReceiver(receiverWifi, new IntentFilter(
                WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
        if(wifi.isWifiEnabled()==false)
        {
            wifi.setWifiEnabled(true);
        }



 // #######################################################################################################################
        toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
        toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
                if (checked) {
                    textView.setText("WiFi is ON");
                    WifiManager wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
                    wifi.setWifiEnabled(true);
                } else {
                    textView.setText("WiFi is OFF");
                    WifiManager wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
                    wifi.setWifiEnabled(false);
                }
            }
        });

        if (toggleButton.isChecked()) {
            textView.setText("WiFi is ON");
            WifiManager wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
            wifi.setWifiEnabled(true);
        } else {
            textView.setText("WiFi is OFF");
            WifiManager wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
            wifi.setWifiEnabled(false);
        }
 // #######################################################################################################################

    }



    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) {

            ArrayList<String> connections=new ArrayList<String>();
            ArrayList<Float> Signal_Strenth= new ArrayList<Float>();
            List<ScanResult> wifiList = wifi.getScanResults();
            wifiList = wifi.getScanResults();

            len = new String[wifiList.size()];

            for(int i = 0; i < wifiList.size(); i++){
                len[i] = ((wifiList.get(i)).toString());
            }

            list.setAdapter(new ArrayAdapter<String>(getApplicationContext(),
                    android.R.layout.activity_list_item,len));







        }
    }
}

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example......"
    tools:layout_editor_absoluteY="81dp"
    tools:layout_editor_absoluteX="0dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="WiFi Network Status"
        android:id="@+id/textView"
        tools:layout_constraintTop_creator="1"
        android:layout_marginStart="16dp"
        android:layout_marginTop="26dp"
        tools:layout_constraintLeft_creator="1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginLeft="16dp" />

    <ToggleButton
        android:id="@+id/toggleButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ToggleButton"
        tools:layout_constraintTop_creator="1"
        tools:layout_constraintRight_creator="1"
        android:layout_marginEnd="16dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginTop="16dp"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginRight="16dp" />

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

        tools:layout_constraintTop_creator="1"
        android:layout_marginStart="16dp"
        android:layout_marginTop="110dp"
        tools:layout_constraintLeft_creator="1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginLeft="16dp" />

    <ListView
        android:id="@+id/listview"
        android:layout_width="0dp"
        android:layout_height="0dp"
        tools:layout_constraintTop_creator="1"
        tools:layout_constraintRight_creator="1"
        tools:layout_constraintBottom_creator="1"
        android:layout_marginStart="15dp"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginEnd="15dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginTop="18dp"
        app:layout_constraintTop_toBottomOf="@+id/toggleButton"
        tools:layout_constraintLeft_creator="1"
        android:layout_marginBottom="20dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp" />

</android.support.constraint.ConstraintLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.....">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
    <uses-permission android:name="android.permisssion.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permisssion.ACCESS_COARSE_LOCATION"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


person Code Pro    schedule 08.08.2017    source источник
comment
попробуйте установить журнал? и поговори со мной результат   -  person Nguyễn Trung Hiếu    schedule 08.08.2017
comment
@NguyễnTrungHiếu Извините, я не понял. Можете расширить?   -  person Code Pro    schedule 08.08.2017
comment
или см. учебник tutorialspoint.com/android/android_wi_fi.htm.   -  person Nguyễn Trung Hiếu    schedule 08.08.2017
comment
Log.i(размер результата %s, wifiList.size+);   -  person Nguyễn Trung Hiếu    schedule 08.08.2017
comment
Мое приложение работает так, но результатов нет. Я написал для него код. Просто искал некоторые указатели по устранению неполадок.   -  person Code Pro    schedule 08.08.2017
comment
Вы уверены, что wifiList.size возвращает 0?   -  person Nguyễn Trung Hiếu    schedule 08.08.2017
comment
Попробуйте посмотреть: stackoverflow.com/questions/5452940/   -  person Nguyễn Trung Hiếu    schedule 08.08.2017


Ответы (1)


Я узнаю твою проблему

изменить android.R.layout.activity_list_item

list.setAdapter(new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.activity_list_item,len));

на android.R.layout.simple_list_item_1

list.setAdapter(new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, len));
person Nguyễn Trung Hiếu    schedule 08.08.2017