Android 監(jiān)聽手機GPS打開狀態(tài)實現(xiàn)代碼
Android 監(jiān)聽手機GPS打開狀態(tài)實現(xiàn)代碼
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 開關(guān)監(jiān)聽
*/
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衛(wèi)星定位,定位級別可以精確到街(通過24顆衛(wèi)星定位,在室外和空曠的地方定位準確、速度快)
boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// 通過WLAN或移動網(wǎng)絡(luò)(3G/2G)確定的位置(也稱作AGPS,輔助GPS定位。主要用于在室內(nèi)或遮蓋物(建筑群或茂密的深林等)密集的地方定位)
boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (gps || network) {
return true;
}
return false;
}
}
GPS_Interface 回調(diào)接口
package com.yiba.core;
/**
* Created by ${zhaoyanjun} on 2017/3/29.
* gps 開關(guān)監(jiān)聽
*/
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 關(guān)閉", Toast.LENGTH_SHORT).show();
}
}
}
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
Android SDK 百度地圖通過poi城市內(nèi)檢索簡介接口的使用
這篇文章主要介紹了Android SDK 百度地圖通過poi城市內(nèi)檢索簡介接口的使用的相關(guān)資料,需要的朋友可以參考下2016-02-02
Android編程使用Service實現(xiàn)Notification定時發(fā)送功能示例
這篇文章主要介紹了Android編程使用Service實現(xiàn)Notification定時發(fā)送功能,涉及Android服務(wù)Service控制通知的發(fā)送功能相關(guān)操作技巧,需要的朋友可以參考下2017-08-08
Android中自定義的dialog中的EditText無法彈出輸入法解決方案
這篇文章主要介紹了Android中自定義的dialog中的EditText無法彈出輸入法解決方案,需要的朋友可以參考下2017-04-04
Android Studio實現(xiàn)仿微信APP門戶界面詳解及源碼
這篇文章帶你通過Android studio來實現(xiàn)微信APP的門戶界面,主要說明框架的各部分功能與實現(xiàn)過程,下文包含了整個開發(fā)過程,以及解決問題的思路并再末尾提供了源碼鏈接2021-10-10
解決VSCode調(diào)試react-native android項目錯誤問題
這篇文章主要介紹了VSCode調(diào)試react-native android項目錯誤解決辦法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12
詳解android在mob平臺實現(xiàn)qq登陸和分享
這篇文章主要介紹了詳解android在mob平臺實現(xiàn)qq登陸和分享,對接入第三方平臺SDK感興趣的同學(xué)們,可以參考下2021-04-04
Android Activity之間傳遞圖片(Bitmap)的方法
這篇文章介紹了Android Activity之間傳遞圖片(Bitmap)的方法,有需要的朋友可以參考一下2013-08-08

