Android 监听手机GPS打开状态
转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/70854942
本文出自【赵彦军的博客】
- 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打开状态的更多相关文章
- Android监听手机网络变化
Android监听手机网络变化 手机网络状态发生变化会发送广播,利用广播接收者,监听手机网络变化 效果图 注册广播接收者 <?xml version="1.0" encodi ...
- Android 监听手机锁屏的工具类
自定义 ScreenListener package com.example.teagardenhd.Listener; import android.content.BroadcastReceive ...
- Android 监听 WiFi 开关状态
Android 监听 WiFi 开关状态 转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/70854309 本文出自[赵彦军的博客] ...
- Android之——监听手机开机事件
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47028535 本文中,主要通过监听开机广播来达到监听手机开机状态的操作.在Andr ...
- Android初级教程使用服务注册广播接收者监听手机解锁屏变化
之前第七章广播与服务理论篇写到: 特殊的广播接收者(一般发广播次数频率很高) 安卓中有一些广播接收者,必须使用代码注册,清单文件注册是无效的 屏幕锁屏和解锁 电量改变 今天在这里就回顾一下,且用代码方 ...
- 使用cordova network-information 插件监听手机网络状态
在使用html5配合cordova做webapp时,有时需要实时监测手机的网络 状况.html5里面是没有相关的js的,这时就需要在cordova里找相关插件了. 一.插件查找 1.在cordova中 ...
- 用BroadcastReceiver监听手机网络状态变化
android--解决方案--用BroadcastReceiver监听手机网络状态变化 标签: android网络状态监听方案 2015-01-20 15:23 1294人阅读 评论(3) 收藏 举报 ...
- Android 监听wifi广播的两种方式
1.XML中声明 <receiver android:name=".NetworkConnectChangedReceiver" > <i ...
- Android 监听双卡信号强度(附完整代码)
Android 监听双卡信号强度 监听单卡信号强度 监听单卡的信号强度非常简单直接用TelephonyManager.listen()去监听sim卡的信号强度. TelephonyManager = ...
随机推荐
- Scala的Trait详解
http://article.yeeyan.org/view/178378/358355
- (转)python的ConfigParser模块
原文:https://blog.csdn.net/miner_k/article/details/77857292 如何使用Python3读写INI配置文件-------https://blog.cs ...
- Python:SQLMap的工作流程
流程图 代码解析 后面补充…… 版权 作 者:曾是土木人 新浪微博:http://weibo.com/cstmr 转载请注明出处:http://www.cnblogs.com/hongfei/p/ ...
- Java 死锁优化
死锁示例1 public class SyncThread implements Runnable{ private Object obj1; private Object obj2; public ...
- sizeof()与Marshal.SizeOf()的不同
在.NET中一般都是通过sizeof()或Marshal.SizeOf()来获取数据类型的大小,来简要地看一下它们二者有何不同. sizeof() sizeof()在MSDN中的介绍是,在编译时获得一 ...
- <Think Python>中统计文献单词的处理代码
def process_line(line, hist): """Adds the words in the line to the histogram. Modi ...
- Python多版本管理器-pyenv 介绍及部署记录
一. pyenv简单介绍 在日常运维中, 经常遇到这样的情况: 系统自带的Python是2.x,而业务部署需要Python 3.x 环境, 此时需要在系统中安装多个Python版本,但又不能影响系统自 ...
- [转]SQL Server 2008 如何配置报表管理器
本文转自:https://docs.microsoft.com/zh-cn/previous-versions/sql/sql-server-2008/cc281384%28v%3dsql.100%2 ...
- 分分钟弄明白UML中泛化 , 实现 , 关联, 聚合, 组合, 依赖
在UML类图中,常见的有以下几种关系: 泛化(Generalization), 实现(Realization), 关联(Association), 聚合(Aggregation), 组合(Compo ...
- Table转换成实体、Table转换成实体集合(可转换成对象和值类型)
/// <summary> /// Table转换成实体 /// </summary> /// <typeparam name="T">< ...