转载请标明出处: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打开状态的更多相关文章

  1. Android监听手机网络变化

    Android监听手机网络变化 手机网络状态发生变化会发送广播,利用广播接收者,监听手机网络变化 效果图 注册广播接收者 <?xml version="1.0" encodi ...

  2. Android 监听手机锁屏的工具类

    自定义 ScreenListener package com.example.teagardenhd.Listener; import android.content.BroadcastReceive ...

  3. Android 监听 WiFi 开关状态

    Android 监听 WiFi 开关状态 转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/70854309 本文出自[赵彦军的博客] ...

  4. Android之——监听手机开机事件

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47028535 本文中,主要通过监听开机广播来达到监听手机开机状态的操作.在Andr ...

  5. Android初级教程使用服务注册广播接收者监听手机解锁屏变化

    之前第七章广播与服务理论篇写到: 特殊的广播接收者(一般发广播次数频率很高) 安卓中有一些广播接收者,必须使用代码注册,清单文件注册是无效的 屏幕锁屏和解锁 电量改变 今天在这里就回顾一下,且用代码方 ...

  6. 使用cordova network-information 插件监听手机网络状态

    在使用html5配合cordova做webapp时,有时需要实时监测手机的网络 状况.html5里面是没有相关的js的,这时就需要在cordova里找相关插件了. 一.插件查找 1.在cordova中 ...

  7. 用BroadcastReceiver监听手机网络状态变化

    android--解决方案--用BroadcastReceiver监听手机网络状态变化 标签: android网络状态监听方案 2015-01-20 15:23 1294人阅读 评论(3) 收藏 举报 ...

  8. Android 监听wifi广播的两种方式

    1.XML中声明 <receiver android:name=".NetworkConnectChangedReceiver" >             <i ...

  9. Android 监听双卡信号强度(附完整代码)

    Android 监听双卡信号强度 监听单卡信号强度 监听单卡的信号强度非常简单直接用TelephonyManager.listen()去监听sim卡的信号强度. TelephonyManager = ...

随机推荐

  1. SqlServer 查看被锁的表和解除被锁的表

    查看被锁的表 1 2 select   request_session_id   spid,OBJECT_NAME(resource_associated_entity_id) tableName   ...

  2. java+hibernate+mysql

    实体类News package org.mythsky.hibernatedemo; import javax.persistence.*; @Entity @Table(name="new ...

  3. Java类加载顺序

    很长时间没看这方面的内容了,写篇文章让自己牢记一下,顺便分享一下. 首先,写代码以便检验结果.测试代码: public class Test { public static void main(Str ...

  4. Python学习--02输入和输出、运算符

    命令行输入 x = input("Please input x:") y = raw_input("Please input x:") 使用input和raw_ ...

  5. Redis+Jedis封装工具类

    package com.liying.monkey.core.util; import java.io.IOException; import java.util.ArrayList; import ...

  6. Leetcode 763. Partition Labels

    思路:动态规划.对于属于coins的coin,只要知道amount-coin至少需要多少个货币就能表示,那么amount需要的货币数目=amount-coin需要的货币数目+1:如果amount-co ...

  7. Java MongoDB

    package com.dys.mongo; import java.io.IOException; import java.util.ArrayList; import java.util.List ...

  8. [AHOI 2013]差异

    Description 题库链接 给定一个长度为 \(n\) 的字符串 \(S\) ,令 \(T_i\) 表示它从第 \(i\) 个字符开始的后缀.求 \[\sum_{1\leqslant i< ...

  9. spring cloud zuul在使用中遇到的坑 : 转发时自动去掉prefix

    在使用zuul的时候遇到的坑总结一下: 逐渐增加更新以后遇到的 1.在路由的时候莫名其妙的把serviceId给去掉,导致404.比如请求:/serviceId/search/book,zuul会把s ...

  10. C# WebApi 中设置Session可用

    在Global.acax中,添加下面方法 //设置session可用 public override void Init() { PostAuthenticateRequest += MvcAppli ...