Using Notification in Android to display notifications on the status bar

Keywords: Android Programming xml encoding

scene

Show notification effect on status bar

 

 

Note:

Blog:
https://blog.csdn.net/badao_liumang_qizhi
Pay attention to the public address
Domineering procedural ape
Get programming related ebooks, tutorials and free downloads.

Realization

Create a new NotificationActivity and get the notification manager through the getSystemService method.

Then create the notification and set some properties of the notification, and then use the notification manager to send the notification.

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;            //Notified ID
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification);
        //New notification manager
        final NotificationManager notificationManager =
                (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        // Create a Notification object
        Notification.Builder notification = new Notification.Builder(this);
        // Set to turn on the notification and it will disappear automatically
        notification.setAutoCancel(true);
        // Set icons for notifications
        notification.setSmallIcon(R.drawable.dog);
        // Set the title of the notification content
        notification.setContentTitle("Do not hurry to pay attention to the public address.");
        // Set notification content
        notification.setContentText("Click to see the details!");
        //Set to use the default sound and vibration of the system
        notification.setDefaults(Notification.DEFAULT_SOUND
                | Notification.DEFAULT_VIBRATE);
        //Set send time
        notification.setWhen(System.currentTimeMillis());
        // Create one to start others Activity Of Intent
        Intent intent = new Intent(NotificationActivity.this
                , DetailActivity.class);
        PendingIntent pi = PendingIntent.getActivity(
                NotificationActivity.this, 0, intent, 0);
        //Set notification bar and click jump
        notification.setContentIntent(pi);
        //Sending notice
        notificationManager.notify(NOTIFYID, notification.build());
    }
}

 

When you click details, go to DetailActivity, design details page and display text information

<?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="Domineering procedural ape"
        android:layout_height="wrap_content"/>

</LinearLayout>

Posted by tested_123 on Thu, 09 Jan 2020 10:47:41 -0800