Android 监听手机GPS打开状态

简介: 转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/70854942 本文出自【赵彦军的博客】GPS_Presenter package com.

转载请标明出处: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();
        }
    }
}
相关文章
|
3月前
|
Android开发 数据安全/隐私保护
安卓手机修改设备id, 安卓硬改一键新机,手机机型修改(伪装)
提供了完整的设备信息修改功能,包含设备模板配置、基础信息修改、网络信息修改、模拟器检测绕
|
3月前
|
存储 人工智能 文字识别
三款安卓手机word编辑器下载,Microsoft Word,wps office,Word手机版,手机word编辑查看阅读器,PDF转换器apk下载
WPS Office是一款功能强大的办公软件,支持文档编辑、表格处理和演示文稿制作,兼容多种格式并提供丰富的云服务。它具备低内存占用、快速运行的特点,支持跨设备同步与多人协作,内置海量模板及AI辅助功能,如智能写作和PPT自动生成。此外,还可扫描文件、编辑PDF并转换为其他格式,极大提升办公效率,适合手机用户便捷操作。
286 1
|
4月前
|
Android开发
安卓硬改一键新机工具,一键修改手机型号,串号网卡Imei、sn码【仅供学习参考】
声明部分:仅供学习参考使用,基于Xposed框架实现的设备信息伪装模块的完整代码,包含多个功能模块:
|
3月前
|
API Android开发 数据安全/隐私保护
|
4月前
|
存储 JSON API
安卓ck提取工具,可提取手机cookie插件,AUTOJS即可实现
怎么用autojs提取手机端的CK?其实autojs是支持提取ck的但是他提取的不是浏览器的CK,二十他自身浏览器环境的c
|
4月前
|
Java Android开发
安卓手机硬改工具, 设备型号修改神器, 安卓硬改一键新机
通过Java创建可执行JAR来修改安卓设备信息。核心功能包括读取系统属性
|
4月前
|
存储 Android开发
一键新机安卓无限, 免root改手机机型, 手机信息修改型号伪装
AndroidManifest.xml配置 资源文件管理 各系统服务的Hook
|
11月前
|
Android开发 数据安全/隐私保护 虚拟化
安卓手机远程连接登录Windows服务器教程
安卓手机远程连接登录Windows服务器教程
2231 5
|
11月前
|
安全 搜索推荐 Android开发
Android vs. iOS:解锁智能手机操作系统的奥秘####
【10月更文挑战第21天】 在当今这个数字化时代,智能手机已成为我们生活中不可或缺的伙伴。本文旨在深入浅出地探讨两大主流操作系统——Android与iOS的核心差异、优势及未来趋势,帮助读者更好地理解这两个平台背后的技术哲学和用户体验设计。通过对比分析,揭示它们如何塑造了我们的数字生活方式,并展望未来可能的发展路径。无论您是技术爱好者还是普通用户,这篇文章都将带您走进一个充满创新与可能性的移动世界。 ####
342 3
|
12月前
|
Android开发 iOS开发 UED
安卓与iOS的较量:谁才是智能手机市场的王者?
本文将深入探讨安卓和iOS两大智能手机操作系统之间的竞争关系,分析它们各自的优势和劣势。通过对比两者在市场份额、用户体验、应用生态等方面的表现,我们将揭示出谁才是真正的市场领导者。无论你是安卓粉丝还是iOS忠实用户,这篇文章都将为你提供一些有趣的观点和见解。

热门文章

最新文章