安卓使用LocationManager定位

简介: 版权声明:本文可能为博主原创文章,若标明出处可随便转载。 https://blog.
版权声明:本文可能为博主原创文章,若标明出处可随便转载。 https://blog.csdn.net/Jailman/article/details/78030105

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>




目录
相关文章
|
定位技术 API 开发工具
Android 按照步骤接入百度地图API,定位显示不了解决办法
Android 按照步骤接入百度地图API,定位显示不了解决办法
378 1
|
2月前
|
Java 测试技术 Android开发
Android性能测试——发现和定位内存泄露和卡顿
本文详细介绍了Android应用性能测试中的内存泄漏与卡顿问题及其解决方案。首先,文章描述了使用MAT工具定位内存泄漏的具体步骤,并通过实例展示了如何分析Histogram图表和Dominator Tree。接着,针对卡顿问题,文章探讨了其产生原因,并提供了多种测试方法,包括GPU呈现模式分析、FPS Meter软件测试、绘制圆点计数法及Android Studio自带的GPU监控功能。最后,文章给出了排查卡顿问题的四个方向,帮助开发者优化应用性能。
178 4
Android性能测试——发现和定位内存泄露和卡顿
|
6月前
|
JSON Java 定位技术
【Android App】GPS获取定位经纬度和根据经纬度获取详细地址讲解及实战(附源码和演示 超详细)
【Android App】GPS获取定位经纬度和根据经纬度获取详细地址讲解及实战(附源码和演示 超详细)
1923 0
|
6月前
|
安全 算法 JavaScript
安卓逆向 -- 关键代码定位与分析技术
安卓逆向 -- 关键代码定位与分析技术
158 0
|
6月前
|
XML Java 定位技术
【Android App】定位导航GPS中开启手机定位功能讲解及实战(附源码和演示 超详细)
【Android App】定位导航GPS中开启手机定位功能讲解及实战(附源码和演示 超详细)
305 0
|
Android开发 C语言
[笔记]安卓 使用breakpad定位崩溃问题
[笔记]安卓 使用breakpad定位崩溃问题
107 0
|
定位技术 Android开发 芯片
Android 中获取LocationProvider的三种方法和获取定位信息
Android 中获取LocationProvider的三种方法和获取定位信息
506 0
|
算法 测试技术 Android开发
Android逆向——定位到某书 Sign 算法(下)
Android逆向——定位到某书 Sign 算法(下)
|
设计模式 算法 Java
Android逆向——定位到某书 Sign 算法(上)
Android逆向——定位到某书 Sign 算法
|
Java Shell 定位技术
Android 6.0 默认关闭定位和GPS,开启后默认选省电,永不休眠
Android 6.0 默认关闭定位和GPS,开启后默认选省电,永不休眠
322 0
下一篇
无影云桌面