gpt4 book ai didi

java - 通过多种 Intent 正确地传递值

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

我已经尝试了通过 Intent 发送数据的所有好的旧时尚方法,例如 getExtra 和 putExtra。然而,这条特定的信息不会通过 Intent 传播。变量在这里初始化(driverId):

   private void findDriver() {
DatabaseReference drivers = FirebaseDatabase.getInstance().getReference(Common.driver_tbl);
GeoFire gfDrivers = new GeoFire(drivers);
GeoQuery geoQuery = gfDrivers.queryAtLocation(new GeoLocation(mLastLocation.getLatitude(),mLastLocation.getLongitude()),radius);
geoQuery.removeAllListeners();
geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
@Override
public void onKeyEntered(String key, GeoLocation location) {
if(!Common.isDriverFound){
Common.isDriverFound = true;
Common.driverId = key;
btnPickupRequest.setText("CALL SUB");
Toast.makeText(Home.this,""+key, Toast.LENGTH_SHORT).show();
}
}

然后,当单击此处的按钮时,我会发送 driverId 的信息:

 Intent intent = new Intent(Home.this, CallDriver.class);

intent.putExtra("driverId",marker.getSnippet());
intent.putExtra("lat",mLastLocation.getLatitude());
intent.putExtra("lng",mLastLocation.getLongitude());
startActivity(intent);

然后我将文本 Intent 中的信息称为如下:

 if (driverId != null && !driverId.isEmpty())
if (getIntent() !=null) {
driverId = getIntent().getStringExtra("driverId");
}
sendRequestToDriver(driverId, mService, getBaseContext(), mLastLocation);
}

我向驱动程序发送请求的方法是:

 public static void sendRequestToDriver(String driverId,final IFCMService mService,final Context context,final Location currentLocation) {

DatabaseReference tokens = FirebaseDatabase.getInstance().getReference(Common.token_tbl);

tokens.orderByKey().equalTo(driverId)
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot postSnapShot:dataSnapshot.getChildren())
{
Token token = postSnapShot.getValue(Token.class);

//String json_lat_lng = new Gson().toJson(new LatLng(mLastLocation.getLatitude(),mLastLocation.getLongitude()));
String riderToken = FirebaseInstanceId.getInstance().getToken();



Map<String,String> content = new HashMap<>();
content.put("customer", riderToken);
content.put("driverId",driverId);
content.put("lat",String.valueOf(currentLocation.getLatitude()));
content.put("lng",String.valueOf(currentLocation.getLongitude()));
DataMessage dataMessage = new DataMessage(token.getToken(),content);
Log.d(String.valueOf(dataMessage), "here big boy"+dataMessage);

mService.sendMessage(dataMessage).enqueue(new Callback<FCMResponse>() {
@Override
public void onResponse(Call<FCMResponse> call, Response<FCMResponse> response) {
if(response.body().success == 1)
Toast.makeText(context, "Request Sent!", Toast.LENGTH_SHORT).show();
else
Toast.makeText(context, "Failed", Toast.LENGTH_SHORT).show();
}

@Override
public void onFailure(Call<FCMResponse> call, Throwable t) {
Log.e("Error", t.getMessage());
}
});

}
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});

}

现在,当我尝试接收有关 Intent RateActivity 的信息时,我得到一个空值。如何确保字符串 driverId 已正确初始化或数据在这些 Intent 中保持不变?

最佳答案

试试这个 intent.putExtra("driverId",marker.getSnippet().toString());
intent.putExtra("lat",mLastLocation.getLatitude().toString());
Intent.putExtra("lng",mLastLocation.getLongitude().toString());

关于java - 通过多种 Intent 正确地传递值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60180723/

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