zoukankan      html  css  js  c++  java
  • Android 响应notification事件

    两种情况,第三种情况,类似于微信,点击消息,跳到聊天框,退出后回到主页

    第一种情况就是:

    点击Notification ——>进入secActivity ——> back键 ——> 退出应用

    PendingIntent contentIntent = PendingIntent.getActivity(context, 0,  intent, PendingIntent.FLAG_CANCEL_CURRENT);  

    第二种情况:

    点击Notification ——>进入secActivity ——> back键 ——> 退到mainActivity ——>back键 ——>退出应用

    需要添加intent数组,这里是两个单位,所以先打开的是secActivity(intent[1]),

    需要在Manifest中对指定的Activity设置属性

    <activity android:name=".secActivityl"
            android:launchMode="singleTask"
            android:taskAffinity=""
            android:excludeFromRecents="true">
    </activity>

    PendingIntent提供了个静态方法getActivities,里面可以设置一个Intent数组,用来指定一系列的Activity。

    Intent[] makeIntentStack(Context context) {
        Intent[] intents = new Intent[2];
        intents[0] = Intent.makeRestartActivityTask(new ComponentName(context, com.example.notificationtest.MainActivity.class));
        intents[1] = new Intent(context,  com.example.notificationtest.SubActivity.class);
        return intents;
    }

    其中需要注意的是Intent.makeRestartActivityTask方法,这个方法用来创建activity栈的根activity

    接下来,创建并显示Notification:

    void showNotification(Intent intent) {
        Notification notification = new Notification(
                R.drawable.status_icon, 
                "消息栏顶部标题",
                System.currentTimeMillis());
    
        PendingIntent contentIntent = PendingIntent.getActivities(
                this,
                0,
                makeIntentStack(this), 
                PendingIntent.FLAG_CANCEL_CURRENT);
        notification.setLatestEventInfo(
                this, 
                "下拉消息栏标题",
                "消息内容", 
                contentIntent);
        notification.flags |= Notification.DEFAULT_ALL;
    
        mNM.notify(1, notification);
    }

     第三种:

    点击Notification ——>进入mainActivity——>根据bundle信息——>进入secActivity ——> back键 ——> 退到 mainActivity——>back键 ——>退出应用  

    这里需要注意的是,当点击notification的时候,应用是否已经在栈中

    1.如果应用不在,也就是未打开状态,那么使用singltask模式打开,bundle消息可以在onstart中获取

    2.如果应用已经在前台,那么点击notification是不会出发onstart更新intent消息的,需要使用

    @Override
        protected void onNewIntent(Intent intent) {
            super.onNewIntent(intent);
            Log.e("tag", "onNewINtent执行了");
            setIntent(intent);
            String ringName = intent.getStringExtra("ringName");
            Log.e("tag", ringName+"传过来的值");
            if (ringName != null) {
                pager.setCurrentItem(1);
            }
        }
  • 相关阅读:
    绘制矩形
    绘制线条
    画直线
    画弧线
    绘制贝塞尔曲线
    我关注的一些博客或资源链接
    vim必记指令
    mac下使用vim gcc/g++ 编译cpp(c++)文件并运行
    机器学习算法中如何选取超参数:学习速率、正则项系数、minibatch size
    正则化方法:L1和L2 regularization、数据集扩增、dropout
  • 原文地址:https://www.cnblogs.com/bkycjj/p/5031342.html
Copyright © 2011-2022 走看看