打开和关闭GPS编程方式在安卓4.0及以上? [英] Turning on and off GPS programmatically in android 4.0 and above?

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

问题描述

您好,我建立一个防盗应用程序,并通过短信查找我的手机,它完美的作品,直到2.3。 但在4.0我不能打开或关闭GPS编程有通过code对GPS切换其他任何可能的方式。

Hi there i am creating an anti-theft application and, locating my phone through sms and it works perfectly until 2.3. But in 4.0 i cant turn on or off gps programmatically is there any other possible way to switch on gps through code.

推荐答案

尝试使用这种code。它为我工作的所有版本。

Try using this code. It worked for me on all the versions.

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

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

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