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();
        }
    }
}
时间: 2024-08-27 11:10:49

Android 监听手机GPS打开状态的相关文章

Android 监听手机GPS打开状态实现代码

Android 监听手机GPS打开状态实现代码 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; /** *

Android监听手机电话状态与发送邮件通知来电号码的方法(基于PhoneStateListene实现)_Android

本文实例讲述了Android监听手机电话状态与发送邮件通知来电号码的方法.分享给大家供大家参考,具体如下: 在android中可以用PhoneStateListener来聆听手机电话状态(比如待机.通话中.响铃等).本例是通过它来监听手机电话状态,当手机来电时,通过邮件将来电号码发送到用户邮箱的例子.具体程序如下: import android.app.Activity; import android.content.Intent; import android.os.Bundle; impor

Android 监听WiFi的开关状态实现代码

Android 监听WiFi的开关状态实现代码 WifiSwitch_Presenter 源码: package com.yiba.wifi.sdk.lib.presenter; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net

Android监听来电和去电的实现方法_Android

本文实例讲述了Android监听来电和去电的实现方法.分享给大家供大家参考,具体如下: 要监听android打电话和接电话,只需下面2步骤 第一步,写一个Receiver继承自BroadcastReceiver import android.app.Service; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import andr

Android监听电池状态实例代码_Android

如果要监听电池的状态改变,需要动态注册:android.intent.action.BATTERY_CHANGED,收到Action后可以根据对应的Key获取你需要的信息,更详细信息可以参考以下例子中的BatteryChangedReceiver类 具体代码如下所示: package com.example.charginganimation; import android.app.Activity; import android.content.BroadcastReceiver; impor

Android 监听 WiFi 开关状态

Android 监听 WiFi 开关状态 转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/70854309 本文出自[赵彦军的博客] WifiSwitch_Presenter 源码: package com.yiba.wifi.sdk.lib.presenter; import android.content.BroadcastReceiver; import android.content.Context; import and

Android 监听网络状态方法详解

Android 监听网络状态方法详解 一.加入网络权限 获取网络信息需要在AndroidManifest.xml文件中加入相应的权限. <uses-permission Android:name="android.permission.ACCESS_NETWORK_STATE" /> 二.判断手机网络的几个方案 1)判断是否有网络连接 public boolean isMobileConnected(Context context) { if (context != nul

android 有办法 监听 手机 有没有发出声音 或 获取当前发出声音的音量吗

问题描述 android 有办法 监听 手机 有没有发出声音 或 获取当前发出声音的音量吗 又或者说 监听 当前手机自己发出的声音的分贝,急急急, 求 解决 解决方案 个人认为,你得和硬件人员先沟通下,或者直接咨询平台公司,看支持这个功能不,就如感光sensor一样,都是需要硬件和平台支持的.

Android 监听软键盘状态的实例详解

Android 监听软键盘状态的实例详解 近日遇到要检测软键盘是否显示或隐藏的问题,搜了一下网上,最后找到一个很简单的,记录一下. activityRoot是activity的根view,就是xml里面的第一个view,给它设置一个id. final View activityRootView = findViewById(R.id.activityRoot); activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(ne