以编程方式打开GPS的机器人 [英] Programmatically turn ON GPS in android

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

问题描述

可能重复,

<一个href="http://stackoverflow.com/questions/16752859/how-to-enable-disable-gps-and-mobile-data-in-android-programmatically">How启用/禁用GPS和移动数据在Android的编程?

我已经看到的人告诉他们是能够把对GPS的机器人编程。但是,我使用的是相同的code,不能够做到这一点。

这只是展示了搜索GPS。但实际上没有这样做。

下面是code我使用,

  //启用GPS
  意向意图=新的意图(android.location.GPS_ENABLED_CHANGE);
  intent.putExtra(已启用,真正的);
  sendBroadcast(意向);

  //禁用GPS
 意向意图=新的意图(android.location.GPS_ENABLED_CHANGE);
 intent.putExtra(已启用,假);
 sendBroadcast(意向);
 

我也试过这个,

 字符串商= Settings.Secure.getString(context.getContentResolver(),Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
    如果(!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经理=(LocationManager)getSystemService(getApplicationContext()LOCATION_SERVICE。);
 布尔isGPSEnabled =经理
            .isProviderEnabled(LocationManager.GPS_PROVIDER);
 

返回我的 isGPSEnabled 作为假的始终。如果我打开手动的GPS则返回为真。

任何机构可以告诉我是否有可能在这样的全球定位系统?如果是这样,我要去哪里错了。

解决方案

  //启用GPS
意向意图=新的意图(android.location.GPS_ENABLED_CHANGE);
intent.putExtra(已启用,真正的);
context.sendBroadcast(意向);

//禁用GPS
意向意图=新的意图(android.location.GPS_ENABLED_CHANGE);
intent.putExtra(已启用,假);
context.sendBroadcast(意向);
 

  

使用情况下,当你播放。

希望这个作品。

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

 最后ConnectivityManager赌侠=(ConnectivityManager)上下文
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    最后类conmanClass =的Class.forName(conman.getClass()的getName());
    最后一个字段iConnectivityManagerField = conmanClass
            .getDeclaredField(MSERVICE);
    iConnectivityManagerField.setAccessible(真正的);
    最终目标iConnectivityManager = iConnectivityManagerField
            获得(赌侠);
    最后类iConnectivityManagerClass =类
            .forName(iConnectivityManager.getClass()的getName());
    最后一个方法setMobileDataEnabledMethod = iConnectivityManagerClass
            .getDeclaredMethod(setMobileDataEnabled,Boolean.TYPE);
    setMobileDataEnabledMethod.setAccessible(真正的);

    setMobileDataEnabledMethod.invoke(iConnectivityManager,启用);
 

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

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

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