Android 通知栏(Notification)点击跳转页面

1,通知栏应用:

Notification,是一种具有全局效果的通知,可以在系统的通知栏中显示。当 APP 向系统发出通知时,它将先以图标的形式显示在通知栏中。用户可以下拉通知栏查看通知的详细信息。通知栏和抽屉式通知栏均是由系统控制,用户可以随时查看。 

2,代码:

【1】显示通知栏。跳转页面   

//[1]获取通知的管理者

        NotificationManager barmanager = (NotificationManager)getSystemService(this.NOTIFICATION_SERVICE);

        Notification notice;

        //[2] 获取Notification

        Notification.Builder builder = new Notification.Builder(this).setTicker("我是测试内容")

                .setSmallIcon(R.mipmap.ic_launcher).setWhen(System.currentTimeMillis());

        //开启另一个页面

        Intent appIntent = new Intent(this,SplashActivity.class);

        appIntent.setAction(Intent.ACTION_MAIN);

        appIntent.addCategory(Intent.CATEGORY_LAUNCHER);

        //设置启动模式

        appIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);



        PendingIntent contentIntent =PendingIntent.getActivity(this, 0,appIntent,PendingIntent.FLAG_UPDATE_CURRENT);



        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {

            //设置通知栏中的内容

            notice = builder.setContentIntent(contentIntent).setContentTitle("我是标题").setContentText("我是内容").build();

            notice.flags=Notification.FLAG_AUTO_CANCEL;

            barmanager.notify(10,notice);

        }

【2】取消通知ID是上面设置的

 

   barmanager.cancel(10);

【3】XML要配置打开的Activity。singleTask模式

<activity android:name=".SplashActivity"

android:launchMode="singleTask"

android:taskAffinity=""

android:excludeFromRecents="true"

/>

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要实现点击 Android 通知转到对应页面的功能,可以通过以下步骤实现: 1. 在 AndroidManifest.xml 文件中注册相应的 Activity,确保声明了相应的 Intent Filter,以便能够通过通知启动 Activity。 2. 在创建通知时,为其设置 PendingIntent,这个 PendingIntent 包含了转到相应 Activity 的 Intent。 3. 在点击通知时,通过 PendingIntent 启动 Activity。 以下是一个示例代码: ```Java // 创建一个 Intent Intent intent = new Intent(this, MyActivity.class); // 设置 flags,确保每次点击通知时都能启动新的 Activity intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); // 创建 PendingIntent PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); // 创建通知 NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.drawable.notification_icon) .setContentTitle("My notification") .setContentText("Hello World!") .setPriority(NotificationCompat.PRIORITY_DEFAULT) .setContentIntent(pendingIntent) // 设置 PendingIntent .setAutoCancel(true); // 显示通知 NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); notificationManager.notify(notificationId, builder.build()); ``` 在上面的代码中,点击通知时会启动名为 MyActivity 的 Activity。在创建 PendingIntent 时,需要指定 PendingIntent.FLAG_UPDATE_CURRENT 标志,以便能够更新 PendingIntent 中的 Intent。如果不指定此标志,则会创建一个旧的 PendingIntent,其中的 Intent 可能已过时,导致无法正确启动 Activity。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

兴帅_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值