Notification通知
manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this);
builder.setContentTitle("标题");
builder.setContentText("内容");
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setDefaults(Notification.DEFAULT_ALL);
builder.setAutoCancel(true);
Intent intent = new Intent();
intent.setClass(MainActivity.this, SecondActivity.class);
PendingIntent pIntent = PendingIntent.getActivity
(MainActivity.this, 1, intent, PendingIntent.FLAG_ONE_SHOT);
builder.setContentIntent(pIntent);
notification = builder.build();
manager.notify(1, notification);
}
});
final NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this);
builder.setContentTitle("标题");
builder.setContentText("内容");
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setDefaults(Notification.DEFAULT_ALL);
progress = 0;
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
progress+=10;
if(progress>100){
progress = 100;
timer.cancel();
}
builder.setProgress(100, progress, false);
notification = builder.build();
manager.notify(2, notification);
}
}, 1000 , 1000);
自定义:
NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this);
builder.setContentTitle("标题");
builder.setAutoCancel(true);
builder.setSmallIcon(R.drawable.ic_launcher);
// 创建RemoteView并显示在通知中
RemoteViews views = new RemoteViews(getPackageName(), // 当前所在应用的包
R.layout.notification);
views.setTextViewText(R.id.textview, "普通文本");
views.setImageViewResource(R.id.image, R.drawable.ic_launcher);
builder.setContent(views);// 设置自定义的通知
notification = builder.build();
manager.notify(3, notification);