如何检查是否已启用定位服务? [英] How to check if Location Services are enabled?
本文介绍了如何检查是否已启用定位服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发Android操作系统上的应用程序。我不知道如何检查,如果位置服务都启用。
I'm developing an app on Android OS. I don't know how to check if Location Services are enabled or not.
我需要返回true,如果他们被启用,假,如果没有一个方法(因此在过去的情况下,我可以显示一个对话框,让他们)。
I need a method that returns "true" if they are enabled and "false" if not (so in the last case I can show a dialog to enable them).
推荐答案
您可以使用下面的code,检查GPS提供商和网络提供商是否启用与否。
You can use the below code to check whether gps provider and network providers are enabled or not.
LocationManager lm = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
boolean gps_enabled = false;
boolean network_enabled = false;
try {
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch(Exception ex) {}
try {
network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch(Exception ex) {}
if(!gps_enabled && !network_enabled) {
// notify user
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
dialog.setMessage(context.getResources().getString(R.string.gps_network_not_enabled));
dialog.setPositiveButton(context.getResources().getString(R.string.open_location_settings), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
// TODO Auto-generated method stub
Intent myIntent = new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS);
context.startActivity(myIntent);
//get gps
}
});
dialog.setNegativeButton(context.getString(R.string.Cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
// TODO Auto-generated method stub
}
});
dialog.show();
}
这篇关于如何检查是否已启用定位服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文