2016-06-19 4 views

Antwort

1

Diese Frage wird oft diskutiert. Wie ich weiß, können Sie es nicht automatisch von Android 4.0.3 aus tun. Wenn Sie Android 2+ verwenden, haben Sie die folgende Zeile zu AndroidManifest hinzugefügt.

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

Es gibt einen anderen Ansatz. Verwenden Sie dazu die Google Einstellungs-API.

com.google.android.gms:play-services:8.1.0 

prüfen Google Samples

prüfen this auch Frage.

-1

In einigen Geräten können Sie GPS programmgesteuert nicht ein-/ausschalten, aber dort ein anderer großartiger Ansatz, der in allen Geräten (nonrooted/rooted) funktioniert. Zeigen Sie einfach einen Dialog, der GPS-Einstellungen öffnet, wo der Benutzer gps selbst bedienen kann.

public void statusCheck() 
      { 
       final LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

       if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 
        buildAlertMessageNoGps(); 

       } 


      } 
      private void buildAlertMessageNoGps() { 
        final AlertDialog.Builder builder = new AlertDialog.Builder(this); 
        builder.setMessage("Your GPS seems to be disabled, do you want to enable it?") 
          .setCancelable(false) 
          .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
           public void onClick(final DialogInterface dialog, final int id) { 
            startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS)); 
           } 
          }) 
          .setNegativeButton("No", new DialogInterface.OnClickListener() { 
           public void onClick(final DialogInterface dialog, final int id) { 
            dialog.cancel(); 
           } 
          }); 
        final AlertDialog alert = builder.create(); 
        alert.show(); 

       } 

oder benutzen diesen Code versuchen, es automatisch zu drehen, aber in einigen OS wird es nicht funktionieren.

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

    String provider = Settings.Secure.getString(ctx.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")); 
     this.ctx.sendBroadcast(poke); 


    } 
} 
// automatic turn off the gps 
public void turnGPSOff() 
{ 
    String provider = Settings.Secure.getString(ctx.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.ctx.sendBroadcast(poke); 
    } 
} 
0
LocationManager mlocationManager; 
Boolean isGPSEnabled; 

private static boolean checkGPSConnection(){ 
    if(mLocationManager == null) 
     mLocationManager = (LocationManager) mcontext.getSystemService(Context.LOCATION_SERVICE); 

    isGPSEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 
    return isGPSEnabled; 
} 

Verwenden checkGPSConnection() Methode, wo Sie wollen überprüfen GPS verfügbar ist oder nicht.

Verwandte Themen