I tried to creating wifi manager from Dialogfragment.However, I can not pass a problem that Receiver not registered.
private BroadcastReceiver mReceiver;
Then, I'm creating a method is that:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
//gets Wifi instances
mWifiManager = (WifiManager) getActivity().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
//wifi state control
if (!mWifiManager.isWifiEnabled()) {
Toast.makeText(getActivity(), "Wifi enabled", Toast.LENGTH_LONG).show();
mWifiManager.setWifiEnabled(true);
}
//register BroadCast
getActivity().getApplicationContext().registerReceiver(mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent ıntent) {
mScanResults = mWifiManager.getScanResults();
size = mScanResults.size();
Log.d("WiFi", "Total Wifi NetWork ======= > " + size);
}
}, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
/* checkandAskPermission();*/
} else {
mWifiManager.startScan();
values.clear();
try {
size = size - 1;
while (size >= 0) {
device d = new device();
d.setName(mScanResults.get(size).SSID.toString());
d.setBssIdName(mScanResults.get(size).BSSID.toString());
values.add(d);
mWifiScanAdapter.notifyDataSetChanged();
size = size - 1;
}
} catch (Exception e) {
Log.d("Wifi", "Wifi-------------->" + e.getMessage());
}
btnScan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mWifiManager.startScan();
values.clear();
try {
size = size - 1;
while (size >= 0) {
device d = new device();
d.setName(mScanResults.get(size).SSID.toString());
d.setBssIdName(mScanResults.get(size).BSSID.toString());
values.add(d);
mWifiScanAdapter.notifyDataSetChanged();
size = size - 1;
}
} catch (Exception e) {
Log.d("Wifi", e.getMessage());
}
}
});
}
}
then It's the issue here when the device rotates, DiolagFragment restarts and I clicked the button again or not state save that can check to Wi-Fi state again and I clicked the button again too.not good but not too bad, later I used this method:
@Override
public void onDestroy() {
super.onDestroy();
getActivity().unregisterReceiver(mReceiver);
}
I get the error that Receiver not registered. or I'm setting setRetainInstance(true) in onCreateView after I deleted setRetainInstance(true) from onActivityCreated method.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.wifisdialog, container, false);
setRetainInstance(true);
btnScan = (Button) view.findViewById(R.id.wifiScan);
mRecyclerView = (RecyclerView) view.findViewById(R.id.adam_wifi_recycle);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mWifiScanAdapter = new WifiScanAdapter(values, getContext());
mRecyclerView.setAdapter(mWifiScanAdapter);
this.getDialog().setTitle("Wifi Manager");
return view;
}
then I have set the unregisterReceiver as you can see:
@Override
public void onDestroyView() {
super.onDestroyView();
getActivity().unregisterReceiver(mReceiver);
}
I still get the error that Receiver not registered.
And, in MainActivity is:
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome_page);
new WifiCheck().execute();
}
public class WifiCheck extends AsyncTask<Void, Void, Void> {
ProgressDialog pdLoading = new ProgressDialog(HomePageLRG.this);
@Override
protected Void doInBackground(Void... voids) {
new WifiDialogFragment().show(getSupportFragmentManager(), "Wifi Manager");
return null;
}
@Override
protected void onPreExecute() {
pdLoading.setMessage("\tLoading...");
pdLoading.show();
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
pdLoading.dismiss();
}
}
Okay, That's run perfectly when I click the button, I get all the Wifi features the way I want.Exactly in the local area. But I want to that: if the dialog box is open and show wifi in the local area then device rotates, not changing or if the dialog box (dialogfragment, whatever you say) is not open then device rotates, not changing.
I think it is that possible but I can not found, Can someone help me? and I'm sorry for my bad english.