gpt4 book ai didi

使用 FusedLocationProviderApi 的 Android 定位

转载 作者:搜寻专家 更新时间:2023-11-01 08:42:14 25 4
gpt4 key购买 nike

我正在尝试使用 FusedLocationProviderApi 在我的平板电脑上获取位置数据,但每当我启动该应用程序时,我都会收到错误

E/MainActivity:连接失败:ConnectionResult{statusCode=SERVICE_VERSION_UPDATE_REQUIRED, resolution=null}

我不知道是什么导致了这个错误。我有一种非品牌平板电脑,我认为这可能是导致问题的原因。型号:DL701Q,Android 版本:4.4.2 和 google play 构建版本 4.9.13

这是我的应用程序代码,在我测试以找出问题时,有些东西被注释掉了。

package temp;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;

import java.text.DateFormat;
import java.util.Date;


public class MainActivity extends Activity implements LocationListener,
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{

Button bLogout;
ImageButton bLogData;
GPSTracker gps;
TextView etLabel;
UserLocalStore userLocalStore;
Location mCurrentLocation;
String mLastUpdateTime;
LocationRequest mLocationRequest;
GoogleApiClient mGoogleApiClient;
private static final String TAG = "MainActivity";
private static final long INTERVAL = 1000 * 10;
private static final long FATEST_INTERVAL = 1000 * 5;
private static Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.e(TAG, "On Create . . . . .");

// if(!isGooglePlayServicesAvailable()){
// finish();
// }
createLocationRequest();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();

etLabel = (TextView) findViewById(R.id.etEmailLabel);
bLogout = (Button) findViewById(R.id.bLogout);
bLogData = (ImageButton) findViewById(R.id.DataLog);
// gps = new GPSTracker(MainActivity.this);

bLogData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
updateUI();
}
});

bLogout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
userLocalStore.clearuserData();
userLocalStore.setUserLoggedIn(false);
startActivity(new Intent(MainActivity.this, login.class));
}
});

userLocalStore = new UserLocalStore(this);
}

protected void createLocationRequest(){
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(INTERVAL);
mLocationRequest.setFastestInterval(FATEST_INTERVAL);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}

@Override
public void onStart() {
super.onStart();

if(authenticate() == true){
displayUserDetails();
}else{
startActivity(new Intent(MainActivity.this, login.class));
}

Log.e(TAG, "onStart fired ..............");
mGoogleApiClient.connect();
Log.e(TAG, "onStart fired .............." + mGoogleApiClient.isConnected());
}

@Override
public void onStop() {
super.onStop();
Log.e(TAG, "onStop fired ..............");
// mGoogleApiClient.disconnect();
Log.e(TAG, "isConnected ...............: " + mGoogleApiClient.isConnected());
}

private boolean isGooglePlayServicesAvailable() {
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (ConnectionResult.SUCCESS == status) {
return true;
} else {
GooglePlayServicesUtil.getErrorDialog(status, this, 0).show();
return false;
}
}

@Override
public void onConnected(Bundle bundle) {
Log.e(TAG, "onConnected - isConnected ...............: " + mGoogleApiClient.isConnected());
startLocationUpdates();
}

protected void startLocationUpdates() {
PendingResult<Status> pendingResult = LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
Log.e(TAG, "Location update started ..............: ");
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.e(TAG, "Connection failed: " + connectionResult.toString());
}

@Override
public void onLocationChanged(Location location) {
Log.e(TAG, "Firing onLocationChanged..............................................");
mCurrentLocation = location;
mLastUpdateTime = DateFormat.getTimeInstance().format(new Date());
updateUI();
}

private void updateUI() {
Log.e(TAG, "UI update initiated .............");
if (null != mCurrentLocation) {
String lat = String.valueOf(mCurrentLocation.getLatitude());
String lng = String.valueOf(mCurrentLocation.getLongitude());

Toast.makeText(getApplicationContext(), "Longitude: " + lng + "\nLatitude: "
+ lat, Toast.LENGTH_LONG).show();
} else {
Log.e(TAG, "location is null ...............");
Toast.makeText(getApplicationContext(), "Location Null", Toast.LENGTH_LONG).show();
}
}

@Override
protected void onPause() {
super.onPause();
// stopLocationUpdates();
}

protected void stopLocationUpdates() {
// LocationServices.FusedLocationApi.removeLocationUpdates(
// mGoogleApiClient, this);
Log.e(TAG, "Location update stopped .......................");
}

@Override
public void onResume() {
super.onResume();
if (mGoogleApiClient.isConnected()) {
startLocationUpdates();
Log.e(TAG, "Location update resumed .....................");
}
}

private void displayUserDetails(){
User user = userLocalStore.getLoggedInUser();
String userdisplay = "Logged in as: " + user.username;
etLabel.setText(userdisplay);
}

private boolean authenticate(){
return userLocalStore.getUserLoggedIn();
}
}

list 文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="temp" >

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".login"
android:label="@string/title_activity_login" >
</activity>
<activity
android:name=".Register"
android:label="@string/title_activity_register" >
</activity>
</application>

</manifest>

构建文件

apply plugin: 'com.android.application'

android {
compileSdkVersion 22
buildToolsVersion "22.0.1"

defaultConfig {
applicationId "temp"
minSdkVersion 16
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.google.android.gms:play-services:7.5.0'
}

最佳答案

您应该将平板电脑上的 GooglePlay Services 服务“应用程序”升级到更高版本,例如 7.5.74,以便与客户端库版本 7.5.0 进行交互另见 this有关如何升级它的进一步说明

关于使用 FusedLocationProviderApi 的 Android 定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31434905/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com