notification中发起activity问题

本文介绍了如何使用Android系统服务创建并显示自定义的通知,并详细解释了如何从通知启动Activity时获取Intent中的额外信息,以便执行特定操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

String ns = Context.NOTIFICATION_SERVICE; 
   
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); 
   
int icon = R.drawable.icon;      
   
long when = System.currentTimeMillis(); 
   
Notification notification = new Notification(icon, "Test Notification", when); 
 
 
   
Context context = getApplicationContext();       
 
   
Bundle bundle = new Bundle(); 
    bundle
.putString("action", "view"); 
   
Intent notificationIntent = new Intent(this, MainActivity.class); 
    notificationIntent
.putExtras(bundle); 
 
   
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK); 
    notification
.setLatestEventInfo(context, contentTitle, contentText, contentIntent);      
    mNotificationManager
.notify(1, notification); 

 

下面是要取出"action", 的值

Bundle bundle = this.getIntent().getExtras(); 
 
   
if(bundle != null) 
   
{ String action = bundle.getString("action"); 
            performAction
(action) 
   
} 
程序呢按照预期执行,只是当我返回程序,把状态栏缩小,然后回到屏幕通过程序的图标发起程序竟然从上次notivication点击的时候发起,

       

可以通过

Intent intent = getIntent(); 
int flags = intent.getFlags(); 
boolean launchedFromHistory = ((flags & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY)

来判断一下是不是从历史中发起的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值