Intent.putExtra(键,值)不起作用

Intent.putExtra (key,value) is not working

我得到了一个ValueEventListener

   query.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
                final Intent intent = new Intent(BackgroundService.this, NextActivity.class);
                Task<String> t=genrate();//not null

                        t.addOnCompleteListener(new OnCompleteListener<String>() {
                            @Override
                            public void onComplete(@NonNull Task<String> task) {
                                if (task.isSuccessful()) {
                                 intent.putExtra("token",task.getResult());

            }
        } });
       intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                PendingIntent pendingIntent = PendingIntent.getActivity(BackgroundService.this, 0, intent, 0);
                mBuilder.setContentIntent(pendingIntent).setAutoCancel(true);
                mBuilder.setVisibility(VISIBILITY_SECRET);
                NotificationManagerCompat notificationManager = NotificationManagerCompat.from(BackgroundService.this);
                notificationManager.notify(121, mBuilder.build());

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

NextActivity 中缺少 token 键。

getIntent().getExtras().getString("token")

returnnull

可能是什么问题?

您需要此标志:PendingIntent.FLAG_UPDATE_CURRENT 在您的 PendingIntent
变化:

PendingIntent pendingIntent = PendingIntent.getActivity(BackgroundService.this, 0, intent, 0);

至:

PendingIntent pendingIntent = PendingIntent.getActivity(BackgroundService.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

addValueEventListener() 是异步的。您只会在 onDataChange() 内获得结果。将显示通知的代码移动到一个单独的方法中,并在 onDataChange().

中调用它
query.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                final Intent intent = new Intent(BackgroundService.this, NextActivity.class);
                Task < String > t = genrate(); //not null

                t.addOnCompleteListener(new OnCompleteListener < String > () {
                    @Override
                    public void onComplete(@NonNull Task < String > task) {
                        if (task.isSuccessful()) {
                            intent.putExtra("token", task.getResult());

                            //Show notification here
                            showNotification(intent);
                        }
                    }
                });

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
});

显示通知的单独方法。

private void showNotification(Intent intent) {
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(BackgroundService.this, 0, intent, 0);
    mBuilder.setContentIntent(pendingIntent).setAutoCancel(true);
    mBuilder.setVisibility(VISIBILITY_SECRET);
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(BackgroundService.this);
    notificationManager.notify(121, mBuilder.build());
}

你没有打电话给 startActivity(intent);

query.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                final Intent intent = new Intent(BackgroundService.this, NextActivity.class);
                Task < String > t = genrate(); //not null

                t.addOnCompleteListener(new OnCompleteListener < String > () {
                    @Override
                    public void onComplete(@NonNull Task < String > task) {
                        if (task.isSuccessful()) {
                            intent.putExtra("token", task.getResult());
                            startActivity(intent);
                        }
                    }
                });
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                PendingIntent pendingIntent = PendingIntent.getActivity(BackgroundService.this, 0, intent, 0);
                mBuilder.setContentIntent(pendingIntent).setAutoCancel(true);
                mBuilder.setVisibility(VISIBILITY_SECRET);
                NotificationManagerCompat notificationManager = NotificationManagerCompat.from(BackgroundService.this);
                notificationManager.notify(121, mBuilder.build());

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

你的 task.getResult() returns 字符串吗?如果是,则在第二个 activity.

中尝试此操作
Bundle extras = getIntent().getExtras();
if (extras != null) {
    String result = extras.getString("token");                
    System.out.println("yeah"+result);
}