Notification
Notification,即通知。这个估计是我们作为用户最不喜欢,作为开发者有不得不做的东西了。有时候我们手机一联网,通知栏里就会收到N多的推送消息,微博的新鲜事,淘宝什么什么又搞活动了……这些在我们通知栏里的都是Notification通知。那么他们是如何实现的呢?
NotificationManager创建Notification
通过NotificationManager来创建Notification。我们来看一下下面这个例子:
//定义了一个全局变量哦,方便使用
private NotificationManager mNotificationManager;
private void showNotification() {
mNotification = new Notification();
//设置通知栏的图片
mNotification.icon = R.mipmap.ic_launcher;
//设置状态栏上显示的消息
mNotification.tickerText = "我是状态栏上的通知";
//通过系统获得时间,通知的时间显示
mNotification.when = System.currentTimeMillis() ;
//设置通知可自动取消
mNotification.flags=Notification.FLAG_AUTO_CANCEL;
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),1, intent, PendingIntent.FLAG_ONE_SHOT);
//设置通知的标题,内容以及点击通知启动的事件。
mNotification.setLatestEventInfo(getApplicationContext(),"通知的标题", "我是通知的内容,显示在通知下拉栏中",pendingIntent);
//设置启动id和启动的通知
mNotificationManager.notify(1, mNotification);
}
Notification.Builder创建Notification
通过Notification.Builder来创建Notification。我们来看一下下面这个例子:
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void showNotification2() {
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 2, intent, PendingIntent.FLAG_ONE_SHOT);
Notification notification = new Notification.Builder(MainActivity.this)
.setSmallIcon(R.mipmap.ic_launcher)
.setTicker("我是显示在状态栏上的信息")
.setContentTitle("我通知的标题")
.setContentText("我是通知的内容")
.setContentInfo("我是显示在时间下的内容")
.setContentIntent(pendingIntent)
.setAutoCancel(true).setWhen(System.currentTimeMillis()).build();
mNotificationManager.notify(2, notification);
}
自定义Notification
自定义Notification虽然可以给我们更多的空间去发挥我们的想象力,去创造我们自己想要的Notification形式,但是在实际的开发中,Notification的形式简单单一,很少会使用自定义的Notification,即使用的少我们也应该掌握一下,下面看例子:
自定义Notification:通过定义一个RemoteView后将其加载。
RemoteView :TextView ,ImageView,ProgressBar属于RemoteView,Linearlayout也属于RemoteView等。
步骤:
- 创建一个RemoteView的布局文件(xml), 该文件中只能包含TextView ,ImageView,ProgressBar控件和Linearlayout布局。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">
<ImageButton
android:id="@+id/imageview_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/textview_content"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="这是通知的内容" />
<ImageView
android:id="@+id/imageview_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />
</LinearLayout>
创建 一个RemoteView的对象,将自定义的RemoteView加载在RemoteView的对象中。
RemoteViews remoteView = new RemoteViews(getPackageName(), R.layout.notification_remoteview);创建Notification的对象,设置其图片,内容等信息(虽然设置的信息是没有用的,但是如果不设置的话会进行报错,这是Android的怪癖!)
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 2, intent, PendingIntent.FLAG_ONE_SHOT);
Notification notification = new Notification.Builder(MainActivity.this)
.setSmallIcon(R.mipmap.ic_launcher)
.setTicker("我是显示在状态栏上的信息")
.setContentTitle("我通知的标题")
.setContentText("我是通知的内容")
.setContentInfo("我是显示在时间下的内容")
.setContent(remoteView)
.setContentIntent(pendingIntent)
.setAutoCancel(true).setWhen(System.currentTimeMillis()).build();
mNotificationManager.notify(2, notification);