android 定位 广播,android - 如何触发广播接收器在GPS开启/关闭? - SO中文参考 - www.soinside.com...
如何触发广播接收器在GPS开启/关闭?
问题描述 投票:35回答:5
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().matches("android.location.PROVIDERS_CHANGED")) {
Toast.makeText(context, "in android.location.PROVIDERS_CHANGED",
Toast.LENGTH_SHORT).show();
Intent pushIntent = new Intent(context, LocalService.class);
context.startService(pushIntent);
} else {
Toast.makeText(context, "not in android.location.PROVIDERS_CHANGED",
Toast.LENGTH_SHORT).show();
Intent pushIntent = new Intent(context, LocalService.class);
context.startService(pushIntent);
}
}
@Override
public void onLocationChanged(Location arg0) {
}
}
在我的应用程序,我需要触发广播接收器在GPS开启/关闭。关注此事,并建议在应用程序以实现最好的一个。
android
gps
broadcastreceiver
location-provider
5个回答
71
投票
当用户想触发反过来任何行动开/关的位置提供,这是有用
你应该在清单中添加这个动作
并添加这个动作后,你可以触发你的广播接收器
而在你BroadcastReceiver类,你必须实现LocationListener在类,这是下面给出以下..
public class GpsLocationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().matches("android.location.PROVIDERS_CHANGED")) {
Toast.makeText(context, "in android.location.PROVIDERS_CHANGED",
Toast.LENGTH_SHORT).show();
Intent pushIntent = new Intent(context, LocalService.class);
context.startService(pushIntent);
}
}
}
31
投票
我想补充@Deepak古普塔answer。我想为当GPS状态改变如何在您的片段或活动注册一个动态brodacsast接收器添加代码。
在您的活动或片段如下注册您的广播接收器。
private BroadcastReceiver gpsReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().matches(LocationManager.PROVIDERS_CHANGED_ACTION)) {
//Do your stuff on GPS status change
}
}
};
对于片段:
getContext().registerReceiver(gpsReceiver, new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION));
对于活动:
registerReceiver(gpsReceiver, new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION));
这在使用,你必须访问上下文,不只是一个活动或片段内的任何地方。我希望它的帮助。
13
投票
你可以试试这个代码,用户@DanielNugent指出:
ContentResolver contentResolver = getContext().getContentResolver();
// Find out what the settings say about which providers are enabled
int mode = Settings.Secure.getInt(
contentResolver, Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF);
if (mode != Settings.Secure.LOCATION_MODE_OFF) {
if (mode == Settings.Secure.LOCATION_MODE_HIGH_ACCURACY) {
locationMode = "High accuracy. Uses GPS, Wi-Fi, and mobile networks to determine location";
} else if (mode == Settings.Secure.LOCATION_MODE_SENSORS_ONLY) {
locationMode = "Device only. Uses GPS to determine location";
} else if (mode == Settings.Secure.LOCATION_MODE_BATTERY_SAVING) {
locationMode = "Battery saving. Uses Wi-Fi and mobile networks to determine location";
}
}
0
投票
后,Android定位模式工作的解决方案在Android中引入
我们可以使用位置模式检查更精确
private boolean isGpsEnabled(Context context) {
ContentResolver contentResolver = getContext().getContentResolver();
// Find out what the settings say about which providers are enabled
// String locationMode = "Settings.Secure.LOCATION_MODE_OFF";
int mode = Settings.Secure.getInt(
contentResolver, Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF);
if (mode != Settings.Secure.LOCATION_MODE_OFF) {
return true;
/* if (mode == Settings.Secure.LOCATION_MODE_HIGH_ACCURACY) {
locationMode = "High accuracy. Uses GPS, Wi-Fi, and mobile networks to determine location";
} else if (mode == Settings.Secure.LOCATION_MODE_SENSORS_ONLY) {
locationMode = "Device only. Uses GPS to determine location";
} else if (mode == Settings.Secure.LOCATION_MODE_BATTERY_SAVING) {
locationMode = "Battery saving. Uses Wi-Fi and mobile networks to determine location";
}*/
} else {
return false;
}
}
-2
投票
你可以试试这个
表现
MyReceiver
if (intent.getAction().matches("android.location.GPS_ENABLED_CHANGE"))
{
boolean enabled = intent.getBooleanExtra("enabled",false);
Toast.makeText(context, "GPS : " + enabled,
Toast.LENGTH_SHORT).show();
}
热门问题
android 定位 广播,android - 如何触发广播接收器在GPS开启/关闭? - SO中文参考 - www.soinside.com...相关推荐
- android 隐藏webview地址栏,flutter - 在Flutter Webview中隐藏Url引用 - SO中文参考 - www.soinside.com...
flutter_inappwebview: git: url: https://github.com/pichillilorenzo/flutter_inappwebview.git ref: mas ...
- android新材料设计,android - 如何实现新材料BottomAppBar为BottomNavigationView - SO中文参考 - www.soinside.com...
解决了 基本上,而不是试图迫使菜单的资源,我需要的布局,我用这个方法,而不是,我只是把使用"空"元素作为@dglozano建议BottomAppBar内的LinearLayout. ...
- android layout 工具栏,android - 滚动时CollapsingToolbarLayout并隐藏工具栏 - SO中文参考 - www.soinside.com...
我正在尝试使用CoordinatorLayout和CollapsingToolbarLayout创建一些组合布局. 在第一个状态,当我们在最顶层的页面上,并且还没有滚动时,我希望工具栏如下所示(是的, ...
- Android 字体没效果,android - 更改选项卡的字体,fontFamily不起作用 - SO中文参考 - www.soinside.com...
我在Xamarin.Forms应用程序(在android上)中为标签定义自定义字体时遇到问题. The Tabbar.xml<?xml version="1.0" encod ...
- android 定位蓝牙,Android BLE定位应用(蓝牙搜索方法)
最近刚好有一个需求,需要实时收取并保存指定蓝牙广播,用于蓝牙定位应用. 然后我使用老方法注册广播开启蓝牙搜素,发现并不能实时收到广播,有时会隔个几秒. 这个当然是不行的,蓝牙定位最低要求也是需要一秒一 ...
- java模拟滑动事件_java - 以编程方式在Android视图中触发滑动动作事件 - SO中文参考 - www.soinside.com...
我正在尝试以编程方式触发视图中的滑动.还有其他问题的答案,但他们没有为我工作.我还看到一些comments表示程序化的拖动/滚动因安全原因被禁用,这是真的吗?我很想找到一个明确的答案. 我尝试了以下方 ...
- android定位周边,Android 使用高德SDK编写周边搜索定位
转载请注明! 前言 使用高德SDK实现定位及周边的搜索界面,先看效果图: 使用到了高德以下sdk: com.amap.api:3dmap:latest.integration com.amap.api ...
- android+定位跟踪,android实现远程定位追踪(UC+XSS)
前言 当PC时代的辉光逐渐隐入了暮霭,一轮更加炫目的红日正在缓缓升起,时至今日,移动互联网已经成为引领时代发展与前沿技术不断革新的中流砥柱,短短十数年 间,手机的兴盛就超出了所有人的预料,大海淘沙,洗 ...
- android 定位蓝牙,Android开发-蓝牙RSSI定位
RSSI(Received Signal Strength Indication)是接收的信号强度指示,用来判定链接质量,利用RSSI信号值可以计算主设备和蓝牙设备的距离信息.通过至少三个设备的RSS ...
最新文章
- Web App、Hybrid App与Native App的设计差异
- Bioinformatics | 预测药物相互作用的多模式深度学习框架
- CentOS7安装Docker详细教程
- Openlayers中使用Overlay实现点击要素显示html内容弹窗并且动态更改弹窗内容
- 连招 横版 flash 游戏_街机游戏中的无限连究竟有多变态?有种对决叫作没开始就结束了!...
- [蓝桥杯]PREV-19.历届试题_九宫重排
- 休眠和UUID标识符
- plsql连接远程Oracle的方法
- Java中使用Jedis操作Redis,java初级面试笔试题
- JAVA中为什么要用接口定义编程_【Java公开课|为什么要用Java接口,这些内容你一定要搞清楚】- 环球网校...
- js排序算法详解-希尔排序
- 在Windows中使用FileZilla Server创建FTP
- 微信小程序UI组件库集合(个人收藏)
- 计算机开机一直无法访问,电脑开机时显示:AutoIt 错误 不能打开脚本文件 如何处理...
- 解决 Could not locate executable null\bin\winutils.exe in the Hadoop binaries 异常
- tableau数据汇总/明细/分-总的行列展示— Lee桑的学习笔记
- C++调用Lua出现 unproteted error in call to Lua API错误的发现过程与解决方法
- k8s 部署 owncloud
- 2022年终总结:少年不惧岁月长,彼方尚有荣光在。
- Burpsuite介绍及2022.8.2版本超详细安装教程(图文版)
热门文章
- 机器学习(一) 基于sklearn库的数据集划分(交叉验证)
- 北京林业大学计算机技术复试,北京林业大学计算机应用技术04年考研复试办法...
- 4pam调制与解调(matlab实现)
- html5表单实现简单计算器
- 2017.10.13java上机出现中的问题
- [LeetCode] 4Sum II 四数之和之二
- poj 3728 Catch That Cow ([kuangbin带你飞]专题一 简单搜索)
- 【数据结构】量子危机
- modprobe:FATAL: could not load /lib/modules/2.6.35-22-generic/modules.dep No such file or directory
- 世界领先的界面设计公司:The Skins Factory