gpt4 book ai didi

java - 使用 mFusedLocationClient 获取 Firebase 服务中的当前位置

转载 作者:太空宇宙 更新时间:2023-11-04 10:44:51 25 4
gpt4 key购买 nike

我是 java/android 开发新手,所以我的代码可能很愚蠢。

我有一个使用 google map 的网站,网站上有一个按钮,可以使用 firebase 将推送 cURL 消息发送到特定设备。我希望设备能够获取当前位置并更新我的数据库。

我正在使用 MyFirebaseMessagingService 和 onMessageReceived 来获取 cURL 消息,一切正常,但当我开始进入位置代码时它会中断。我遇到了各种错误,因此我尝试创建一个类来获取位置,然后在我的服务中调用该类的方法。

类(class):

 public class LocationMedium {

private FusedLocationProviderClient mFusedLocationClient;
private Context mContext;

public LocationMedium(Context context) {
mContext=context;
}


public void getLocation()
{
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(mContext);

if (ActivityCompat.checkSelfPermission(mContext, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mContext, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

//currently pointless

//return;
}


mFusedLocationClient.getLastLocation()
.addOnSuccessListener((Activity)mContext, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
Log.d("mytag", "2222");
// Got last known location. In some rare situations this can be null.
if (location != null) {
// Logic to handle location object
double x = location.getLatitude();
double y = location.getLongitude();


Log.d("mytag1", String.valueOf(x));

Log.d("mytag2", String.valueOf(y));
String user = PreferenceManager.getDefaultSharedPreferences(mContext).getString("USERNAME", "none");
MyFirebaseInstanceIDService firebase = new MyFirebaseInstanceIDService();
firebase.onDeadline(user, x, y);


} else //no location found
{
String user = PreferenceManager.getDefaultSharedPreferences(mContext).getString("USERNAME", "none");
MyFirebaseInstanceIDService firebase = new MyFirebaseInstanceIDService();
firebase.onDeadline(user, 0, 0);
Log.d("mytag", "hmm");
}
}
});
//END OF LOCATION

Log.d("mytag", "theend");


}//end of run

}

我从 MyFirebaseMessagingService 中调用它

        if (Objects.equals(user, " location ")) {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
public void run() {
LocationMedium task = new LocationMedium(context);
task.getLocation();
}
});
}

我得到的错误是:

MyFirebaseMessagingService cannot be cast to android.app.Activity

它似乎在这一行:

mFusedLocationClient.getLastLocation()

我尝试从以下位置删除( Activity )位:

 .addOnSuccessListener((Activity) mContext, new OnSuccessListener<Location>() 

并将其更改为:

 .addOnSuccessListener(mContext, new OnSuccessListener<Location>()

但随后我收到错误:

Cannot resolve method 'addOnSuccessListener...'

也许我的处理方式完全错误。任何帮助/建议将不胜感激。谢谢。

最佳答案

.addOnSuccessListener(mContext, new OnSuccessListener<Location>()

用这个替换上面的代码,

.addOnSuccessListener(new OnSuccessListener<Location>()

为我工作。

关于java - 使用 mFusedLocationClient 获取 Firebase 服务中的当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48529963/

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