Android:检查是否启用了Google设置位置

前端之家收集整理的这篇文章主要介绍了Android:检查是否启用了Google设置位置前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_404_0@
我正在开发一款使用Google Play服务的应用.在某些手机上,客户端为位置返回null.发生这种情况是因为在Google设置中未启用locaiton选项,如附带的图片中所示.

如何编程检查Android应用程序中是否启用了谷歌设置位置?

http://www.cnet.com/how-to/explaining-the-google-settings-icon-on-your-android-device/

解决方法

LocationManager lm = null;
 boolean gps_enabled,network_enabled;
    if(lm==null)
        lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    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){
        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),int paramInt) {
                // TODO Auto-generated method stub

            }
        });
        dialog.show();

    }

ACTION_LOCATION_SOURCE_SETTINGS显示用户,允许配置当前位置源的设置.

原文链接:https://www.f2er.com/android/313928.html

猜你在找的Android相关文章