zoukankan      html  css  js  c++  java
  • Android中使用Notification在状态栏上显示通知

    场景

    状态栏上显示通知效果

    注:

    博客:
    https://blog.csdn.net/badao_liumang_qizhi
    关注公众号
    霸道的程序猿
    获取编程相关电子书、教程推送与免费下载。

    实现

    新建NotificationActivity,通过getSystemService方法获取通知管理器。

    然后创建通知并设置通知的一些属性,再使用通知管理器发送通知。

    package com.badao.relativelayouttest;
    
    import androidx.annotation.RequiresApi;
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.app.Notification;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.content.Intent;
    import android.os.Build;
    import android.os.Bundle;
    
    public class NotificationActivity extends AppCompatActivity {
        final int NOTIFYID = 0x123;            //通知的ID
        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_notification);
            //新建通知管理器
            final NotificationManager notificationManager =
                    (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            // 创建一个Notification对象
            Notification.Builder notification = new Notification.Builder(this);
            // 设置打开该通知,该通知自动消失
            notification.setAutoCancel(true);
            // 设置通知的图标
            notification.setSmallIcon(R.drawable.dog);
            // 设置通知内容的标题
            notification.setContentTitle("还不赶紧关注公众号");
            // 设置通知内容
            notification.setContentText("点击查看详情!");
            //设置使用系统默认的声音、默认震动
            notification.setDefaults(Notification.DEFAULT_SOUND
                    | Notification.DEFAULT_VIBRATE);
            //设置发送时间
            notification.setWhen(System.currentTimeMillis());
            // 创建一个启动其他Activity的Intent
            Intent intent = new Intent(NotificationActivity.this
                    , DetailActivity.class);
            PendingIntent pi = PendingIntent.getActivity(
                    NotificationActivity.this, 0, intent, 0);
            //设置通知栏点击跳转
            notification.setContentIntent(pi);
            //发送通知
            notificationManager.notify(NOTIFYID, notification.build());
        }
    }

    点击详情时跳转到DetailActivity,设计详情页,显示文本信息

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".DetailActivity">
    
        <TextView
            android:layout_width="wrap_content"
            android:text="霸道的程序猿"
            android:layout_height="wrap_content"/>
    
    </LinearLayout>
  • 相关阅读:
    Cheatsheet: 2011 03.19 ~ 03.31
    Cheatsheet: 2011 03.07 ~ 03.14
    Cheatsheet: 2011 04.01 ~ 04.10
    Cheatsheet: 2011 04.20 ~ 04.29
    Java动手动脑02
    Java日报10.5
    java四则运算第三阶段
    验证码界面——Java图形界面
    Java四则运算V3.0
    Java日报10.8
  • 原文地址:https://www.cnblogs.com/badaoliumangqizhi/p/12174009.html
Copyright © 2011-2022 走看看