猿问

如何在Android中以编程方式启用禁用GPS?

我正在创建一个防盗应用程序,并通过短信找到我的手机,它可以完美运行到2.3。但是在4.0中,我无法以编程方式打开或关闭gps,是否还有其他可能的方式通过代码打开gps。



冉冉说
浏览 485回答 3
3回答

SMILET

尝试使用此代码。它适用于所有版本。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 gpspublic 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 Intent intent = new Intent(“ android.location.GPS_ENABLED_CHANGE”); intent.putExtra(“ enabled”,false); sendBroadcast(intent); 

蓝山帝景

我收到“ java.lang.SecurityException:权限被拒绝:不允许发送广播android.location.GPS_ENABLED_CHANGE”。应用程序崩溃了
随时随地看视频慕课网APP

相关分类

Android
我要回答