안 드 로 이 드 감청 휴대 전화 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;
  }
}
package com.yiba.core;
/**
 * Created by ${zhaoyanjun} on 2017/3/29.
 * gps     
 */
public interface GPS_Interface {
  void gpsSwitchState( boolean gpsOpen );
}
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();
    }
  }
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Bitrise에서 배포 어플리케이션 설정 테스트하기이 글은 Bitrise 광고 달력의 23일째 글입니다. 자체 또는 당사 등에서 Bitrise 구축 서비스를 사용합니다. 그나저나 며칠 전 Bitrise User Group Meetup #3에서 아래 슬라이드를 발표했...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.