Notification是一种具备全局效果的通知,它展现在屏幕的顶端,首先会表现为一个图标的形式,当用户向下滑动的时候,展现出通知具体的内容。咱们在用手机的时候,若是来了短信,而咱们没有点击查看的话,在手机的最上边的状态栏里有一个短信的小图标提示,这个提示效果就是用Notification来作。android
状态通知栏主要涉及到2个类:Notification 和NotificationManager网络
Notification:通知信息类,它里面对应了通知栏的各个属性app
NotificationManager:是状态栏通知的管理类,负责发通知、清除通知等操做。ide
使用的基本流程:布局
另外咱们还能够调用NotificationManager的cancel()方法取消通知ui
setContentTitle(CharSequence):设置标题 this
首先定义两个简单的按钮,这里就不写出来了,接着在MainActivity类实现功能 ,代码以下所示:spa
package com.nyl.notification; 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.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends Activity implements View.OnClickListener { private Context context; private NotificationManager notificationManager; private Notification notification; Bitmap bitmap = null; //位图 private static final int NOTIFICATION_1 = 1; private Button btnShow; private Button btnClose; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); context = this; //建立图片的Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.activity_main); notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); initView(); //初始化控件 } /** * 初始化布局控件 */ private void initView() { btnShow = (Button) findViewById(R.id.btnShow); btnClose = (Button) findViewById(R.id.btnClose); btnShow.setOnClickListener(this); btnClose.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.btnShow: // //定义一个PendingIntent点击Notification后启动一个Activity Intent intent = new Intent(context,OtherActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent,0); //设置图片,通知标题,发送时间,提示方式等属性 Notification.Builder builder = new Notification.Builder(this); //标题 builder.setContentTitle("状态栏通知") .setContentText("状态栏会显示一个通知栏的图标") //内容 .setSubText("丰富你的程序,运用手机多媒体") //内容下面的一小段文字 .setTicker("收到Notification信息") //收到信息后状态显示的文字信息 .setWhen(System.currentTimeMillis()) //设置通知时间 .setSmallIcon(R.mipmap.ic_launcher) //设置小图片 .setLargeIcon(bitmap) //设置大图片 .setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE) //设置默认的三色灯与振动器 .setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.kalimba)) //设置自定义的提示音 .setAutoCancel(true) //设置点击后取消Notification .setContentIntent(pendingIntent); //设置pendingIntent notification = builder.build(); notificationManager.notify(NOTIFICATION_1,notification); break; case R.id.btnClose: //除了能够根据ID来取消Notification外,还能够调用cancelAll();关闭该应用产生的全部通知 notificationManager.cancel(NOTIFICATION_1); //取消Notification break; } } }
点击【显示普通的状态栏通知】按钮,运行效果以下:.net
在手机的最上边的状态栏里出现了【收到Notification信息】的提示消息,向下滑动,能够看到通知的图标,标题,子标题这些内容,以下图所示:code
点击通知,就能够看到通知的详细内容,以下图所示:
这里是显示一张照片
点击【关闭状态栏通知】按钮是关闭手机状态栏上显示通知消息,关闭以后,就看不到了。这里不贴图了。有兴趣的园友本身复制代码去动手体验吧。
关于Notification的基本用法就先介绍这么多。