AlertDialog If/Else 检查系统服务?
AlertDialog If/Else to check against system services?
快速提问,我有一个方法可以构建警报对话框,当它检测到 android 设备上的 WIFI 或 GPS 未启用时,我想调用它。
所以基本上:
如果:设备没有wifi或GPS //不知道这个代码是怎么来的
{
构建警报对话框
}
别的:
{
别担心,什么都不做
}
有什么想法吗?
我的代码:
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();
}
设计听起来不准确。
我会做什么:
1) 有一个后台进程检查 internet/wifi 是否已启动,每 n 秒执行一次 ping(即;n = 120)
2) 如果 ping 失败,然后显示对话框。
并且只有ui在您知道您想要显示它之前显示对话框。
您需要将业务逻辑与 ui 分开;它会带来更简洁的设计。
您可以使用它来检查 WiFi 是否已启用:
public boolean isWiFiEnabled(Context context) {
WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
return wifi.isWifiEnabled();
}
要检查 GPS 是否已启用,您可以使用:
public boolean isGPSEnabled (Context context){
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
编辑:我会这样使用它:
if (!isWiFiEnabled(this) || !isGPSEnabled(this)) {
displayConnectivityProblemDialog();
}
请注意,我已将您的 checkPhoneStatus()
方法重命名为 displayConnectivityProblemDialog()
。没必要,但个人喜好问题:)
快速提问,我有一个方法可以构建警报对话框,当它检测到 android 设备上的 WIFI 或 GPS 未启用时,我想调用它。
所以基本上:
如果:设备没有wifi或GPS //不知道这个代码是怎么来的 { 构建警报对话框 } 别的: { 别担心,什么都不做 }
有什么想法吗?
我的代码:
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();
}
设计听起来不准确。
我会做什么:
1) 有一个后台进程检查 internet/wifi 是否已启动,每 n 秒执行一次 ping(即;n = 120)
2) 如果 ping 失败,然后显示对话框。
并且只有ui在您知道您想要显示它之前显示对话框。
您需要将业务逻辑与 ui 分开;它会带来更简洁的设计。
您可以使用它来检查 WiFi 是否已启用:
public boolean isWiFiEnabled(Context context) {
WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
return wifi.isWifiEnabled();
}
要检查 GPS 是否已启用,您可以使用:
public boolean isGPSEnabled (Context context){
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
编辑:我会这样使用它:
if (!isWiFiEnabled(this) || !isGPSEnabled(this)) {
displayConnectivityProblemDialog();
}
请注意,我已将您的 checkPhoneStatus()
方法重命名为 displayConnectivityProblemDialog()
。没必要,但个人喜好问题:)