以编程方式在android中打开GPS [英] Programmatically turn ON GPS in android

查看:40
本文介绍了以编程方式在android中打开GPS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复,

如何启用/禁用 gps 和以编程方式在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);

这篇关于以编程方式在android中打开GPS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆