Android 監(jiān)聽手機(jī)GPS打開狀態(tài)實(shí)現(xiàn)代碼
Android 監(jiān)聽手機(jī)GPS打開狀態(tài)實(shí)現(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開啟一個(gè)就認(rèn)為是開啟的
* @param context
* @return true 表示開啟
*/
public boolean gpsIsOpen(final Context context) {
LocationManager locationManager
= (LocationManager) context.getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
// 通過GPS衛(wèi)星定位,定位級(jí)別可以精確到街(通過24顆衛(wèi)星定位,在室外和空曠的地方定位準(zhǔn)確、速度快)
boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// 通過WLAN或移動(dòng)網(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, " 手機(jī)GPS 打開", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(this, " 手機(jī)GPS 關(guān)閉", Toast.LENGTH_SHORT).show();
}
}
}
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
android實(shí)現(xiàn)支付寶咻一咻的幾種思路方法
本篇文章主要介紹了android實(shí)現(xiàn)支付寶咻一咻的幾種思路方法,詳解的介紹了幾種實(shí)現(xiàn)咻一咻的思路和方法,有需要的可以了解一下。2016-11-11
Android實(shí)現(xiàn)網(wǎng)絡(luò)多線程斷點(diǎn)續(xù)傳下載功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)網(wǎng)絡(luò)多線程斷點(diǎn)續(xù)傳下載功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
Android中okhttp3.4.1+retrofit2.1.0實(shí)現(xiàn)離線緩存
這篇文章主要介紹了Android中okhttp3.4.1結(jié)合retrofit2.1.0實(shí)現(xiàn)離線緩存,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
一文帶你搞清楚Android游戲發(fā)行切包資源ID那點(diǎn)事
這篇文章主要介紹了Android 解決游戲發(fā)行切包資源ID的一些問題,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2023-05-05
Android Studio 3.0 新功能全面解析和舊項(xiàng)目適配問題
Android Studio是Android的官方IDE。接下來通過本文給大家分享Android Studio 3.0 新功能全面解析和舊項(xiàng)目適配問題,需要的朋友可以參考下2017-11-11
android開發(fā)教程之自定義控件checkbox的樣式示例
這篇文章主要介紹了android自定義checkbox的樣式示例,需要的朋友可以參考下2014-03-03
關(guān)于Android 6.0權(quán)限的動(dòng)態(tài)適配詳解
Android 6.0版本(Api 23)推出了很多新的特性, 大幅提升了用戶體驗(yàn), 同時(shí)也為程序員帶來新的負(fù)擔(dān). 動(dòng)態(tài)權(quán)限管理就是這樣, 一方面讓用戶更加容易的控制自己的隱私, 一方面需要重新適配應(yīng)用權(quán)限,本文介紹了關(guān)于Android 6.0權(quán)限動(dòng)態(tài)適配的相關(guān)資料,需要的朋友可以參考下。2017-11-11
Android開發(fā)中的MVC設(shè)計(jì)模式淺析
這篇文章主要介紹了Android開發(fā)中的MVC設(shè)計(jì)模式淺析,本文講解了對(duì)Android開發(fā)中的MVC設(shè)計(jì)模式的理解,需要的朋友可以參考下2015-06-06

