Android 监听手机GPS打开状态实现代码
更新时间:2017年05月16日 14:49:47 作者:赵彦军
这篇文章主要介绍了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();
}
}
}
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
您可能感兴趣的文章:
相关文章
Android SDK 百度地图通过poi城市内检索简介接口的使用
这篇文章主要介绍了Android SDK 百度地图通过poi城市内检索简介接口的使用的相关资料,需要的朋友可以参考下2016-02-02
Android编程使用Service实现Notification定时发送功能示例
这篇文章主要介绍了Android编程使用Service实现Notification定时发送功能,涉及Android服务Service控制通知的发送功能相关操作技巧,需要的朋友可以参考下2017-08-08
Android中自定义的dialog中的EditText无法弹出输入法解决方案
这篇文章主要介绍了Android中自定义的dialog中的EditText无法弹出输入法解决方案,需要的朋友可以参考下2017-04-04
Android Studio实现仿微信APP门户界面详解及源码
这篇文章带你通过Android studio来实现微信APP的门户界面,主要说明框架的各部分功能与实现过程,下文包含了整个开发过程,以及解决问题的思路并再末尾提供了源码链接2021-10-10
解决VSCode调试react-native android项目错误问题
这篇文章主要介绍了VSCode调试react-native android项目错误解决办法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2020-12-12
Android Activity之间传递图片(Bitmap)的方法
这篇文章介绍了Android Activity之间传递图片(Bitmap)的方法,有需要的朋友可以参考一下2013-08-08


最新评论