Activity在onResume里调用getIntent()拿不到数据

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Song_74110/article/details/70174942

有时候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

参考

猜你喜欢

转载自blog.csdn.net/Song_74110/article/details/70174942