如何在android中启用gps [英] how to enable gps in android

查看:205
本文介绍了如何在android中启用gps的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在android中打开gps时关闭以检索当前位置
我测试了两种方法

how to open the gps in android when it is closed to retrieve the current position I test two method

private void turnGPSOn(){
    String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);

    if(!provider.contains("gps")){ //if gps is disabled
        final Intent poke = new Intent();
        poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"); 
        poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
        poke.setData(Uri.parse("3")); 
        sendBroadcast(poke);
    }
}

private void turnGPSOff(){
    String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);

    if(provider.contains("gps")){ //if gps is enabled
        final Intent poke = new Intent();
        poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
        poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
        poke.setData(Uri.parse("3")); 
        sendBroadcast(poke);
    }
}

和这个方法

and this method

enaABLE GPS:
Intent intent=new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
sendBroadcast(intent);
DISABLE GPS:

Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", false);
sendBroadcast(intent);

但是当通过这些方法之一打开gps是执行代码恢复位置时,它显示

but when opening the gps by one of these methods is execute code recovery position, it displays

    Toast.makeText(LbsGeocodingActivity.this,
            "Provider disabled by the user. GPS turned off",
            Toast.LENGTH_LONG).show();


推荐答案

您无法为用户启用GPS,你可以做的是,如果GPS被禁用,用消息提示用户启用GPS并发送给设置以启用它。

You can't enable GPS for a user, what you can do is, if GPS s disabled, prompt the user with a message to ask him to enable GPS and send him to the settings to enable it

使用以下代码:

with this code :

Intent gpsOptionsIntent = new Intent(  
    android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);  
startActivity(gpsOptionsIntent);

这是关于如何做的更详细的代码

This is a more detailed code on how to do it

http://java-blog.kbsbng .com / 2012/08 /显示对话框在android-to-prompt.html

要获取位置,请执行此操作

To get the location you do this

    locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

  /*try to see if the location stored on android is close enough for you,and avoid rerequesting the location*/
    Location location =  locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

    if (location == null
            || location.getTime()
                    - Calendar.getInstance().getTimeInMillis() > MAX_LAST_LOCATION_TIME)
    {

        locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, TIME_INTERVAL, LOCATION_INTERVAL, yourLocationListener);

    }
    else
    {
        //do your handling if the last known location stored on android is enouh for you
    }

在yourLocationListener中,您可以实现在获取位置时执行的操作

in yourLocationListener, you implement what to do when you get the location

这篇关于如何在android中启用gps的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆