protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
send_notice = (Button) findViewById(R.id.send_notice);
send_notice.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//创建No
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(MainActivity.this);
builder.setTicker("新消息");//不好使
builder.setContentTitle("title");
builder.setAutoCancel(true);//自动取消通知栏
builder.setContentText("content text");
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setWhen(System.currentTimeMillis());
builder.setColor(Color.BLUE);//图标背景颜色
// builder.setSound(Uri.fromFile(new File("/外置存储卡/song/Butter Fly-和田光司.mp3")));//错误
// builder.setVibrate(new long[]{0,1000,1000,1000});
// builder.setLights(Color.GRAY,1000,1000);
builder.setDefaults(Notification.DEFAULT_ALL);//添加所有默认效果,灯光,震动,音效
Intent intent = new Intent(MainActivity.this, Notice_Activity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0,
intent, PendingIntent.FLAG_CANCEL_CURRENT);
builder.setContentIntent(pendingIntent);
Notification notification = builder.build();
manager.notify(1, notification);
}
});
}
setLatestEventInfo在API高的版本里不好用,用Buider替代哦