2015-02-10 132 views
2

我已經寫了一個簡單的gps代碼,它打開和關閉一些設備(比如moto g)編程的gps,但一些舊版本或三星二重奏和一些其他設備它不以編程方式打開gps。 請幫我對這個問題,Android gps以編程方式打開和關閉

如果打開GPS成功,我將獲得的經度和緯度,所以我會用它

,並在此先感謝

你必須減少你的API從清單文件中獲得的級別

+1

請張貼您的代碼! – CodeWalker 2015-02-10 06:58:20

回答

5

請根據您的要求更改它。

轉彎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和獲取Lat Long網

本作停機碼的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); 
    } 

獲取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); 
     }  
    } 
} 
+0

如果我們在使用上面的代碼的GPS上,我們必須從代碼中停止它,這意味着如果我們想從設備設置中關閉GPS,我們不能做到這一點。 – 2015-02-10 10:45:26

+3

**「我給你我的洞」**這讓我笑XD – 2015-06-15 11:03:04

+0

5.1.1上沒有功能 – Youness 2016-11-23 04:02:57

0

首先,它取決於您是否通過Google Play服務獲取地理位置,或者您是否採用「舊」方式。我不會建議在用戶背後做些事情。通常的方法是檢查GPS是否打開,並且最好通過使用PreferenceScreen來引導用戶進入系統設置。也可以通過DialogPreference或祝酒通知用戶。否則,如果你想這樣做的骯髒的方式,你可以找到一個答案Stackoverflow

0
public void turnGPSOn() { 
    Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE"); 
    intent.putExtra("enabled", true); 
    this.sendBroadcast(intent); 

    String provider = Settings.Secure.getString(this.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.setData(Uri.parse("3")); 
     this.sendBroadcast(poke); 
    } 
} 

// automatic turn off the gps 
public void turnGPSOff() { 
    Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE"); 
    intent.putExtra("enabled", false); 
    this.sendBroadcast(intent); 

    String provider = Settings.Secure.getString(this.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")); 
     this.sendBroadcast(poke); 
    } 
} 
相關問題