如何在android中启用gps

如何在android中启用gps,第1张

概述当它关闭以检索当前位置时如何在android中打开gps我测试了两种方法privatevoidturnGPSOn(){Stringprovider=Settings.Secure.getString(getContentResolver(),Settings.Secure.LOCATION_PROVIDERS_ALLOWED);if(!provider.contains("gps")){//ifgpsisdisabl

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

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);    }}

而这种方法

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是执行代码恢复位置时,它会显示

    Toast.makeText(LbsGeoCodingActivity.this,            "ProvIDer Disabled by the user. GPS turned off",            Toast.LENGTH_LONG).show();

解决方法:

您无法为用户启用GPS,您可以做的是,如果GPS已禁用,则会提示用户发送消息,要求他启用GPS并将其发送到设置以启用它

使用此代码:

Intent gpsOptionsIntent = new Intent(      androID.provIDer.Settings.ACTION_LOCATION_SOURCE_SETTINGS);  startActivity(gpsOptionsIntent);

这是一个关于如何 *** 作的更详细的代码

http://java-blog.kbsbng.com/2012/08/displaying-dialog-in-android-to-prompt.html

要获得该位置,请执行此 *** 作

    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中,您可以实现获取位置时要执行的 *** 作

总结

以上是内存溢出为你收集整理的如何在android中启用gps全部内容,希望文章能够帮你解决如何在android中启用gps所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1097085.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存