以编程方式在android中打开GPS方式、android、GPS

2023-09-07 10:14:50 作者:口我

可能的重复,

如何启用/禁用 gps 和以编程方式在android中移动数据?

编程有意思 几款好用的Android静态代码检查工具,附实践过程

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

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

这是我正在使用的代码,

//启用GPSIntent intent = new Intent("android.location.GPS_ENABLED_CHANGE");intent.putExtra("启用", true);发送广播(意图);//禁用GPSIntent intent = new Intent("android.location.GPS_ENABLED_CHANGE");intent.putExtra("启用", false);发送广播(意图);

我也试过了,

 String provider = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);if(!provider.contains("gps")){//如果gps被禁用最终意图戳=新意图();poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");poke.addCategory(Intent.CATEGORY_ALTERNATIVE);poke.setData(Uri.parse("3"));context.sendBroadcast(戳);}

但当我这样做时,

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

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

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

解决方案

//开启GPSIntent intent = new Intent("android.location.GPS_ENABLED_CHANGE");intent.putExtra("启用", true);context.sendBroadcast(意图);//禁用GPSIntent intent = new Intent("android.location.GPS_ENABLED_CHANGE");intent.putExtra("启用", false);context.sendBroadcast(意图);

在广播时使用上下文.

希望这有效..

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

final ConnectivityManager conman = (ConnectivityManager) 上下文.getSystemService(Context.CONNECTIVITY_SERVICE);最终类 conmanClass = Class.forName(conman.getClass().getName());最终字段 iConnectivityManagerField = conmanClass.getDeclaredField("mService");iConnectivityManagerField.setAccessible(true);最终对象 iConnectivityManager = iConnectivityManagerField.get(骗子);最终类 iConnectivityManagerClass = 类.forName(iConnectivityManager.getClass().getName());最终方法 setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);setMobileDataEnabledMethod.setAccessible(true);setMobileDataEnabledMethod.invoke(iConnectivityManager, enabled);

Possible duplicates,

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

I have been seeing people telling that they are able to turn ON the GPS in android programatically. But I'm using the same code and not able to do that.

It is just showing that "Searching for GPS .." but not actually doing it.

Here is the code I'm using,

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

I also tried this,

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

But when I do,

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

it returns me isGPSEnabled as false always. If I turn on the gps manually it is returned as true.

Can any body tell me is it possible to ON the GPS like this? If so, where I'm going wrong.

解决方案

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

use context when you broadcast.

hope this works..

To enable mobile data you should use this -

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