gpt4 book ai didi

android - 为什么我们使用 TaskStackBuilder?

转载 作者:IT老高 更新时间:2023-10-28 21:54:34 29 4
gpt4 key购买 nike

为什么我们在创建通知时使用TaskStackBuilder?我不明白它背后的逻辑。

谁能解释一下。

public void showText(final String text){
Intent intent = new Intent (this, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(getString(R.string.app_name))
.setAutoCancel(true)
.setPriority(Notification.PRIORITY_MAX)
.setDefaults(Notification.DEFAULT_VIBRATE)
.setContentIntent(pendingIntent)
.setContentText(text)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICACTION_ID, notification);
}

最佳答案

假设您有一个电子邮件发送应用程序,其中有两个 Activity 。一个是MainActivity,它有电子邮件列表,另一个用于显示电子邮件(EmailViewActivity)。因此,现在当您收到新电子邮件时,您会在状态栏上显示通知。现在,您希望在用户单击该电子邮件时查看该电子邮件,并且在用户单击返回按钮后显示该电子邮件后,您希望显示电子邮件列表 Activity (MainActivity)。对于这种情况,我们可以使用 TaskStackBuilder。见下例:

public void showEmail(final String text){

Intent intent = new Intent (this, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(intent);
Intent intentEmailView = new Intent (this, EmailViewActivity.class);
intentEmailView.putExtra("EmailId","you can Pass emailId here");
stackBuilder.addNextIntent(intentEmailView);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(getString(R.string.app_name))
.setAutoCancel(true)
.setPriority(Notification.PRIORITY_MAX)
.setDefaults(Notification.DEFAULT_VIBRATE)
.setContentIntent(pendingIntent)
.setContentText(text)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICACTION_ID, notification);
}

希望你能理解。

点击以下网址了解更多详情: http://developer.android.com/reference/android/support/v4/app/TaskStackBuilder.html http://developer.android.com/guide/components/tasks-and-back-stack.html http://www.programcreek.com/java-api-examples/index.php?api=android.app.TaskStackBuilder

关于android - 为什么我们使用 TaskStackBuilder?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36912325/

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