本例是对Notification的练习,通过点击按钮弹出Notification系统通知。
主要知识点:
- 获取系统通知管理器
private NotificationManager notificationManager;
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
- 实例化一个Notification
Notification notification = new Notification(R.drawable.ic_launcher,"tickerText", System.currentTimeMillis());
函数解释:
android.app. Notification.Notification(int icon, CharSequence tickerText,long when)Deprecated. Use
Builder
instead.Constructs a Notification object with the information needed to have a status bar icon without the standard expanded view.
Parameters:
icon The resource id of the icon to put in the status bar.通知图标
tickerText The text that flows by in the status bar when the notification first activates.(看截图就明白了)
when The time to show in the time field. In the System.currentTimeMillis timebase.系统时间
- 设置Notification的信息
notification.setLatestEventInfo(AtyNotification.this, "contentTitle",
"contentText", PendingIntent.getActivity(AtyNotification.this,
1, getIntent(), 0));
函数解释:
void android.app. Notification.setLatestEventInfo( Context context, CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent)Deprecated. Use
Builder
instead.Sets the
contentView
field to be a view with the standard "Latest Event" layout.Uses the
icon
andwhen
fields to set the icon and time fields in the view.Parameters:
context The context for your application / activity.
contentTitle The title that goes in the expanded entry.
contentText The text that goes in the expanded entry.
contentIntent The intent to launch when the user clicks the expanded notification. If this is an activity, it must include the
android.content.Intent.FLAG_ACTIVITY_NEW_TASK
flag, which requires that you take care of task management as described in theTasks and Back Stack document.点击通知后触发的Intent,进入的activity。
布局文件:notification_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btn_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="showNotification"
android:text="弹出通知" />
</LinearLayout>
AtyNotification.java文件:
package com.fxj.composit;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import com.fxj.compractice.R;
public class AtyNotification extends Activity {
private NotificationManager notificationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notification_layout);
// 获取系统通知管理器
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// 当再次点击系统通知进入此activity时取消通知。
if (notificationManager != null) {
notificationManager.cancel(R.layout.notification_layout);
}
}
@SuppressWarnings("deprecation")
public void showNotification(View view) {
// 实例化一个Notification
Notification notification = new Notification(R.drawable.ic_launcher,
"tickerText", System.currentTimeMillis());
// 设置Notification的信息
notification.setLatestEventInfo(AtyNotification.this, "contentTitle",
"contentText", PendingIntent.getActivity(AtyNotification.this,
1, getIntent(), 0));
//执行Notification
notificationManager.notify(R.layout.notification_layout, notification);
}
}
运行效果:
结束。