Activity在onResume裏調用getIntent()拿不到數據

有時候MainActivity一般都是設置啓動模式爲:singleTop,也就是說如果MainActivity處於棧頂位置的話就不會從新創建實例,也就是不會調用Activity的onCreate方法,會調用onResume方法,所以從通知欄直接打開MainActivity就會在onResume裏面拿不到intent攜帶的數據,處理方法如下:

處理方法

    /**
     *
     * 重寫此方法,加上setIntent(intent);否則在onResume裏面得不到intent
     * @param intent intent
     */
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
    }

然後在onResume裏面就可以拿到intent的數據了

    @Override
    protected void onResume() {
        super.onResume();
        Intent intent = getIntent();
        String message = intent.getStringExtra("message");
        //操作。。。。。。
    }

NotificationListenerService

有篇文章寫有關NotificationListenerService,但我沒親測過,文章地址:
* http://blog.csdn.net/cankingapp/article/details/50858229

參考

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章