git地址
https://github.com/yayaa/LocationManager
安装
在gradle里边添加
compile 'com.yayandroid:LocationManager:2.0.4'
AndroidManifest.xml权限添加
<!-- Required to check whether user has network connection or not --> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <!-- Dangerous Permissions --> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />添加presenterX类
package online.geekgalaxy.layoutlearn; /** * Created by jailman on 2017/9/19. */ import android.location.Location; import android.text.TextUtils; import com.yayandroid.locationmanager.constants.FailType; import com.yayandroid.locationmanager.constants.ProcessType; public class presenter { private SampleView sampleView; public presenter(SampleView view) { this.sampleView = view; } public void destroy() { sampleView = null; } public void onLocationChanged(Location location) { sampleView.dismissProgress(); setText(location); } public void onLocationFailed(@FailType int failType) { sampleView.dismissProgress(); switch (failType) { case FailType.TIMEOUT: { sampleView.setText("Couldn't get location, and timeout!"); break; } case FailType.PERMISSION_DENIED: { sampleView.setText("Couldn't get location, because user didn't give permission!"); break; } case FailType.NETWORK_NOT_AVAILABLE: { sampleView.setText("Couldn't get location, because network is not accessible!"); break; } case FailType.GOOGLE_PLAY_SERVICES_NOT_AVAILABLE: { sampleView.setText("Couldn't get location, because Google Play Services not available!"); break; } case FailType.GOOGLE_PLAY_SERVICES_CONNECTION_FAIL: { sampleView.setText("Couldn't get location, because Google Play Services connection failed!"); break; } case FailType.GOOGLE_PLAY_SERVICES_SETTINGS_DIALOG: { sampleView.setText("Couldn't display settingsApi dialog!"); break; } case FailType.GOOGLE_PLAY_SERVICES_SETTINGS_DENIED: { sampleView.setText("Couldn't get location, because user didn't activate providers via settingsApi!"); break; } case FailType.VIEW_DETACHED: { sampleView.setText("Couldn't get location, because in the process view was detached!"); break; } case FailType.VIEW_NOT_REQUIRED_TYPE: { sampleView.setText("Couldn't get location, " + "because view wasn't sufficient enough to fulfill given configuration!"); break; } case FailType.UNKNOWN: { sampleView.setText("Ops! Something went wrong!"); break; } } } public void onProcessTypeChanged(@ProcessType int newProcess) { switch (newProcess) { case ProcessType.GETTING_LOCATION_FROM_GOOGLE_PLAY_SERVICES: { sampleView.updateProgress("Getting Location from Google Play Services..."); break; } case ProcessType.GETTING_LOCATION_FROM_GPS_PROVIDER: { sampleView.updateProgress("Getting Location from GPS..."); break; } case ProcessType.GETTING_LOCATION_FROM_NETWORK_PROVIDER: { sampleView.updateProgress("Getting Location from Network..."); break; } case ProcessType.ASKING_PERMISSIONS: case ProcessType.GETTING_LOCATION_FROM_CUSTOM_PROVIDER: // Ignored break; } } private void setText(Location location) { String appendValue = location.getLatitude() + ", " + location.getLongitude() + "\n"; String newValue; CharSequence current = sampleView.getText(); if (!TextUtils.isEmpty(current)) { newValue = current + appendValue; } else { newValue = appendValue; } sampleView.setText(newValue); } public interface SampleView { String getText(); void setText(String text); void updateProgress(String text); void dismissProgress(); } }
操作view显示坐标的类
package online.geekgalaxy.layoutlearn; import android.app.ProgressDialog; import android.location.Location; import android.os.Bundle; import android.view.View; import android.view.Window; import android.widget.Button; import android.widget.TextView; import com.yayandroid.locationmanager.base.LocationBaseActivity; import com.yayandroid.locationmanager.configuration.Configurations; import com.yayandroid.locationmanager.configuration.LocationConfiguration; import com.yayandroid.locationmanager.constants.FailType; import com.yayandroid.locationmanager.constants.ProcessType; /** * Created by jailman on 2017/9/19. */ public class location extends LocationBaseActivity implements presenter.SampleView { private ProgressDialog progressDialog; private TextView locationText; private presenter Presenter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.location); final Button locate = (Button) findViewById(R.id.button6); locate.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { locationText = (TextView) findViewById(R.id.textView); Presenter = new presenter(location.this); getLocation(); } }); } @Override protected void onDestroy() { super.onDestroy(); Presenter.destroy(); } @Override public LocationConfiguration getLocationConfiguration() { return Configurations.defaultConfiguration("Gimme the permission!", "Would you mind to turn GPS on?"); } @Override public void onLocationChanged(Location location) { Presenter.onLocationChanged(location); } @Override public void onLocationFailed(@FailType int failType) { Presenter.onLocationFailed(failType); } @Override public void onProcessTypeChanged(@ProcessType int processType) { Presenter.onProcessTypeChanged(processType); } @Override protected void onResume() { super.onResume(); if (getLocationManager().isWaitingForLocation() && !getLocationManager().isAnyDialogShowing()) { displayProgress(); } } @Override protected void onPause() { super.onPause(); dismissProgress(); } private void displayProgress() { if (progressDialog == null) { progressDialog = new ProgressDialog(this); progressDialog.getWindow().addFlags(Window.FEATURE_NO_TITLE); progressDialog.setMessage("Getting location..."); } if (!progressDialog.isShowing()) { progressDialog.show(); } } @Override public String getText() { return locationText.getText().toString(); } @Override public void setText(String text) { locationText.setText(text); } @Override public void updateProgress(String text) { if (progressDialog != null && progressDialog.isShowing()) { progressDialog.setMessage(text); } } @Override public void dismissProgress() { if (progressDialog != null && progressDialog.isShowing()) { progressDialog.dismiss(); } } }
view
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/bg3"> <TextView android:id="@+id/textView" android:layout_width="261dp" android:layout_height="137dp" android:layout_marginBottom="8dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_marginTop="8dp" android:text="位置信息" android:textAlignment="center" android:textColor="@android:color/holo_red_dark" android:textSize="18sp" android:textStyle="bold" app:layout_constraintBottom_toTopOf="@+id/button6" app:layout_constraintHorizontal_bias="0.504" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.637" /> <Button android:id="@+id/button6" android:layout_width="138dp" android:layout_height="75dp" android:layout_marginBottom="156dp" android:layout_marginLeft="16dp" android:layout_marginRight="8dp" android:background="@android:drawable/btn_default" android:backgroundTint="@android:color/holo_orange_dark" android:text="locate" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintHorizontal_bias="0.481" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" /> </android.support.constraint.ConstraintLayout>