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; /** * 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-09-20 05:28:31

Android 监听手机GPS打开状态实现代码的相关文章

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.Intent

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监听手机电话状态与发送邮件通知来电号码的方法(基于PhoneStateListene实现)_Android

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

Android中监听软键盘显示状态实现代码_Android

/**监听软键盘状态 * @param activity * @param listener */ public static void addOnSoftKeyBoardVisibleListener(Activity activity, final OnSoftKeyBoardVisibleListener listener) { final View decorView = activity.getWindow().getDecorView(); decorView.getViewTree

Android中监听软键盘显示状态实现代码

/**监听软键盘状态 * @param activity * @param listener */ public static void addOnSoftKeyBoardVisibleListener(Activity activity, final OnSoftKeyBoardVisibleListener listener) { final View decorView = activity.getWindow().getDecorView(); decorView.getViewTree

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