zoukankan      html  css  js  c++  java
  • 启动App的Intent

    类似桌面图标打开App的Intent

    程序中需要一种通知,点击后的效果需要像点击桌面图标那样:

    • 程序在前台就什么也不干。
    • 程序在后台,就切换到前台。
    • 程序未启动,就启动程序。

    点击通知后,通知本身跳转到Receiver,然后onReceive方法里面处理通知的跳转intent,startActivity使用onReceive的context或者Application都行。
    创建打开app的intent,自己尝试了一些写法有点bug——多次打开app——偶然性,而且不稳定?
    最终从 launcher的源码中找到了系统桌面图标的点击打开app使用的Intent:

    ComponentName className = new ComponentName(packageName, activityName);
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    intent.setComponent(className);
    int launchFlags = Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED;
    intent.setFlags(launchFlags);
    

    注意:
    上面最关键的是launchFlags,可以避免多次启动程序:也就是程序已经启动了,在后台,然后点击通知后又闪出启动页面。

  • 相关阅读:
    uva111 History Grading
    UVA 10100Longest Match
    UVA 147Dollars
    归并排序模板
    找礼物(find)
    水流(water)dfs
    细菌(disease) 位运算
    单词接龙
    关于jquery的each遍历,return只终止当前循环,不好使的解决办法
    jquery中ajax回调函数使用this
  • 原文地址:https://www.cnblogs.com/everhad/p/5385105.html
Copyright © 2011-2022 走看看