【问题标题】:Programmatically turn ON GPS in android [duplicate]以编程方式在android中打开GPS [重复]
【发布时间】:2013-12-23 09:18:15
【问题描述】:

可能的重复,

How to enable/disable gps and mobile data in android programmatically?

我一直看到有人告诉他们能够以编程方式在 android 中打开 GPS。但我使用的是相同的代码,但无法做到这一点。

它只是显示“正在搜索 GPS ..”,但实际上并没有这样做。

这是我正在使用的代码,

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

  //Disable GPS
 Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
 intent.putExtra("enabled", false);
 sendBroadcast(intent);

我也试过了,

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

但当我这样做时,

 LocationManager manager = (LocationManager) getSystemService( getApplicationContext().LOCATION_SERVICE );
 boolean isGPSEnabled = manager
            .isProviderEnabled(LocationManager.GPS_PROVIDER);

它总是将 isGPSEnabled 返回为 false。如果我手动打开 gps,它会返回 true。

任何人都可以告诉我这样可以打开 GPS 吗?如果是这样,我哪里出错了。

【问题讨论】:

  • 问:我哪里出错了?答:知道问题是重复的,但还是继续发布。

标签: android gps locationmanager


【解决方案1】:
//Enable GPS
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
context.sendBroadcast(intent);

//Disable GPS
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", false);
context.sendBroadcast(intent);

在广播时使用上下文。

希望这有效..

要启用移动数据,您应该使用它 -

final ConnectivityManager conman = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    final Class conmanClass = Class.forName(conman.getClass().getName());
    final Field iConnectivityManagerField = conmanClass
            .getDeclaredField("mService");
    iConnectivityManagerField.setAccessible(true);
    final Object iConnectivityManager = iConnectivityManagerField
            .get(conman);
    final Class iConnectivityManagerClass = Class
            .forName(iConnectivityManager.getClass().getName());
    final Method setMobileDataEnabledMethod = iConnectivityManagerClass
            .getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
    setMobileDataEnabledMethod.setAccessible(true);

    setMobileDataEnabledMethod.invoke(iConnectivityManager, enabled);

【讨论】:

  • 我只使用第一个代码,但没有上下文。它不工作
  • 当你使用上下文时它应该可以工作,请尝试使用上下文。
  • 我尝试使用 getApplicationContext().sendBroadcast(intent);如果我手动打开 GPS,禁用有效但无法启用。它只是说正在搜索 GPS
  • 请检查您是否无法获得经纬度,如果您获得则它工作正常
  • 不,我无法理解。启用此功能后,我尝试了 LocationManager manager = (LocationManager) getSystemService( getApplicationContext().LOCATION_SERVICE ); boolean isGPSEnabled = manager .isProviderEnabled(LocationManager.GPS_PROVIDER);但它返回错误。当我做 location = manager .getLastKnownLocation(LocationManager.GPS_PROVIDER);位置为空
猜你喜欢
  • 2015-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
  • 1970-01-01
相关资源
最近更新 更多