zoukankan      html  css  js  c++  java
  • 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;
        }
  • 相关阅读:
    (网页)中的简单的遮罩层
    (后端)shiro:Wildcard string cannot be null or empty. Make sure permission strings are properly formatted.
    (网页)jQuery的时间datetime控件在AngularJs中使用实例
    Maven Myeclipse 搭建项目
    MyBatis 环境搭建 (一)
    java 常用方法
    XML 基础
    JS BOM
    js 事件
    js 的使用原则
  • 原文地址:https://www.cnblogs.com/yichouangle/p/3047542.html
Copyright © 2011-2022 走看看