Android Notification is used to display a notification in the status bar

Scenes

The status bar displays notification effect

 

 

Note:

Blog:
https://blog.csdn.net/badao_liumang_qizhi
public concern number of
programs overbearing ape
acquisition-related programming e-books, tutorials and push for free download.

achieve

New NotificationActivity, get notification manager by getSystemService method.

Then create a notification notification and set some properties, and then send notifications using the Notification Manager.

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>

Guess you like

Origin www.cnblogs.com/badaoliumangqizhi/p/12174009.html