Android ------- Notification method of transmitting notifications
Notifications is a unique feature of the Android system, when a user want to give prompt information app, but the app not running in the foreground, you can use the notification.
After sending a notification, the status bar at the top of the phone will display a small icon, pull down the status bar, notification of specific information will be displayed.
1. The layout of the front end
2. The back-end design
Steps: 1. get NotificationManger
2. Create an object PendingIntent
3. Create notification Builder objects
4. Set the parameters
4. NotificationManager send notifications
package com.example.day20190530;
// guide package
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public void NotificationActivity entends Activity implements onClickListener{
private Button sendBtn;
private Button cancelBtn;
NotificationManager manager;
@Orrvide
protected void onCreat(Bundle savedInstanceState){
super.onCreat(savedInstanceState);
setContentView(R.layout.notification_activity);
sendBtn=(Button)findViewById(R.id.button1);
cancelBtn=(Button)findViewById(R.id.button2);
sendBtn.setonClickListener(this);
cancelBtn.setonClickListener(this);
}
@Ovrride
public void onClick(View v){
switc(v.getId){
case R.id.button1:
// Get the system notifies the managed objects
manager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Create Intent
Intent it=new Intent(this,OtherActivity.Class);
// Create an object PendingIntent
PendingIntent pending=PendingIntent.getActivity(this,0,it,PendingIntent.FALG_CANCLE_CURRETN);
// Create Notification Builder objects
NotificationBuilder builder=new Notification.Builder(NotificationActivity.this);
builder.setSmallIcon (R.drawble.ic_launcher); // Set small icon
builder.setContentTitle ( "Notice"); // notice title
builder.setContentText ( "Beautiful day"); // notification content
builder.setTicker ( "the information from XXX");
// set the sound
builder.setSounds(Uri.parse("android.resourse://"+getPackageName+"/"+R.raw.music));
builder.setAutoCancel (true); // Cancel sending notifications
builder.setWhen (System.CurrentTimeMIills ()); // set the notification time
builder.setsetContentIntent (pending); // click to jump to the interface
manager.notification (1, builder.getNotification ()); // show notifications
break;
case R.id.button2:
manager.cancel (1); // cancellation notice
break;
}
}
}