android 点击通知栏返回应用 研究

此前在网上找到的一段代码如下:

    //自定义的setNotiType()方法
    @SuppressWarnings("deprecation")
    private void backNotification2() {
        // 建立新的Intent
        Intent notifyIntent = new Intent(this, ResultActivity.class);
        notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        // 建立PendingIntent作为设定递延执行的Activity
        PendingIntent appIntent = PendingIntent.getActivity(EmulatorActivity.this, 0, notifyIntent, 0);
        // 建立Notification,并设定相关参数
        Notification myNoti = new Notification();
        // 设置状态栏显示的图标
        myNoti.icon = R.drawable.ic_launcher;
        // 设定状态栏显示的文字信息
        myNoti.tickerText = getString(R.string.hint_click_to_back);
        // 发出预设的身影
        myNoti.defaults = Notification.DEFAULT_LIGHTS;
        myNoti.flags = Notification.FLAG_NO_CLEAR; //不可清楚
        // 设置通知消息
        myNoti.setLatestEventInfo(EmulatorActivity.this, 
                emulator.getCurMrpFile().getAppName(), 
                getString(R.string.hint_click_to_back), 
                appIntent);
        // 发送出消息
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(1001, myNoti);
        
        isNotificationShow = true;
        MobclickAgent.onEvent(this, "backrun", MyUtils.getTimeNow());
    }

他的原理是启动一个新的activity ResultActivity 这个 Activity 啥都没做,在 onCreate 里直接 finish(),这样就可以实现返回按下 home 前的 Activity ,但是这样做看起来有点不专业就是。

于是研究了一下 ApiDemo 的 Notification 使用方法,在 Notification/Status Bar 找到了我想要的效果,但是看他实现的方式有点复杂,不适合我的使用场景,他适合如下场景:

当你想要返回的 Activity 不在 Activity 堆栈最上层

 

于是在上 StackOverFlow 找到了这篇帖子:

http://stackoverflow.com/questions/4089213/android-notification-bar-open-last-active-activity

大概意思是,在 Mainfest 里设置 Activity flags android:launchMode="singleTop"

再使用如下的 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/yichouangle/archive/2013/04/27/3047542.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值