2016-06-19 10 views

答えて

1

この質問は何度も議論されています。私が知っているように、あなたはAndroid 4.0.3からそれを自動的に行うことはできません。 Android 2+を使用している場合は、AndroidManifestに次の行を追加しましたか?

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

異なるアプローチがあります。それはGoogleの設定APIを使用しています。

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

チェックGoogle Samples

チェックだけでなくthis質問。

-1

一部のデバイスでは、GPSをプログラムでオン/オフすることはできませんが、 でも、すべてのデバイス(ルートではない/ルート)で動作する別の優れたアプローチがあります。ユーザーがgpsを自分で回すことができる場所でGPS設定を開くダイアログを表示するだけです。

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

       } 

このコードを使用して自動的に有効にすることもできますが、一部のOSでは動作しません。

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

使用あなたはGPSが利用可能であるかどうか確認したいcheckGPSConnection()方法。

関連する問題