Android 监听手机GPS打开状态实现代码
作者:赵彦军 时间:2022-09-28 08:56:38
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();
}
}
}
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
来源:http://blog.csdn.net/zhaoyanjun6/article/details/70854942
标签:Android,手机,GPS
0
投稿
猜你喜欢
利用logback filter过滤某个类 屏蔽某个类
2023-07-25 20:38:08
java设计模式之工厂模式实例详解
2023-11-24 23:35:12
基于静态Singleton模式的使用介绍
2022-09-13 20:11:08
Java语言实现二叉堆的打印代码分享
2021-11-27 23:00:15
安卓(Android)ListView 显示图片文字
2023-12-26 12:44:43
C#抓取网页数据 解析标题描述图片等信息 去除HTML标签
2023-03-13 17:23:07
Android 蓝牙开发实例解析
2021-06-04 03:34:37
Android实现给TableLayou绘制边框的方法
2021-08-04 02:08:41
C语言实现两个矩阵相乘
2023-07-22 12:41:20
Springboot Mybatis Plus自动生成工具类详解代码
2022-09-17 12:01:57
JAVA调用SAP WEBSERVICE服务实现流程图解
2023-11-07 03:54:57
Spring自定义注解配置简单日志示例
2023-01-25 09:31:08
详解Springboot配置文件的使用
2022-02-26 09:02:26
Android如何通过scheme跳转界面
2021-08-11 05:19:41
Java中的线程生命周期核心概念
2021-10-14 13:37:54
Android实现MVVM架构数据刷新详解流程
2023-07-05 13:33:41
c#中的扩展方法学习笔记
2023-04-11 10:29:15
JVM中的flag设置详解
2022-08-11 01:37:20
使用idea解决maven依赖冲突的问题
2021-10-16 12:32:51
Java剑指offer之删除链表的节点
2023-05-19 15:30:27