Notification为android中的通知,是在android桌面上方的状态栏中显示的提示信息,显示时不会影响用户的操作,同时Notification提供了各种不同的风格,可以显示文字、图像或按钮,只要用户不清楚Notificaion,Notification将永久的保留在手机的状态栏中。Notification中常用的几个对象如下:
NotificationManager 用于管理Notification,是用于处理系统通知的服务
Notification属于通知,可以设置其图标、标题以及内容,也可以自定义Notification 的UI。
PendingIntent名为预处理意图,用于处理点击通知后的跳转对象的封装。下面我们将用实际的代码来讲解一下:
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
用于获取系统通知服务,实例代码如下:
import android.os.Bundle;
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.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity{
privatestatic final int NOTIFICAITON_ID = 0;
privateButton btnSend;
privateNotificationManager notificationManager;
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.btnSend= (Button) findViewById(R.id.btnSend);
this.btnSend.setOnClickListener(clickListener);
this.notificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
OnClickListenerclickListener = new OnClickListener() {
@Override
publicvoid onClick(View arg0) {
Intentintent = new Intent(MainActivity.this, PendingIntentActivity.class);
PendingIntentpendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
Notificationnotification = new Notification(R.drawable.ic_launcher, "通知。。。。", System.currentTimeMillis());
notification.setLatestEventInfo(MainActivity.this,"标题", "详细内容。。。。", pendingIntent);
notificationManager.notify(NOTIFICAITON_ID,notification);
}
};
}
以上纯属个人笔记,以方便以后查看,如果各位大牛们有何指点,还请赐教!多交流!