Notification是显示在手机状态的通知,一般显示手机当前的网络状态、电池状态、时间等。
设置Notification涉及到两个类,一个类是NotificationManager,一个类是Notification。可以这样理解这两个类,NotificationManager相当于顺丰快递小哥,notification代表的就是我们送的信件,我们要发送信件首先打电话给顺丰小哥,相当于初始化NotificationManager,然后填写好信件,相当于初始化Notifiaction,然后小哥发送信件,就是notificationmanager.notify(notifaction)
1)初始化NotificationManager: this.getSystemService(Context.NOTIFICATION_SERVICE);
2)初始化Notification:new Notification.Builder(this).builder();
main.xml:
01 |
< RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android" |
[!--empirenews.page--]
02 |
xmlns:tools = "http://schemas.android.com/tools" |
[!--empirenews.page--]
03 |
android:layout_width = "match_parent" |
[!--empirenews.page--]
04 |
android:layout_height = "match_parent" |
[!--empirenews.page--]
05 |
tools:context = ".Main" > |
[!--empirenews.page--]
[!--empirenews.page--]
[!--empirenews.page--]
09 |
android:layout_width = "wrap_content" |
[!--empirenews.page--]
10 |
android:layout_height = "wrap_content" |
[!--empirenews.page--]
11 |
android:text = "发送notification" /> |
[!--empirenews.page--]
[!--empirenews.page--]
14 |
android:id = "@+id/deletebtn" |
[!--empirenews.page--]
15 |
android:layout_width = "wrap_content" |
[!--empirenews.page--]
16 |
android:layout_height = "wrap_content" |
[!--empirenews.page--]
17 |
android:layout_marginLeft = "200sp" |
[!--empirenews.page--]
18 |
android:text = "取消notification" /> |
[!--empirenews.page--]
[!--empirenews.page--]
Main.java:
[!--empirenews.page--]
03 |
import android.annotation.SuppressLint; |
[!--empirenews.page--]
04 |
import android.app.Activity; |
[!--empirenews.page--]
05 |
import android.app.Notification; |
[!--empirenews.page--]
06 |
import android.app.NotificationManager; |
[!--empirenews.page--]
07 |
import android.app.PendingIntent; |
[!--empirenews.page--]
08 |
import android.content.Context; |
[!--empirenews.page--]
09 |
import android.content.Intent; |
[!--empirenews.page--]
10 |
import android.os.Bundle; |
[!--empirenews.page--]
11 |
import android.view.View; |
[!--empirenews.page--]
12 |
import android.view.View.OnClickListener; |
[!--empirenews.page--]
13 |
import android.widget.Button; |
[!--empirenews.page--]
14 |
import android.widget.Toast; |
[!--empirenews.page--]
16 |
public class Main extends Activity { |
[!--empirenews.page--]
18 |
NotificationManager manager = null ; |
[!--empirenews.page--]
[!--empirenews.page--]
22 |
int responseCode = 0x123 ; |
[!--empirenews.page--]
24 |
@SuppressLint ( "NewApi" ) |
[!--empirenews.page--]
[!--empirenews.page--]
26 |
protected void onCreate(Bundle savedInstanceState) { |
[!--empirenews.page--]
28 |
super .onCreate(savedInstanceState); |
[!--empirenews.page--]
30 |
setContentView(R.layout.main); |
[!--empirenews.page--]
32 |
button = (Button) this .findViewById(R.id.btn); |
[!--empirenews.page--]
34 |
btn1 = (Button) this .findViewById(R.id.deletebtn); |
[!--empirenews.page--]
36 |
manager = (NotificationManager) this |
[!--empirenews.page--]
37 |
.getSystemService(Context.NOTIFICATION_SERVICE); |
[!--empirenews.page--]
39 |
button.setOnClickListener( new OnClickListener() { |
[!--empirenews.page--]
[!--empirenews.page--]
42 |
public void onClick(View view) { |
[!--empirenews.page--]
[!--empirenews.page--]
[!--empirenews.page--]
[!--empirenews.page--]
50 |
btn1.setOnClickListener( new OnClickListener() { |
[!--empirenews.page--]
[!--empirenews.page--]
53 |
public void onClick(View view) { |
[!--empirenews.page--]
[!--empirenews.page--]
[!--empirenews.page--]
[!--empirenews.page--]
[!--empirenews.page--]
[!--empirenews.page--]
[!--empirenews.page--]
[!--empirenews.page--]
65 |
@SuppressLint ( "NewApi" ) |
[!--empirenews.page--]
[!--empirenews.page--]
68 |
Intent intent = new Intent(Main. this , OtherActivity. class ); |
[!--empirenews.page--]
70 |
PendingIntent pi = PendingIntent.getActivity( this , 0 , intent, 0 ); |
[!--empirenews.page--]
72 |
Notification notification = new Notification.Builder( this ) |
[!--empirenews.page--]
73 |
.setAutoCancel( true ).setTicker( "aaaa" ) |
[!--empirenews.page--]
74 |
.setSmallIcon(R.drawable.ic_launcher).setContentTitle( "bbbb" ) |
[!--empirenews.page--]
75 |
.setContentText( "ccccc" ) |
[!--empirenews.page--]
76 |
.setDefaults(Notification.DEFAULT_VIBRATE).build(); |
[!--empirenews.page--]
78 |
manager.notify(responseCode, notification); |
[!--empirenews.page--]
[!--empirenews.page--]
82 |
public void deleteMsg() { |
[!--empirenews.page--]
83 |
manager.cancel(responseCode); |
[!--empirenews.page--]
[!--empirenews.page--]
实现效果:
tips:在实例化Notification.Builder()实例的时候,必须setSmallIcon()或者setLargerIcon(),否则notifaction不显示。