我怎样才能启用禁用GPS编程在Android? [英] How can i enable disable GPS programmatically in android?

查看:106
本文介绍了我怎样才能启用禁用GPS编程在Android?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在创建一个防盗应用程序,并通过短信查找我的手机,它的工作原理完美,直到2.3。
但是,在4.0中我无法打开或关闭gps编程方式是否有任何其他可能的方式来通过代码来切换gps。

尝试使用此代码。它适用于所有版本。

  public void turnGPSOn()
{
Intent 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)){//如果gps被禁用
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);



$ b //自动关闭gps
public void turnGPSOff()
{
String provider = Settings.Secure.getString(ctx.getContentResolver(),Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(provider.contains(gps)){//如果启用gps
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);
}
}


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.

解决方案

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编程在Android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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