Android gps 以编程方式打开和关闭 [英] Android gps programmatically turning on and turning off

查看:16
本文介绍了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 以编程方式打开和关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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