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();
        }
    }
}
相关文章
|
2月前
|
监控 安全 Android开发
【新手必读】Airtest测试Android手机常见的设置问题
【新手必读】Airtest测试Android手机常见的设置问题
|
2月前
|
机器学习/深度学习 人工智能 Android开发
安卓智能手机操作系统演化史
【2月更文挑战第5天】 本文通过对安卓智能手机操作系统的演化历程进行探讨,分析了安卓系统从诞生至今的发展脉络和关键技术革新,从最初的版本到如今的最新版本,探讨了其在移动互联网时代的重要作用,以及未来可能的发展方向。
|
3月前
|
前端开发 小程序
监听手机屏幕旋转 横屏 竖屏 dome
监听手机屏幕旋转 横屏 竖屏 dome
|
3月前
|
JavaScript Android开发
手机也能搭建个人博客?安卓Termux+Hexo搭建属于你自己的博客网站
手机也能搭建个人博客?安卓Termux+Hexo搭建属于你自己的博客网站
36 0
|
1月前
|
Web App开发 前端开发 网络安全
前端分析工具之 Charles 录制 Android/IOS 手机的 https 应用
【2月更文挑战第21天】前端分析工具之 Charles 录制 Android/IOS 手机的 https 应用
50 1
前端分析工具之 Charles 录制 Android/IOS 手机的 https 应用
|
1月前
|
网络协议 关系型数据库 MySQL
安卓手机termux上安装MariaDB数据库并实现公网环境下的远程连接
安卓手机termux上安装MariaDB数据库并实现公网环境下的远程连接
|
1月前
|
JavaScript Android开发
手机也能轻松搭建个人博客,使用安卓Termux+Hexo建立自己的网站
手机也能轻松搭建个人博客,使用安卓Termux+Hexo建立自己的网站
|
2月前
|
安全 Android开发
如何在Android手机上安装第三方应用?
【2月更文挑战第4天】在Android系统中,安装第三方应用是一个常见的需求。本文将介绍如何在Android手机上安装第三方应用,并提供详细的步骤和注意事项。
157 2
|
3月前
|
安全 网络协议 Linux
【公网远程手机Android服务器】安卓Termux搭建Web服务器
【公网远程手机Android服务器】安卓Termux搭建Web服务器
63 0
|
4月前
|
数据挖掘 定位技术
出租车GPS轨迹、社交软件签到、手机信令数据下载网站整理
出租车GPS轨迹、社交软件签到、手机信令数据下载网站整理
113 2