机器人的GPS编程接通和关断 [英] Android gps programmatically turning on and turning off

查看:196
本文介绍了机器人的GPS编程接通和关断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了一个简单的GPS code,这是开启和一些设备关闭GPS编程(说摩托G),但某些旧版本或三星二重奏,它不是在GPS开启编程其他一些设备。 请帮我对这个问题的,

如果GPS接通后我会得到经度和纬度,所以我会用它

和在此先感谢

  

您必须从清单文件降低你的API级别

解决方案

请其更改为按您的要求。

打开了GPS

 私人无效turnGPSOn()
{

    字符串商= android.provider.Settings.Secure.getString(getContentResolver(),android.provider.Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
    如果(!provider.contains(全球定位系统))
    {
        最终意图捅=新意图();
        poke.setClassName(com.android.settings,com.android.settings.widget.SettingsAppWidgetProvider);
        poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
        poke.setData(Uri.parse(3));
        sendBroadcast(捅);
    }
}
 

停止GPS和获取纬度长

这$ C $下停止GPS

  LocationUtil locationUtil =新LocationUtil(getApplicationContext());
                        locationUtil.stopUsingGPS();
 如果(statusOfGPS){

         意向意图=新的意图(android.location.GPS_ENABLED_CHANGE);
         intent.putExtra(已启用,假);
         sendBroadcast(意向);
    }

     字符串商= Settings.Secure.getString(getContentResolver(),Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
     如果(provider.contains(全球定位系统))
     {//如果GPS已启用
         最终意图捅=新意图();
         poke.setClassName(com.android.settings,com.android.settings.widget.SettingsAppWidgetProvider);
         poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
         poke.setData(Uri.parse(3));
         sendBroadcast(捅);
     }
 

获取纬度长

 公共类LocationUtil实现LocationListener的
{
私人上下文的背景下;
私人LocationManager locationManager;
位置ToPassLocation = NULL;
//阿赫亚变化
私人字符串提供商;

//改变

公共LocationUtil(上下文的背景下)
{
    this.context =背景;
    Log.d(位置,创建对象);

    locationManager =(LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,这一点);
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,这一点);
}

公共场所getLatLongLast()
{
    //阿赫亚变化
    如果(ToPassLocation == NULL)
    {
        locationManager =(LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
        标准标准=新标准();
        供应商= locationManager.getBestProvider(标准,FALSE);

        如果(provider.isEmpty())
        {
            Log.e(Location_Util,OLD提供: -  NETWORK_Provider);
            供应商= LocationManager.NETWORK_PROVIDER;
            onProviderEnabled(供应商);
            位置位置= locationManager.getLastKnownLocation(供应商);
            onProviderEnabled(供应商);
            返回的位置;
        }
        否则,如果(provider.isEmpty())
        {
            Log.e(Location_Util,OLD提供: -  GPS_Provider);
            供应商= LocationManager.GPS_PROVIDER;
            onProviderEnabled(供应商);
            位置位置= locationManager.getLastKnownLocation(供应商);
            onProviderEnabled(供应商);
            返回的位置;
        }
        其他
        {
            Log.e(Location_Util,OLD提供: -  PASSIVE_Provider);
            供应商= LocationManager.PASSIVE_PROVIDER;
            onProviderEnabled(供应商);
            位置位置= locationManager.getLastKnownLocation(供应商);
            onProviderEnabled(供应商);
            返回的位置;
        }
    }
    其他
    {
        Log.e(Location_Util,新的提供者: - 会得同时调用get提供者);
        返回ToPassLocation;
    }
}

  //公共场所getLatLongLastNew()
 // {
//返回ToPassLocation;
//}
//

@覆盖
公共无效onLocationChanged(位置定位)
{
    ToPassLocation =位置;
    通信System.err.println(新定位 - + ToPassLocation);
    通信System.err.println(纵横__: - + location.getLatitude()+ - );
    通信System.err.println(经度_: - + location.getLongitude()+ - );
    Log.e(Location_Util,提供者__: - + location.getProvider());
    location.getLatitude();
    location.getLongitude();
}

@覆盖
公共无效onProviderDisabled(字符串提供商)
{
    // TODO自动生成方法存根
}

@覆盖
公共无效onProviderEnabled(字符串提供商)
{}

@覆盖
公共无效onStatusChanged(字符串商,INT地位,捆绑演员)
{
    // TODO自动生成方法存根
}

  公共无效stopUsingGPS(){
        如果(locationManager!= NULL){
            locationManager.removeUpdates(本);
        }
    }
}
 

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,

If gps is turned on successfully i will get latitude and longitude, so i will use it

and thanks in advance

You have to decrease your API level from Manifest File

解决方案

Please change it as per your requirement.

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

Stop GPS and Get Lat Long

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

Get Lat Long

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

这篇关于机器人的GPS编程接通和关断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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