Android:FusedLocationProvider,每隔几秒获取一次位置

Posted

技术标签:

【中文标题】Android:FusedLocationProvider,每隔几秒获取一次位置【英文标题】:Android: FusedLocationProvider, get location every few seconds 【发布时间】:2019-11-25 02:41:27 【问题描述】:

我正在制作一个应用程序来跟踪用户的位置并最终将其上传到 Firebase 服务器。

我创建了一个简单的应用程序,当我按下屏幕上的按钮时会显示位置。问题是当我走了几米后再次按下它时它不会改变位置(但当我重新进入应用程序时我确实得到了一个新位置)。为了让应用每 X 秒更新一次位置,我需要添加什么?这是我的活动:

package com.example.gpstest;

import android.content.pm.PackageManager;
import android.location.Location;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.tasks.OnSuccessListener;

import static android.Manifest.permission.ACCESS_FINE_LOCATION;

public class MainActivity extends AppCompatActivity 

    private FusedLocationProviderClient client;


    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        requestPermission();
        client = LocationServices.getFusedLocationProviderClient(this);
        Button button = findViewById(R.id.getLocation);
        button.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                if (ActivityCompat.checkSelfPermission(MainActivity.this, ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED ) 

                    return;
                
                client.getLastLocation().addOnSuccessListener(MainActivity.this, new OnSuccessListener<Location>() 
                    @Override
                    public void onSuccess(Location location) 

                        if(location != null)
                            TextView lat = findViewById(R.id.location1);
                            TextView lon = findViewById(R.id.location2);
                            TextView alt = findViewById(R.id.location3);
                            lat.setText(Double.toString(location.getLatitude()));
                            lon.setText(Double.toString(location.getLongitude()));
                            alt.setText(Double.toString(location.getAltitude()));
                        
                    
                );
            
        );
    

    private void requestPermission()
        ActivityCompat.requestPermissions(this, new String[]ACCESS_FINE_LOCATION, 1);
    



【问题讨论】:

你可以查看这个here的答案。 【参考方案1】:

因为您从 getLastLocation 获取位置。为此,您必须实现 LocationCallback。

consider this link了解更多信息

【讨论】:

【参考方案2】:

// 在你的类中实现这些接口 GoogleApiClient.ConnectionCallbacks、GoogleApiClient.OnConnectionFailedListener、LocationListener。 //比使用这个代码

public void onConnected(Bundle bundle)

    Location mLastLocation = null;
    if (mGoogleApiClient.isConnected()) 
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) 

            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more precise.
            return;
        
        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
                mGoogleApiClient);

        if (mLastLocation != null) 
            latLng = new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude());
        

        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(5000); //5 seconds
        mLocationRequest.setFastestInterval(3000); //3 seconds
        mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
        //mLocationRequest.setSmallestDisplacement(0.1F); //1/10 meter

        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
     else 
        buildGoogleApiClient();
        mGoogleApiClient.connect();
    


【讨论】:

以上是关于Android:FusedLocationProvider,每隔几秒获取一次位置的主要内容,如果未能解决你的问题,请参考以下文章

Android 逆向Android 权限 ( Android 逆向中使用的 android.permission 权限 | Android 系统中的 Linux 用户权限 )

android 21 是啥版本

Android逆向-Android基础逆向(2-2)

【Android笔记】android Toast

图解Android - Android核心机制

Android游戏开发大全的目录