Elérhető wifi-k listája Androidon

Elérhető wifi-k listája Androidon
2019-06-02T13:00:37+02:00
2019-06-02T14:14:35+02:00
2022-10-15T21:35:35+02:00
bancza
Sziasztok!

Szeretném kilistázni az elérhető wifihálózatokat. Ezzel a lehető legegyszerübb letöltött példával.:

public class WifiDemo extends Activity implements View.OnClickListener { WifiManager wifi; ListView lv; TextView textStatus; Button buttonScan; int size = 0; List<ScanResult> results; String ITEM_KEY = "key"; ArrayList<HashMap<String, String>> arraylist = new ArrayList<HashMap<String, String>>(); SimpleAdapter adapter; BroadcastReceiver receiver; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.wifidemo); textStatus = (TextView) findViewById(R.id.textStatus); buttonScan = (Button) findViewById(R.id.buttonScan); buttonScan.setOnClickListener(this); lv = (ListView)findViewById(R.id.list); wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE); if (wifi.isWifiEnabled() == false) { Toast.makeText(getApplicationContext(), "wifi is disabled..making it enabled", Toast.LENGTH_LONG).show(); wifi.setWifiEnabled(true); } this.adapter = new SimpleAdapter(WifiDemo.this, arraylist, R.layout.row, new String[] { ITEM_KEY }, new int[] { R.id.list_value }); lv.setAdapter(this.adapter); receiver = new BroadcastReceiver() { @Override public void onReceive(Context c, Intent intent) { results = wifi.getScanResults(); size = results.size(); } }; registerReceiver( receiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); } public void onClick(View view) { arraylist.clear(); wifi.startScan(); try { Toast.makeText(this, "Scanning...." + size, Toast.LENGTH_SHORT).show(); size = size - 1; while (size >= 0) { HashMap<String, String> item = new HashMap<String, String>(); item.put(ITEM_KEY, results.get(size).SSID + " " + results.get(size).capabilities); arraylist.add(item); size--; adapter.notifyDataSetChanged(); } } catch (Exception e){ } } }
AndroidManifest.xml részlet:

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

A BroadcastReceiver onReceive metódusa soha nem fut le. Nem tudom miért?
Mutasd a teljes hozzászólást!
Realtime request petmission kene hozza android m ota

ACCESS_COARSE_LOCATION
private static final int PERMISSIONS_REQUEST_CODE_ACCESS_COARSE_LOCATION = 1001;

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){
        requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSIONS_REQUEST_CODE_ACCESS_COARSE_LOCATION); 
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,
                                       int[] grantResults) {
    if (requestCode == PERMISSIONS_REQUEST_CODE_ACCESS_COARSE_LOCATION
            && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        // TODO: What you want to do when it works or maybe .PERMISSION_DENIED if it works better
    }
}

Es a manifestbe is
Mutasd a teljes hozzászólást!

Tetszett amit olvastál? Szeretnél a jövőben is értesülni a hasonló érdekességekről?
abcd