zoukankan      html  css  js  c++  java
  • android开发之Intent.setFlags()_让Android点击通知栏信息后返回正在运行的程序

    android开发之Intent.setFlags()_让Android点击通知栏信息后返回正在运行的程序

     
     

    在应用里使用了后台服务,并且在通知栏推送了消息,希望点击这个消息回到activity,

    结果总是存在好几个同样的activity,就算要返回的activity正在前台,点击消息后也会重新打开一个一样的activity,返回好几次才能退出,

    而不能像qq之类的点击通知栏消息回到之前存在的activity,如果存在就不再新建一个activity

    说的有点绕,如果是遇到此类问题的肯定能懂,没遇到过的估计看不懂我这混乱的表达了.....

    经过查阅资料,通过如下方式解决了,

    其实主要功劳就是第10行,

    public void shownotification(String msg)
        {
            NotificationManager barmanager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
            Notification notice = new Notification(android.R.drawable.stat_notify_chat,"服务器发来信息了",System.currentTimeMillis());
            notice.flags=Notification.FLAG_AUTO_CANCEL;
            Intent appIntent = new Intent(Intent.ACTION_MAIN);
            //appIntent.setAction(Intent.ACTION_MAIN);
            appIntent.addCategory(Intent.CATEGORY_LAUNCHER);
            appIntent.setComponent(new ComponentName(this.getPackageName(), this.getPackageName() + "." + this.getLocalClassName())); 
            appIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);//关键的一步,设置启动模式
             PendingIntent contentIntent =PendingIntent.getActivity(this, 0,appIntent,0);
            notice.setLatestEventInfo(this,"通知","信息:"+msg, contentIntent);
            barmanager.notify(STATUS_BAR_ID,notice);
           
        }
    
  • 相关阅读:
    C# Asp.net中简单操作MongoDB数据库(二)
    Windows下使用TeamViewer连接远程服务器,以及解决“远程桌面关闭后TeamViewer不能连接”的问题
    存储过程
    C# 使用MongoDB遇到的问题
    C# Asp.net中简单操作MongoDB数据库(一)
    Windows下MongoDB设置用户、密码
    退役狗也要搬博客
    codeforces840E In a Trap
    uoj207共价大爷游长沙
    bzoj2662: [BeiJing wc2012]冻结 最短路 建图
  • 原文地址:https://www.cnblogs.com/yaya-Android/p/4098960.html
Copyright © 2011-2022 走看看