android 点击通知栏返回应用 ,非启动一个新Activity

本文介绍了如何通过设置Intent的特定标志来避免在Android应用中重新运行Activity,而是在当前Activity上继续操作,同时展示了创建通知的具体实现。

 

再使用如下的 Intent 设置:

Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);

就可以返回当前 Activity 而不是重新运行了一个 activity。

复制代码
public Notification createNotification() {
        Notification nf = new Notification(android.R.drawable.stat_sys_download, "正在为您下载...", System.currentTimeMillis());

        nf.flags = Notification.FLAG_ONGOING_EVENT;
        nf.contentView = new RemoteViews(getPackageName(), R.layout.notification_download);
        setNotificationInfo(nf, 0, "..M", null, true);

        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        
        nf.contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        return nf;
    }
复制代码

 

转载于:https://www.cnblogs.com/taoweiji/p/3359581.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值