Quick question, I've got a method where an Alert Dialog is built and want to call it when it detects that WIFI or GPS on the android device is not enabled.
So essentially:
If: The device does not have wifi or GPS //Don't know how this code is { Built the alert dialog } Else: { Don't worry and do nothing }
Any Ideas?
My code:
public void checkPhoneStatus(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("Warning");
alertDialog.setMessage("We've noticed your WIFI and GPS are off. Are you okay? Do you want to call the Authorities?");
alertDialog.setIcon(R.drawable.about);
alertDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent callIntent = new Intent(Intent.ACTION_CALL); //Runs the intent - starts the phone call
callIntent.setData(Uri.parse("tel:77777777")); //Number set for the phone call
startActivity(callIntent); //Start the intent
}
});
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "Please go to Settings and enable WIFI or GPS!",
Toast.LENGTH_SHORT).show();
}
});
alertDialog.show();
}
You can use this to check if WiFi is enabled:
public boolean isWiFiEnabled(Context context) {
WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
return wifi.isWifiEnabled();
}
To check if GPS is enabled you can use:
public boolean isGPSEnabled (Context context){
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
EDIT: I'd use it this way:
if (!isWiFiEnabled(this) || !isGPSEnabled(this)) {
displayConnectivityProblemDialog();
}
Please note that I've renamed your checkPhoneStatus()
method to displayConnectivityProblemDialog()
. Not necessary, but a matter of taste :)