<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Calvin's Tech Space

    成于堅忍,毀于浮躁

       :: 首頁 :: 聯系 :: 聚合  :: 管理
    背景:
    用戶按下Home鍵將程序置于后臺運行或者應用啟動其他activity,比如系統瀏覽器,短信等,需要向系統發送通知,用戶做完別的操作后,點擊通知欄,回到應用。

    問題一
    在什么時機發送通知?
    用戶按下Home的事件在應用層時捕捉不到的,因此只能從activity生命周期方法著手。

    方法一:
    系統所有activity繼承一個BaseActivity,在BaseActivity中維護一個當前可見的activity數組:
    protected static ArrayList sVisibleActivities = new ArrayList();

    在onResume中,將當前activity保存,同時清除所有通知:
    protected void onResume()
    {
    if (!sVisibleActivities.contains(this))
    {
    sVisibleActivities.add(this);
    }

    // 清除系統消息
    mNotificationManager.cancel(R.id.notify);
    }

    在onStop中,清除保存的當前activity:
    protected void onStop()
    {
    if (sVisibleActivities.contains(this))
    {
    sVisibleActivities.remove(this);
    }

    // 如果當前沒有可見的activity,則發送系統通知
    if (sVisibleActivities.isEmpty())
    {
    sendBackgroundNotify();
    }

    super.onStop();
    }

    這種方式在大多數情況下工作良好,可以達到需求,但是問題時,當前臺的activity被至于后臺時,onStop()方法不一定會被調用,因此通知有可能不會被發出!

    方法二:
    幾經周折,發現activity有一個生命周期方法可以達到目的:

    protected void onUserLeaveHint ()

    Since: API Level 
    3
    Called as part of the activity lifecycle when an activity is about to go into the background as the result of user choice.
    For example, when the user presses the Home key, onUserLeaveHint() will be called, but when an incoming phone call causes the in
    -call Activity to be automatically brought to the foreground,
     onUserLeaveHint() will not be called on the activity being interrupted. In cases when it is invoked, 
    this method is called right before the activity's onPause() callback.
    This callback and onUserInteraction() are intended to help activities manage status bar notifications intelligently; specifically, for helping activities determine the proper time to cancel a notfication.

    從文檔來看,這個方法似乎就是為了按下Home鍵時這樣的場景設計的。
    這樣,在onUserLeaveHint里發出系統通知即可。
    但是問題又來了,如果啟動應用,從一個activity依次調用startActivity,finish關閉自己,啟動一個新的activity時,onUserLeaveHint也會被調用....

    再次翻閱文檔,發現Intent中的一個Flag:

    public static final int FLAG_ACTIVITY_NO_USER_ACTION

    Since: API Level 
    3
    If set, 
    this flag will prevent the normal onUserLeaveHint() callback from occurring on the current frontmost activity before it is paused as the newly-started activity is brought to the front.

    Typically, an activity can rely on that callback to indicate that an explicit user action has caused their activity to be moved out of the foreground.
    The callback marks an appropriate point in the activity
    's lifecycle for it to dismiss any notifications that it intends to display "until the user has seen them," such as a blinking LED.
    If an activity is ever started via any non-user-driven events such as phone-call receipt or an alarm handler, this flag should be passed to Context.startActivity, ensuring that the pausing activity does not think the user has acknowledged its notification.

    這正是我想要的,這樣,在啟動activity時,往intent中加上這個flag,onUserLeaveHint就不會再被調用了,hoory...
    posted on 2010-11-22 13:26 calvin 閱讀(5412) 評論(1)  編輯  收藏 所屬分類: Android

    Feedback

    # re: 利用onUserLeaveHint發送后臺運行通知 2012-12-28 14:53 lzq
    Intent i = new Intent();
    i.addFlags(i.FLAG_ACTIVITY_NO_USER_ACTION);


    這樣添加flag 嗎?  回復  更多評論
      

    主站蜘蛛池模板: 精品国产污污免费网站入口在线 | 亚洲A∨精品一区二区三区| 国产一级a毛一级a看免费视频| 久久亚洲精品国产亚洲老地址| 亚洲AV无码国产精品麻豆天美| 免费乱码中文字幕网站| 成人影片麻豆国产影片免费观看 | 四虎影库久免费视频| 中文字幕影片免费在线观看| 在线观看免费无码专区| 特黄aa级毛片免费视频播放| 亚洲乱色伦图片区小说| 亚洲国产理论片在线播放| 亚洲av无码不卡一区二区三区 | 一级毛片a免费播放王色| 亚洲欧美成人av在线观看| 亚洲欧洲日产v特级毛片| 久热综合在线亚洲精品| 国产亚洲美女精品久久久2020| 国产免费69成人精品视频| 成年女人男人免费视频播放| 999国内精品永久免费观看| 一级毛片免费视频| 无码人妻一区二区三区免费看 | 国产亚洲3p无码一区二区| 日韩亚洲变态另类中文| 亚洲国产精品成人网址天堂| 午夜国产羞羞视频免费网站| 国产一区视频在线免费观看| 日韩伦理片电影在线免费观看| 成人免费a级毛片无码网站入口 | 国产av无码专区亚洲国产精品| heyzo亚洲精品日韩| 亚洲国产成人乱码精品女人久久久不卡 | 99re视频精品全部免费| 久久免费观看国产99精品| 91高清免费国产自产拍2021| 24小时日本电影免费看| 我的小后妈韩剧在线看免费高清版| 日本免费网站视频www区| 在线a级毛片免费视频|