国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android 監聽手機GPS打開狀態實現代碼

2019-10-23 18:28:57
字體:
來源:轉載
供稿:網友

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開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 广丰县| 聊城市| 新化县| 鄢陵县| 玛曲县| 商丘市| 乌海市| 交口县| 博兴县| 韩城市| 建昌县| 平阳县| 马尔康县| 鄂伦春自治旗| 张家口市| 左云县| 读书| 武山县| 广安市| 简阳市| 莱芜市| 衡阳市| 平舆县| 瓦房店市| 华坪县| 咸丰县| 秦皇岛市| 和林格尔县| 罗田县| 滦南县| 内乡县| 泌阳县| 恩施市| 神农架林区| 安庆市| 江源县| 郴州市| 普洱| 沂南县| 都江堰市| 威宁|