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

标签 java android

我已经尝试了通过 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/

相关文章:

java - 其他文件中的 JSP 模板 - Spring Boot

Android:如何做这个框架涂料?

java - 通过 Unicode 字符或其他方式在 Android TextView 中显示情感图标

android - 如何完全禁用android上的菜单按钮?

java - 为什么这种锁变化对 JDBC 性能的影响如此之大?

Java 泛型和原始类型

android - 删除以前的标记并在 Google Map v2 中添加新标记

android - 如何跨线程传递 RealmResults

java - Primefaces:转换错误设置值 'Admin' 为 'null Converter'

java - 在 Java 中向按钮添加 ActionListener