如何在 Android 中一次又一次地从 wifi 列表中选择相同的 wifi 运营商?
How to get selected same wifi operator from wifi List again and again in Android?
在我的项目中,我有一个 Map.First 我去 Wifi 路由器位置,我扫描 wifi 列表和 select Operator2 并标记 it.Next 我去另一个位置收集与之前的 Operator2 细节相同(不要),然后我去另一个位置再重复一遍。
我可以第一次 select Wifi Operator.Next 时间 我不知道如何锁定特定的先前 selected 运营商详细信息并获取运营商详细信息 again.So 请帮我解决这个问题。
我的代码:
public class WifiReceiver extends BroadcastReceiver {
private WifiManager wifiManager;
private PlanMapperActivity viewerActivity;
private Context newContext;
private String operator;
private String macAddress;
private int signalStrength;
private String wifiMode;
private int frequency;
private String htMode;
private String security;
private int channelNumber;
private AlertDialog wifiAlertDialog;
private ListView wifiListView;
private ProgressDialog progress;
private Boolean checkWifiSelected;
private Boolean checkServayStart;
private String operatorName;
List<ScanResult> wifiSRList;
private static final String WPA2 = "WPA2";
private static final String WEP = "WEP";
private static final String EAP = "EAP";
private static final String STORED_OPERATOR = "com.kenturf.wifiheatmap.SELECTED_OPERATOR";
private int requiredLevel;
private int ssidCount;
public WifiReceiver(Context ctx,PlanMapperActivity planMapper) {
this.viewerActivity = planMapper;
this.newContext = ctx;
}
public WifiReceiver(WifiManager myWifiManager,ProgressDialog wifiProgress,Boolean isWifiSelected,Boolean isSurveyStart) {
this.wifiManager = myWifiManager;
this.progress = wifiProgress;
this.checkWifiSelected = isWifiSelected;
this.checkServayStart = isSurveyStart;
}
@Override
public void onReceive(final Context context, Intent intent) {
wifiSRList = wifiManager.getScanResults();
if (wifiSRList.size() == 0) {
Toast.makeText(context,"wifi List 0",Toast.LENGTH_SHORT).show();
}
if (checkWifiSelected) {
LayoutInflater wifiLayout = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View wifiView = wifiLayout.inflate(R.layout.dialog_fragment_wifi,null);
AlertDialog.Builder wifiDialog = new AlertDialog.Builder(context);
wifiDialog.setCancelable(false);
wifiDialog.setView(wifiView);
wifiAlertDialog = wifiDialog.create();
wifiListView = (ListView)wifiView.findViewById(R.id.user_wifi_detail);
}
Collections.sort(wifiSRList, new Comparator<ScanResult>() {
@Override
public int compare(ScanResult lhs, ScanResult rhs) {
return (lhs.level > rhs.level ? -1 : (lhs.level == rhs.level ? 0 : 1));
}
});
if (checkWifiSelected) {
String[] wifiListString = new String[wifiSRList.size()];
for (int i = 0; i < wifiSRList.size(); i++) {
wifiListString[i] = (wifiSRList.get(i).SSID);
}
wifiListView.setAdapter(new ArrayAdapter<>(context, android.R.layout.simple_list_item_1, wifiListString));
wifiAlertDialog.show();
progress.dismiss();
wifiListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
operator = wifiSRList.get(position).SSID;
macAddress = wifiSRList.get(position).BSSID;
signalStrength = wifiSRList.get(position).level;
frequency = wifiSRList.get(position).frequency;
final String cap = wifiSRList.get(position).capabilities;
final String[] securityModes = {WEP, WPA2, EAP};
for (int i = securityModes.length - 1; i >= 0; i--) {
if (cap.contains(securityModes[i])) {
security = securityModes[i];
}
}
setOperator(operator);
GetSetClass.wifiOperator = operator;
/* error start ..cannot resolved method getPreferences() */
SharedPreferences sharedPref = context.getPreferences(Context.MODE_PRIVATE); // error line
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString(STORED_OPERATOR, operator);
editor.apply();
/* error end */
operatorName = operator;
setMacAddress(macAddress);
setSignalStrength(signalStrength);
setFrequency(frequency);
setSecurity(security);
setChannelNumber(convertFrequencyToChannel(frequency));
wifiAlertDialog.dismiss();
checkWifiSelected = false;
}
});
}
if(checkServayStart) {
/* error start ..cannot resolved method getPreferences()*/
SharedPreferences shPref = context.getPreferences(Context.MODE_PRIVATE); // error line
String savedOperator = shPref.getString(STORED_OPERATOR,null);
Log.e("operator : ", "saved operator is : " + savedOperator);
/* error end */
if (wifiSRList != null) {
ssidCount = wifiSRList.size();
for(int i = wifiSRList.size() - 1; i >= 0; i--) {
if (GetSetClass.wifiOperator.equals(wifiSRList.get(i).SSID)) {
String requiredOperator = wifiSRList.get(i).SSID;
requiredLevel = wifiSRList.get(i).level;
context.unregisterReceiver(this);
AlertDialog.Builder myBuilder = new AlertDialog.Builder(context);
setRequiredLevel(requiredLevel);
myBuilder.setTitle("Current Signal");
myBuilder.setMessage("Operator : " + requiredOperator + " \n\nSignal Strength : " + requiredLevel + " dBm");
myBuilder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
myBuilder.show();
} else {
Toast.makeText(context,"Operator Mismatch",Toast.LENGTH_SHORT).show();
}
}
progress.dismiss();
}
}
}
public static int convertFrequencyToChannel(int freq) {
if (freq >= 2412 && freq <= 2484) {
return (freq - 2412) / 5 + 1;
} else if (freq >= 5170 && freq <= 5825) {
return (freq - 5170) / 5 + 34;
} else {
return -1;
}
}
}
更新的答案:
private static final String STORED_FILE = "com.package.name.SELECTED_FILE";
private static final String STORED_OPERATOR = "com.package.name.SELECTED_OPERATOR";
将数据保存到 SharedPreferences :
SharedPreferences sharedPref = context.getSharedPreferences(STORED_FILE,Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString(STORED_OPERATOR, operator);
editor.apply();
从 SharedPreferences 获取数据:
SharedPreferences shPref = context.getSharedPreferences(STORED_FILE,Context.MODE_PRIVATE);
String savedOperator = shPref.getString(STORED_OPERATOR,null);
Log.e("operator : ", "saved operator is : " + savedOperator);
如果我没理解错的话,您需要的是一种机制,用于保留用户之前选择的 WiFi 网络(如果有的话)。
您可以使用 SharedPreferences
来实现。
在您的 onReceive
方法的开头,您查询应用的共享首选项以查找任何已保存的 WiFi SSID:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
String savedSsid = sharedPref.getString("saved_wifi_ssid", null);
如果 savedSsid 不为空,则用户之前选择了 WiFi,您可以跳过显示对话框:
if (savedSsid != null) {
// Do whatever you need to do with the stored SSID.
// Return from onReceive to avoid displaying your dialog.
return;
}
您需要做的最后一件事是将所选的 WiFi SSID 存储为您 onItemClick
:
的一部分
wifi_SSID = wifiList.get(position).SSID;
// ... other code ...
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("saved_wifi_ssid", wifi_SSID);
editor.commit();
有关 SharedPreferences 的更多信息,请参阅 the Android Developer Guide。
更新
对于您的特定用例,即 BroadcastReceiver
作为独立 class 存在,您需要通过 Context
实例访问 SharedPreferences,您在 onReceive
像这样:
SharedPreferences sharedPref = context.getSharedPreferences("name_for_your_shared_preferences_file", Context.MODE_PRIVATE);
此行应替换以下两行:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
请参阅 documentation 获取 getSharedPreferences(String, int)。
在我的项目中,我有一个 Map.First 我去 Wifi 路由器位置,我扫描 wifi 列表和 select Operator2 并标记 it.Next 我去另一个位置收集与之前的 Operator2 细节相同(不要),然后我去另一个位置再重复一遍。
我可以第一次 select Wifi Operator.Next 时间 我不知道如何锁定特定的先前 selected 运营商详细信息并获取运营商详细信息 again.So 请帮我解决这个问题。
我的代码:
public class WifiReceiver extends BroadcastReceiver {
private WifiManager wifiManager;
private PlanMapperActivity viewerActivity;
private Context newContext;
private String operator;
private String macAddress;
private int signalStrength;
private String wifiMode;
private int frequency;
private String htMode;
private String security;
private int channelNumber;
private AlertDialog wifiAlertDialog;
private ListView wifiListView;
private ProgressDialog progress;
private Boolean checkWifiSelected;
private Boolean checkServayStart;
private String operatorName;
List<ScanResult> wifiSRList;
private static final String WPA2 = "WPA2";
private static final String WEP = "WEP";
private static final String EAP = "EAP";
private static final String STORED_OPERATOR = "com.kenturf.wifiheatmap.SELECTED_OPERATOR";
private int requiredLevel;
private int ssidCount;
public WifiReceiver(Context ctx,PlanMapperActivity planMapper) {
this.viewerActivity = planMapper;
this.newContext = ctx;
}
public WifiReceiver(WifiManager myWifiManager,ProgressDialog wifiProgress,Boolean isWifiSelected,Boolean isSurveyStart) {
this.wifiManager = myWifiManager;
this.progress = wifiProgress;
this.checkWifiSelected = isWifiSelected;
this.checkServayStart = isSurveyStart;
}
@Override
public void onReceive(final Context context, Intent intent) {
wifiSRList = wifiManager.getScanResults();
if (wifiSRList.size() == 0) {
Toast.makeText(context,"wifi List 0",Toast.LENGTH_SHORT).show();
}
if (checkWifiSelected) {
LayoutInflater wifiLayout = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View wifiView = wifiLayout.inflate(R.layout.dialog_fragment_wifi,null);
AlertDialog.Builder wifiDialog = new AlertDialog.Builder(context);
wifiDialog.setCancelable(false);
wifiDialog.setView(wifiView);
wifiAlertDialog = wifiDialog.create();
wifiListView = (ListView)wifiView.findViewById(R.id.user_wifi_detail);
}
Collections.sort(wifiSRList, new Comparator<ScanResult>() {
@Override
public int compare(ScanResult lhs, ScanResult rhs) {
return (lhs.level > rhs.level ? -1 : (lhs.level == rhs.level ? 0 : 1));
}
});
if (checkWifiSelected) {
String[] wifiListString = new String[wifiSRList.size()];
for (int i = 0; i < wifiSRList.size(); i++) {
wifiListString[i] = (wifiSRList.get(i).SSID);
}
wifiListView.setAdapter(new ArrayAdapter<>(context, android.R.layout.simple_list_item_1, wifiListString));
wifiAlertDialog.show();
progress.dismiss();
wifiListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
operator = wifiSRList.get(position).SSID;
macAddress = wifiSRList.get(position).BSSID;
signalStrength = wifiSRList.get(position).level;
frequency = wifiSRList.get(position).frequency;
final String cap = wifiSRList.get(position).capabilities;
final String[] securityModes = {WEP, WPA2, EAP};
for (int i = securityModes.length - 1; i >= 0; i--) {
if (cap.contains(securityModes[i])) {
security = securityModes[i];
}
}
setOperator(operator);
GetSetClass.wifiOperator = operator;
/* error start ..cannot resolved method getPreferences() */
SharedPreferences sharedPref = context.getPreferences(Context.MODE_PRIVATE); // error line
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString(STORED_OPERATOR, operator);
editor.apply();
/* error end */
operatorName = operator;
setMacAddress(macAddress);
setSignalStrength(signalStrength);
setFrequency(frequency);
setSecurity(security);
setChannelNumber(convertFrequencyToChannel(frequency));
wifiAlertDialog.dismiss();
checkWifiSelected = false;
}
});
}
if(checkServayStart) {
/* error start ..cannot resolved method getPreferences()*/
SharedPreferences shPref = context.getPreferences(Context.MODE_PRIVATE); // error line
String savedOperator = shPref.getString(STORED_OPERATOR,null);
Log.e("operator : ", "saved operator is : " + savedOperator);
/* error end */
if (wifiSRList != null) {
ssidCount = wifiSRList.size();
for(int i = wifiSRList.size() - 1; i >= 0; i--) {
if (GetSetClass.wifiOperator.equals(wifiSRList.get(i).SSID)) {
String requiredOperator = wifiSRList.get(i).SSID;
requiredLevel = wifiSRList.get(i).level;
context.unregisterReceiver(this);
AlertDialog.Builder myBuilder = new AlertDialog.Builder(context);
setRequiredLevel(requiredLevel);
myBuilder.setTitle("Current Signal");
myBuilder.setMessage("Operator : " + requiredOperator + " \n\nSignal Strength : " + requiredLevel + " dBm");
myBuilder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
myBuilder.show();
} else {
Toast.makeText(context,"Operator Mismatch",Toast.LENGTH_SHORT).show();
}
}
progress.dismiss();
}
}
}
public static int convertFrequencyToChannel(int freq) {
if (freq >= 2412 && freq <= 2484) {
return (freq - 2412) / 5 + 1;
} else if (freq >= 5170 && freq <= 5825) {
return (freq - 5170) / 5 + 34;
} else {
return -1;
}
}
}
更新的答案:
private static final String STORED_FILE = "com.package.name.SELECTED_FILE";
private static final String STORED_OPERATOR = "com.package.name.SELECTED_OPERATOR";
将数据保存到 SharedPreferences :
SharedPreferences sharedPref = context.getSharedPreferences(STORED_FILE,Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString(STORED_OPERATOR, operator);
editor.apply();
从 SharedPreferences 获取数据:
SharedPreferences shPref = context.getSharedPreferences(STORED_FILE,Context.MODE_PRIVATE);
String savedOperator = shPref.getString(STORED_OPERATOR,null);
Log.e("operator : ", "saved operator is : " + savedOperator);
如果我没理解错的话,您需要的是一种机制,用于保留用户之前选择的 WiFi 网络(如果有的话)。
您可以使用 SharedPreferences
来实现。
在您的 onReceive
方法的开头,您查询应用的共享首选项以查找任何已保存的 WiFi SSID:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
String savedSsid = sharedPref.getString("saved_wifi_ssid", null);
如果 savedSsid 不为空,则用户之前选择了 WiFi,您可以跳过显示对话框:
if (savedSsid != null) {
// Do whatever you need to do with the stored SSID.
// Return from onReceive to avoid displaying your dialog.
return;
}
您需要做的最后一件事是将所选的 WiFi SSID 存储为您 onItemClick
:
wifi_SSID = wifiList.get(position).SSID;
// ... other code ...
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("saved_wifi_ssid", wifi_SSID);
editor.commit();
有关 SharedPreferences 的更多信息,请参阅 the Android Developer Guide。
更新
对于您的特定用例,即 BroadcastReceiver
作为独立 class 存在,您需要通过 Context
实例访问 SharedPreferences,您在 onReceive
像这样:
SharedPreferences sharedPref = context.getSharedPreferences("name_for_your_shared_preferences_file", Context.MODE_PRIVATE);
此行应替换以下两行:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
请参阅 documentation 获取 getSharedPreferences(String, int)。