gpt4 book ai didi

android - 无法使用 Pubnub 教程解析方法 requestLocationUpdates

转载 作者:行者123 更新时间:2023-11-30 01:29:00 33 4
gpt4 key购买 nike

好的,这个问题已经被问到,我已经尝试了提供的所有解决方案,但似乎都没有用,

我正在使用一个在 Android 应用程序上实时显示我的位置的 Pubnub 教程,我遇到了一些我无法解决的错误,其中一个主要是“无法解析方法 requestLocationUpdates”

在之前的帖子里看到有添加,import android.location.LocationListener;到进口,但它没有改变任何东西。

如果有人对此有任何解决方案,那就太好了,如果您知道的话,还有我的其他错误。

主要 Activity :

package nixerpubcom.nixerpub;

import android.location.Location;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.pubnub.api.PubnubError;

import android.location.LocationListener;

import org.json.JSONException;
import org.json.JSONObject;

import javax.security.auth.callback.Callback;

public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks {

//Create Google API Client
private GoogleApiClient mGoogleApiClient;
//Create pubnub variable
private Pubnub mPubnub;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

//Start The Google Client
this.buildGoogleApiClient();
mGoogleApiClient.connect();

//Retrieve pubnub keys
mPubnub = new Pubnub ("pub-c-fe4ed754-2b9d-4563-abb6-63c3f048f9ad","sub-c-82de7130-eacb-11e5-8346-0619f8945a4f");

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}

//Google Api build Method
private synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this).addApi(LocationServices.API)
.build();
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

// Implement the Connection Callback
@Override
public void onConnected(Bundle connectionHint) {
LocationRequest mLocationRequest = createLocationRequest();
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
}
@Override
public void onConnectionSuspended(int cause) {

Log.d(TAG, "Connection to Google API Suspended ");

}

private LocationRequest createLocationRequest() {
LocationRequest mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(10000);
mLocationRequest.setFastestInterval(5000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
return mLocationRequest;
}

@Override
public void onLocationChanged(Location location) {
broadcastLocation(location);
}


private void broadcastLocation(Location location) {
JSONObject message = new JSONObject();
try {
message.put("lat", location.getLatitude());
message.put("lng", location.getLongitude());
message.put("alt", location.getAltitude());
} catch (JSONException e) {
Log.e(TAG, e.toString());
}
mPubnub.publish("A Channel Name", message, publishCallback);
}

KeyEvent.Callback publishCallback = new Callback() {
@Override
public void successCallback(String channel, Object response) {
Log.d("PUBNUB", response.toString());
}

@Override
public void errorCallback(String channel, PubnubError error) {
Log.e("PUBNUB", error.toString());
}
};

}

This is the error I get for the Cannot resolve methods requestLocationUpdates, my other errors are also in there which is a TAG error and a pubnub error

我是新手,所以如果问题格式不正确,我深表歉意。

最佳答案

  1. 在 requestLocationUpdates 方法中:

    LocationServices.FusedLocationApi.requestLocationUpdates( mGoogleApiClient, mLocationRequest, this);

    期望调用 (MainActivity) 的类实现 com.google.android.gms.location.LocationListener,因此您需要实现它,或使用匿名类更改它:

    LocationServices.FusedLocationApi.requestLocationUpdates(
    mGoogleApiClient, mLocationRequest, new LocationListener() {
    @Override
    public void onLocationChanged(Location location) {
    // Here you have the updated location
    }
    });
    }
  2. 字符串 TAG 没有在这个类中定义,所以在你的类中创建一个像这样的字段:

    private static final String TAG = "MainActivity";

    提示:在 Android Studio 中键入“logt”+ enter 以生成此行。

  3. 你想要的回调

    KeyEvent.Callback publishCallback = new Callback() {...}

    不是来自 KeyEvent,而是来自 com.pubnub.api.Callback

    com.pubnub.api.Callback publishCallback = new Callback() {...}

请确保您的进口是正确的,并且您要求在 list 中提供位置:

android.permission.ACCESS_COARSE_LOCATION

android.permission.ACCESS_COARSE_LOCATION

关于android - 无法使用 Pubnub 教程解析方法 requestLocationUpdates,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36018289/

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