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 = ...
随机推荐
- Form表单中不同的按钮进行不同的跳转
本文参考:http://my.oschina.net/sallency/blog/300568 在开发工作共我们往往会遇到一个表单需要包含多个action不同的提交动作,这时候就不能在使用submit ...
- 修复/lib/ld-linux.so.2: bad ELF interpreter: No such file or directory问题
在配置MongDB的是时候出现这/lib/ld-linux.so.2问题 [root@localhost local]# /usr/local/mongodb/mongodb/bin/mongod - ...
- 使用控制台程序搭建WebApi
原文参考: ASP.NET Web Api 2.2: Create a Self-Hosted OWIN-Based Web Api from Scratch 新建控制台程序,引入Owin包 PM&g ...
- Shell脚本 | 一键卸载安卓App
在平时工作的过程中,很多重复性内容可以通过运行脚本文件来代替.一次编写,就能带来很大的效率提升. 今天跟大家分享一个简单的 Shell 脚本,只有区区 20 行左右的代码. 因为有时候我们测试某个应用 ...
- 高可用Hadoop平台-探索
1.概述 上篇<高可用Hadoop平台-启航>博客已经让我们初步了解了Hadoop平台:接下来,我们对Hadoop做进一步的探索,一步一步的揭开Hadoop的神秘面纱.下面,我们开始赘述今 ...
- 【杂谈】从CGI到Servlet
访问服务器的静态页面 每个Web服务器都运行着一个HTTP服务软件,用于响应web浏览器的请求,返回客户想要的页面.HTTP服务器都会有一个文件夹用于放置相关的页面文件,默认是 /user/loca ...
- Java NIO系列教程(八) SocketChannel
Java NIO中的SocketChannel是一个连接到TCP网络套接字的通道.可以通过以下2种方式创建SocketChannel: 打开一个SocketChannel并连接到互联网上的某台服务器. ...
- Spring注解 @Configuration
Spring注解 @Configuration 一.@Configuration的作用 二.@Configuration的Spring容器启动方式 三.不加@Configuration的@Bean的解 ...
- What is AMQP? and the architecture
What is AMQP? (Advanced Message Queuing Protocol) When two applications need to communicate there ar ...
- 看 Netty 在 Dubbo 中如何应用
目录: dubbo 的 Consumer 消费者如何使用 Netty dubbo 的 Provider 提供者如何使用 Netty 总结 前言 众所周知,国内知名框架 Dubbo 底层使用的是 Net ...