notification常用于下拉式的消息推送。
Notification的构成
Nitification的实例
1.新建一个Builder,要选Notification.compat包。
2.然后用builder来设置nitification的属性。
代码:
public class MainActivity extends Activity { public static final int NOTIFICATION_ID = 200; int count = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.button1).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub count++; Builder builder = new NotificationCompat.Builder(MainActivity.this); builder.setSmallIcon(R.drawable.ic_launcher); builder.setContentTitle("哇哦! 你有count条心的消息"); builder.setContentText("notification创建成功"); Notification notification = builder.build(); NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); manager.notify(NOTIFICATION_ID, notification); } }); } }
NOTIFICATION_ID 这个常量值用于唯一标识notification。
每次更新可以直接更新这个notification。builder可以设置更多的属性。
效果图