NotificationHandlerActivity

NotificationHandlerActivity

我已经建立了一个通知并正确显示它,但是我不知道如何将数据传递给活动。我从意图中拉出一个字符串以显示为通知的标题,但是我需要拉出第二个字符串,并让NotificationHandlerActivity处理它。

//内部intentservice

private void sendNotification(Bundle extras) {
    Intent intent=new Intent(this, NotificationHandlerActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.putExtra("link", extras.getString("link"));
    mNotificationManager = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
    long[] vibrate = {100L, 75L, 50L};
    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.abc_ic_menu_copy_mtrl_am_alpha)
                    .setContentTitle(extras.getString("title"))
                    .setOngoing(false)
                    .setAutoCancel(true)
                    .setVibrate(vibrate);
    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}


//内部NotificationHandlerActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bundle b = getIntent().getExtras();
}

最佳答案

您应该在Intent上使用其他功能。因为您的意图当前是匿名的,所以您不能这样做。附加功能是基本的键值存储。见下文:

public static final String KEY_SECOND_STRING = "keySecondString";
...

    ...
    String secondString = "secondString";
    mNotificationManager = (NotificationManager)
         this.getSystemService(Context.NOTIFICATION_SERVICE);

    Intent intent = new Intent(this, NotificationHandlerActivity.class);
    intent.putExtra(KEY_SECOND_STRING, secondString);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
    ...


然后,您可以从NotificationHandlerActivity中访问该secondString。

@Override
public void onCreate(Bundle sIS){
    super.onCreate();
    String secondString = getIntent().getStringExtra("keySecondString");
    ...
}

09-28 07:30