Notification class and PendingIntent class

Notification

1. Overview
Notification is an important system service in Android. Notification displays notification information in the mobile phone status bar. The mobile phone status bar is located at the top of the screen, where it usually displays the current network status, battery status, time, text messages, etc. of the mobile phone. Notification represents a notification with a global effect, and the Notification object is sent through the NotificationManager (notification manager class) object.

Second, the Notification class

1) Overview The
Notification class is used to store the notification title, content, icon and the target object sent.

2) Common attributes

1.icon: the icon of the notification
Sample code:
Notification notif = new Notification();
notif.icon = R.drawable.ic_launcher;
Description: The first line creates the notification object, and the second line sets the icon of the notification object's icon. The icon uses the Android default icon or other images.

2.tickerText: notification title, which is displayed in the status bar of the window.
Sample code: notif.tickerText="Notification is coming";

3.flags: Set the flag value of the notification state, with the following optional constant values:
FLAG_AUTO_CANCEL: The notification is cleared after clicking this notification in the notification bar.
FLAG_INSISTENT: Repeat the sound until the user responds to this notification.
FLAG_ONGOING_EVNET: Put this notification in the "Ongoing" group of the notification bar
FLAG_NO_CLEAR: Disable manual clearing of this notification.

4.defaults: Set the default properties of notifications. The default properties include sound prompts, vibration prompts, flash prompts, etc.
in:
DEFAULT_SOUND: Default sound
DEFAULT_VIRBATE: Default vibration
DEFAULT_LIGHTS: Default flash
ALL: Use default sound, default vibration and default flash.

5. public int contentIntent: Stores an object of type PendingIntent
The PendingIntent class is introduced later.

6.public int contentView: Stores an object of type RemoteView. Use this object to display effects such as download progress in the status bar.

 

PendingIntent class

1. Overview
1) PendingIntent is used to describe Intent and its final behavior.
2) PendingIntent objects can be submitted to other applications, and then continue processing. This allows the Intent and its eventual behavior described in PendingIntent to be processed later.



2. Common methods

1)public static PendingIntentgetActivity(Context context,int

requestCode,Intent intent,int flags)
Function: Get a Activity from the system to start the target (set in the Intent)

PendingIntent object.
Parameters -- context: The object of the current component.
Parameters - requestCode: request code, use 0.
Parameters - intent: Intent object, used to specify the target component of the notification.
Parameters - flags: set the notification type, there are two commonly used optional values:
1. FLAG_CANCEL_CURRENT: Set when extracting PendingIntent, first close the previous PendingIntent instance, so the obtained PendingIntent is new.

2. FLAG_UPDATE_CURRENT: Set the Intent object data in the PendingIntent before the new Intent is updated, such as updating the Extras in the Intent.

Tip: In this application, you can set the flags value to 0.


2)public static PendingIntent getService(Context context,int

requestCode,Intent intent,int flags)
Role: Get a PendingIntent object from the system for starting the target Service

3)public static PendingIntent getBroadcast(Context context,int

requestCode,Intent intent,int flags)
Role: Get a PendingIntent object from the system that is used to start the BroadcastReceiver's Intent broadcast.


 

 

 

It is worth mentioning that:

The method of using Notification notif = new Notification(R.drawable.ic_launcher, "Notification is coming!", System.currentTimeMillis()); is outdated, so you cannot use this method, you need to use Notification.Builder instead

 

 

actual case:

 

package com.jxust.day07_01_notificationdemo;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;

@SuppressLint("NewApi")
public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate (savedInstanceState);
		setContentView(R.layout.activity_main);
		setListener();
	}

	private void setListener() {
		findViewById(R.id.btnSendNotification).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// Create an Intent object that starts the target Activity
				Intent intent = new Intent(MainActivity.this, SecondActivity.class);
				// Create PendingIntent object
				PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
				// Creating a Notification object is obsolete
				// Notification notif = new
				// Notification(R.drawable.ic_launcher,"Notification is coming!",System.currentTimeMillis());
				// Set the listener for click notification
				// notif.setLatestEventInfo(MainActivity.this,
				// "You have software to update", "Click to start update", pi); obsolete
				
				// create notification manager object / get system service
				NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);	

				// New usage recommended by the system
				Notification.Builder builder = new Notification.Builder(MainActivity.this);
				builder.setContentTitle("You have software to update");
				builder.setContentText("Click to start updating");
				builder.setSmallIcon(R.drawable.ic_launcher);
				builder.setContentIntent(pi); // This is the function of jumping to another Activity by clicking
				Notification notification = builder.build();
				// send notification
				manager.notify(88,notification);
			}
		});
	}

}

 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/btnSendNotification"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Send notification" />

</RelativeLayout>

 

package com.jxust.day07_01_notificationdemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class SecondActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate (savedInstanceState);
		setContentView(R.layout.activity_second);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is
		// present.
		getMenuInflater().inflate(R.menu.second, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}

 

 

 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.jxust.day07_01_notificationdemo.SecondActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Second Activity" />

</RelativeLayout>

 

 

The effect is as follows:

 

 

 

You can jump to SecondActivity by clicking on the created Notification



 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326687667&siteId=291194637