3
我想獲得範圍內的所有路由器,並顯示它在列表視圖,但面臨的問題在Android n及以上。 我能夠得到所有的路由器,並在Android 5棒棒糖和低於棒棒糖列表視圖中顯示它,但它不適用於Android N和以上。 我正在做它的碎片。無法獲取路由器在範圍內的Android N
這是我的代碼。
public class Add_Router extends android.support.v4.app.Fragment {
public Add_Router() {
}
ArrayList<AR_Router> ar_router;
WifiManager mWifiManager;
ListView lv;
ProgressDialog pd;
String adm_token;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.frag_addrouter, container, false);
setHasOptionsMenu(true);
adm_token = getArguments().getString("adm_token");
pd = new ProgressDialog(getActivity(), R.style.pdtheme);
pd.setCancelable(false);
pd.setProgressStyle(android.R.style.Widget_ProgressBar_Small);
ar_router = new ArrayList<>();
lv = (ListView) v.findViewById(R.id.lv);
//get all wifi devices
pd.show();
mWifiManager = (WifiManager) getActivity().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
getActivity().registerReceiver(mWifiScanReceiver,
new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
mWifiManager.startScan();
return v;
}
private final BroadcastReceiver mWifiScanReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context c, Intent intent) {
try{
if (intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
List<ScanResult> mScanResults = mWifiManager.getScanResults();
if (!ar_router.isEmpty()) {
ar_router.clear();
}
for (int i = 0; i < mScanResults.size(); i++) {
String router_name = mScanResults.get(i).SSID.toString();
String router_mac = mScanResults.get(i).BSSID.toString();
Log.d("router : ","rout:"+router_name);
AR_Router router = new AR_Router();
router.arString_routerName = router_name;
router.arString_routerMac = router_mac;
ar_router.add(router);
}
CustomAdapter ca = new CustomAdapter(getActivity(), R.layout.cstm_router, R.id.tv_router_Name, ar_router);
lv.setAdapter(ca);
pd.dismiss();
}}catch (Exception e){
e.printStackTrace();
}
}
};
這是我的自定義適配器。
class CustomAdapter extends ArrayAdapter<AR_Router> {
public CustomAdapter(Context context, int resource, int textViewResourceId, ArrayList<AR_Router> objects) {
super(context, resource, textViewResourceId, objects);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View v = inflater.inflate(R.layout.cstm_router, parent, false);
final TextView router_name = (TextView) v.findViewById(R.id.tv_router_Name);
final TextView router_mac = (TextView) v.findViewById(R.id.router_mac);
AR_Router ar_rout = ar_router.get(position);
router_name.setText(ar_rout.arString_routerName);
router_mac.setText(ar_rout.arString_routerMac);
return v;
}
}
這裏是數組類
//array class
private class AR_Router {
String arString_routerName;
String arString_routerMac;
}
嘗試使用位置ON。打開您的位置,然後嘗試。 –
嘗試添加棉花糖位置權限 –
很棒。它通過添加ACCESS_FINE_LOCATION權限來工作。非常感謝@SardarKhan –