Android gps programmatically turning on and turning off(Android gps 以编程方式打开和关闭)
问题描述
我编写了一个简单的 gps 代码,它在某些设备(比如 moto g)中以编程方式打开和关闭 gps,但在某些旧版本或三星二重奏和其他一些设备上它没有以编程方式打开 gps.请帮我解决这个问题,
i have written a simple gps code , which is turning on and turning off the gps programmatically in some device(say moto g) but some older version or Samsung duos and some other devices it not turning on gps programmatically . please help me regarding this issue,
如果gps开启成功我会得到纬度和经度,所以我会用它
If gps is turned on successfully i will get latitude and longitude, so i will use it
提前致谢
您必须从清单文件中降低您的 API 级别
You have to decrease your API level from Manifest File
推荐答案
请根据您的要求更改.
GPS 转向
private void turnGPSOn()
{
String provider = android.provider.Settings.Secure.getString(getContentResolver(),android.provider.Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if (!provider.contains("gps"))
{
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);
}
}
停止 GPS 并获得更长的纬度
Stop GPS and Get Lat Long
此代码用于停止 gps
this code for stop gps
LocationUtil locationUtil = new LocationUtil(getApplicationContext());
locationUtil.stopUsingGPS();
if (statusOfGPS) {
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", false);
sendBroadcast(intent);
}
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);
}
拉长距离
public class LocationUtil implements LocationListener
{
private Context context;
private LocationManager locationManager;
Location ToPassLocation = null;
// chetan changes
private String provider;
// changes
public LocationUtil(Context context)
{
this.context = context;
Log.d("Location", "Object created");
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,0, this);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0,0, this);
}
public Location getLatLongLast()
{
// chetan changes
if (ToPassLocation == null)
{
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
if (provider.isEmpty())
{
Log.e("Location_Util", "OLD Provider:- NETWORK_Provider");
provider = LocationManager.NETWORK_PROVIDER;
onProviderEnabled(provider);
Location location = locationManager.getLastKnownLocation(provider);
onProviderEnabled(provider);
return location;
}
else if (provider.isEmpty())
{
Log.e("Location_Util", "OLD Provider:- GPS_Provider");
provider = LocationManager.GPS_PROVIDER;
onProviderEnabled(provider);
Location location = locationManager.getLastKnownLocation(provider);
onProviderEnabled(provider);
return location;
}
else
{
Log.e("Location_Util", "OLD Provider:- PASSIVE_Provider");
provider = LocationManager.PASSIVE_PROVIDER;
onProviderEnabled(provider);
Location location = locationManager.getLastKnownLocation(provider);
onProviderEnabled(provider);
return location;
}
}
else
{
Log.e("Location_Util", "NEW Provider:- Will get while calling get provider");
return ToPassLocation;
}
}
// public Location getLatLongLastNew()
// {
// return ToPassLocation;
// }
//
@Override
public void onLocationChanged(Location location)
{
ToPassLocation = location;
System.err.println("Location New:-"+ ToPassLocation);
System.err.println("Latitude__:-"+location.getLatitude()+"-:");
System.err.println("Longitude_:-"+location.getLongitude()+"-:");
Log.e("Location_Util", "Provider__:-"+location.getProvider());
location.getLatitude();
location.getLongitude();
}
@Override
public void onProviderDisabled(String provider)
{
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider)
{}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
// TODO Auto-generated method stub
}
public void stopUsingGPS(){
if(locationManager != null){
locationManager.removeUpdates(this);
}
}
}
这篇关于Android gps 以编程方式打开和关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android gps 以编程方式打开和关闭
基础教程推荐
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01