【发布时间】:2017-05-31 16:43:59
【问题描述】:
如果网络或 GPS 被禁用,我想在我的 android 应用程序中显示警报对话框,这怎么可能。请帮助我,我是 android 新手
【问题讨论】:
标签: android android-gps
如果网络或 GPS 被禁用,我想在我的 android 应用程序中显示警报对话框,这怎么可能。请帮助我,我是 android 新手
【问题讨论】:
标签: android android-gps
您需要使用广播接收器来更改网络状态和 gps 连接状态,请参阅:Check INTENT internet connection 和 How can I check the current status of the GPS receiver?
【讨论】:
测试是否启用 GPS 的函数(布尔返回)是:
String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(provider.contains("gps")) {}
确定是否启用网络(包括对话框):
ConnectivityManager conMgr = ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = conMgr.getActiveNetworkInfo();
if (netInfo == null){
Description.setVisibility(View.INVISIBLE);
new AlertDialog.Builder(WelcomePage.this)
.setTitle(getResources().getString(R.string.app_name))
.setMessage(getResources().getString(R.string.internet_error))
.setPositiveButton("OK", null).show();
}else{
dialog = ProgressDialog.show(WelcomePage.this, "", "Loading...", true,false);
new Welcome_Page().execute();
}
Android - Programmatically check internet connection and display dialog if notConnected
【讨论】: