android - 如何处理通知中的待处理意图
问题描述
如果我有一个应用程序
A -> B -> C -> D
在堆栈中。我怎么能从通知中打开一个活动并使堆栈成为
A -> B -> C -> D -> E
如果应用程序被终止,我如何从通知中打开应用程序,例如从通知中打开 E 活动,但在再次打开应用程序时阻止它启动 [例如,我打开 E 活动然后我回退以退出应用程序,然后当我再次打开应用程序时我希望它打开 A 活动(根活动)而不是 E 活动。]
// When Open Application if app is terminated.
val intent = Intent(activity!!, SampleActivity::class.java)
val pendingIntent= PendingIntent.getActivity(activity!!, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
with(NotificationManagerCompat.from(activity!!)) {
notify(
java.lang.System.currentTimeMillis().toInt(), notiBuilder
.setContentTitle("Title")
.setContentIntent(pendingIntent)
.setContentText("body")
.setNumber(1)
.setSmallIcon(R.drawable.notification_icon_background)
.setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL)
.build())
}
// When Open Application if app is not terminated.
val intent = Intent(activity!!, HomeActivity::class.java)
intent.putExtra("FurtherActivity", 1)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
val pendingIntent= PendingIntent.getActivity(activity!!, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
with(NotificationManagerCompat.from(activity!!)) {
notify(
java.lang.System.currentTimeMillis().toInt(), notiBuilder
.setContentTitle("Title")
.setContentIntent(pendingIntent)
.setContentText("body")
.setNumber(1)
.setSmallIcon(R.drawable.notification_icon_background)
.setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL)
.build())
}
// HomeActivity
override fun onCreate() {
......
......
......
if(intent.getIntExtra("FurtherActivity", 0) == 1) {
SampleActivity.start(this@HomeActivity)
viewpager.currentItem = 0
tab_layout.getTabAt(0)!!.select()
}
......
......
}
override fun onNewIntent(newIntent: Intent?) {
super.onNewIntent(Intent())
if(intent.getIntExtra("FurtherActivity", 0) == 1) {
SampleActivity.start(this@HomeActivity)
viewpager.currentItem = 0
tab_layout.getTabAt(0)!!.select()
}
}
解决方案
您可以创建一个处理意图结果的活动,并将该结果根据要求传递给特定活动,然后只需将 HandleIntent 活动传递给通知意图,并使用 ActivityName 作为参数来打开特定活动。
推荐阅读
- ios - 在swift 4中将对象的创建限制为工厂
- web-deployment - 在网站部署期间确保客户端的一致性
- node.js - 如果需要 Nodejs 文件系统 (fs),如何在 Vue 中使用 NPM 包
- javascript - 为什么@babel/node 找不到要导入的本地文件?
- c# - 检测屏幕 1 和屏幕 2 ,在屏幕 1 处打开(仅当没有屏幕时)
- c# - c#组合多个函数的yield返回
- java - 即使节点在firebase中退出也能获得空值
- javascript - react-native 如何在每次点击视图时执行功能
- azure-webjobs - 我们可以在不重新部署代码的情况下更新客户端 ID 和机密 ID
- java - Netty:使用 sync() 和 addListener() 的区别