Android 监听手机GPS打开状态实现代码
发布时间 - 2026-01-11 01:08:02 点击率:次Android 监听手机GPS打开状态实现代码

GPS_Presenter
package com.yiba.core;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.LocationManager;
/**
* Created by ${zhaoyanjun} on 2017/3/29.
* GPS 开关监听
*/
public class GPS_Presenter {
private Context mContext ;
private Receiver receiver ;
private GPS_Interface mInterface ;
private String GPS_ACTION = "android.location.PROVIDERS_CHANGED" ;
public GPS_Presenter(Context context , GPS_Interface mInterface ){
this.mContext = context ;
this.mInterface = mInterface ;
observeWifiSwitch();
}
private void observeWifiSwitch(){
IntentFilter filter = new IntentFilter();
filter.addAction( GPS_ACTION );
receiver = new Receiver() ;
mContext.registerReceiver(receiver, filter);
}
/**
* 释放资源
*/
public void onDestroy(){
if ( receiver != null ){
mContext.unregisterReceiver( receiver );
}
if (mContext!=null){
mContext = null;
}
}
class Receiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().matches( GPS_ACTION )) {
if ( mInterface != null ){
mInterface.gpsSwitchState( gpsIsOpen( context ));
}
}
}
}
/**
* 判断GPS是否开启,GPS或者AGPS开启一个就认为是开启的
* @param context
* @return true 表示开启
*/
public boolean gpsIsOpen(final Context context) {
LocationManager locationManager
= (LocationManager) context.getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
// 通过GPS卫星定位,定位级别可以精确到街(通过24颗卫星定位,在室外和空旷的地方定位准确、速度快)
boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// 通过WLAN或移动网络(3G/2G)确定的位置(也称作AGPS,辅助GPS定位。主要用于在室内或遮盖物(建筑群或茂密的深林等)密集的地方定位)
boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (gps || network) {
return true;
}
return false;
}
}
GPS_Interface 回调接口
package com.yiba.core;
/**
* Created by ${zhaoyanjun} on 2017/3/29.
* gps 开关监听
*/
public interface GPS_Interface {
void gpsSwitchState( boolean gpsOpen );
}
在 Activity 中使用
package com.yiba.core;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements GPS_Interface {
private GPS_Presenter gps_presenter ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gps_presenter = new GPS_Presenter( this , this ) ;
}
@Override
protected void onDestroy() {
super.onDestroy();
//释放资源
if ( gps_presenter != null ){
gps_presenter.onDestroy();
}
}
@Override
public void gpsSwitchState(boolean gpsOpen) {
if ( gpsOpen ){
Toast.makeText(this, " 手机GPS 打开", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(this, " 手机GPS 关闭", Toast.LENGTH_SHORT).show();
}
}
}
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
# Android
# 监听手机GPS打开状态
# GPS
# 监听
# android手机获取gps和基站的经纬度地址实现代码
# 卫星定位
# 希望能
# 速度快
# 谢谢大家
# 主要用于
# 回调
# 室外
# 室内
# mInterface
# String
# mContext
# private
# Receiver
# GPS_Interface
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
android nfc常用标签读取总结
Laravel如何使用Service Container和依赖注入?(代码示例)
Laravel安装步骤详细教程_Laravel环境搭建指南
JS弹性运动实现方法分析
百度输入法ai面板怎么关 百度输入法ai面板隐藏技巧
Laravel如何实现邮箱地址验证功能_Laravel邮件验证流程与配置
C#如何调用原生C++ COM对象详解
C++用Dijkstra(迪杰斯特拉)算法求最短路径
百度浏览器网页无法复制文字怎么办 百度浏览器复制修复
Laravel如何记录日志_Laravel Logging系统配置与自定义日志通道
网站制作怎么样才能赚钱,用自己的电脑做服务器架设网站有什么利弊,能赚钱吗?
独立制作一个网站多少钱,建立网站需要花多少钱?
Win11怎么关闭透明效果_Windows11辅助功能视觉效果设置
Laravel如何安装Breeze扩展包_Laravel用户注册登录功能快速实现【流程】
如何在IIS中新建站点并解决端口绑定冲突?
标题:Vue + Vuex + JWT 身份认证的正确实践与常见误区解析
Laravel如何处理JSON字段的查询和更新_Laravel JSON列操作与查询技巧
php json中文编码为null的解决办法
Java Adapter 适配器模式(类适配器,对象适配器)优缺点对比
javascript中数组(Array)对象和字符串(String)对象的常用方法总结
Win11搜索栏无法输入_解决Win11开始菜单搜索没反应问题【技巧】
如何用y主机助手快速搭建网站?
Laravel如何编写单元测试和功能测试?(PHPUnit示例)
大连网站制作公司哪家好一点,大连买房网站哪个好?
怎么制作网站设计模板图片,有电商商品详情页面的免费模板素材网站推荐吗?
Win11怎样安装网易有道词典_Win11安装词典教程【步骤】
简历没回改:利用AI润色让你的文字更专业
Win11怎么查看显卡温度 Win11任务管理器查看GPU温度【技巧】
Gemini手机端怎么发图片_Gemini手机端发图方法【步骤】
宙斯浏览器文件分类查看教程 快速筛选视频文档与图片方法
香港服务器租用费用高吗?如何避免常见误区?
如何有效防御Web建站篡改攻击?
网站制作软件免费下载安装,有哪些免费下载的软件网站?
如何用ChatGPT准备面试 模拟面试问答与职场话术练习教程
Java垃圾回收器的方法和原理总结
Bootstrap整体框架之CSS12栅格系统
Linux网络带宽限制_tc配置实践解析【教程】
Laravel如何理解并使用服务容器(Service Container)_Laravel依赖注入与容器绑定说明
桂林网站制作公司有哪些,桂林马拉松怎么报名?
Laravel Eloquent模型如何创建_Laravel ORM基础之Model创建与使用教程
Laravel怎么配置S3云存储驱动_Laravel集成阿里云OSS或AWS S3存储桶【教程】
如何在宝塔面板中修改默认建站目录?
JS碰撞运动实现方法详解
如何快速查询域名建站关键信息?
iOS验证手机号的正则表达式
最好的网站制作公司,网购哪个网站口碑最好,推荐几个?谢谢?
如何在服务器上三步完成建站并提升流量?
java获取注册ip实例
HTML 中动态设置元素 name 属性的正确语法详解
如何用腾讯建站主机快速创建免费网站?

