欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-11-24 11:38:22
android 监听手机gps打开状态实现代码 gps_presenter package com.yiba.core; import android.c...

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;

/**
 * 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();
    }
  }
}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!