打开地图活动时自动转动gps

时间:2016-06-19 09:35:23

标签: java android google-maps

Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
sendBroadcast(intent);    

我在我的主java类中使用了上面的代码,但它没有用。还有其他方法吗?

3 个答案:

答案 0 :(得分:1)

这个问题已多次讨论过。据我所知,您无法从Android 4.0.3自动执行此操作。如果您使用的是Android 2+,那么您是否在AndroidManifest中添加了以下行。

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

有一种不同的方法。这是使用Google Settings API。

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

检查Google Samples

同时检查this问题。

答案 1 :(得分: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;
}

使用checkGPSConnection()方法检查GPS是否可用。

答案 2 :(得分:-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();

                }

或尝试使用此代码自动将其关闭,但在某些操作系统中,它将无效。

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