android 自定义通知
2015-10-10 11:18
309 查看
package cn.qing.learndemo;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.TaskStackBuilder;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.NotificationCompat;
import android.view.View;
import android.widget.RemoteViews;
public class NotificationActivity extends AppCompatActivity {
private NotificationManager notificationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification);
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
System.out.println("classLoader:"+Thread.currentThread().getContextClassLoader());
}
private void simpleNotification() {
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.mipmap.icon);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 100, new Intent(this, MainActivity.class),
PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle("通知title")
.setContentText("最新通知栏消息,来自通知")
.setWhen(System.currentTimeMillis())
.setOngoing(true)
.setLargeIcon(icon)
.setDefaults(Notification.DEFAULT_ALL)
.setSmallIcon(R.mipmap.ic_question)
.setContentIntent(pendingIntent);
Notification notification = builder.build();
notification.flags = Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(1, notification);
}
public void showSimpleNotification(View view) {
simpleNotification();
}
public void showMutilNotification(View view) {
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.mipmap.icon);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 100, new Intent(this, NotificationActivity.class),
PendingIntent.FLAG_ONE_SHOT);
Intent resultIntent = new Intent(this, NotificationActivity.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent pi = stackBuilder.getPendingIntent(100, PendingIntent.FLAG_UPDATE_CURRENT);
String msg = "最新通知栏消息,来自通知,这里还有很多很多内容需要显示,这里还有很多很多内容需要显示,这里还有很多很多内容需要显示,这里还有很多很多内容需要显示,这里还有很多很多内容需要显示";
NotificationCompat.BigTextStyle bigTextStyle = new android.support.v4.app.NotificationCompat.BigTextStyle();
bigTextStyle.bigText(msg);
bigTextStyle.setSummaryText("点击查看更多详情");
bigTextStyle.setBigContentTitle("通知");
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle("通知")
.setWhen(System.currentTimeMillis())
.setOngoing(true)
.setLargeIcon(icon)
.setContentText(msg)
.setStyle(bigTextStyle)
.setDefaults(Notification.DEFAULT_ALL)
.setSmallIcon(R.mipmap.ic_question)
.setContentIntent(pi);
Notification notification = builder.build();
notification.flags = Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(2, notification);
}
public void showPriceNotification(View view) {
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.mipmap.icon);
Bitmap priceBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.my_top_bg);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 100, new Intent(this, NotificationActivity.class),
PendingIntent.FLAG_ONE_SHOT);
String msg = "最新通知栏消息,来自通知,这里还有很多很多内容需要显示,这里还有很多很多内容需要显示,这里还有很多很多内容需要显示,这里还有很多很多内容需要显示,这里还有很多很多内容需要显示";
NotificationCompat.BigPictureStyle bigPictureStyle = new android.support.v4.app.NotificationCompat.BigPictureStyle();
bigPictureStyle.setBigContentTitle("通知");
bigPictureStyle.setSummaryText("点击查看更多详情");
bigPictureStyle.bigLargeIcon(icon);
bigPictureStyle.bigPicture(priceBitmap);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle("通知")
.setWhen(System.currentTimeMillis())
.setOngoing(true)
.setLargeIcon(icon)
.setContentText(msg)
.setStyle(bigPictureStyle)
.setDefaults(Notification.DEFAULT_ALL)
.setSmallIcon(R.mipmap.ic_question)
.setContentIntent(pendingIntent);
Notification notification = builder.build();
notification.flags = Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(3, notification);
}
public void showSelfNotification(View view) {
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.mipmap.icon);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 100, new Intent(this, MainActivity.class),
PendingIntent.FLAG_ONE_SHOT);
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification);
String msg = "最新通知栏消息,来自通知,这里还有很多很多内容需要显示,这里还有很多很多内容需要显示,这里还有很多很多内容需要显示,这里还有很多很多内容需要显示,这里还有很多很多内容需要显示";
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setWhen(System.currentTimeMillis())
.setContentTitle("通知")
.setContentText(msg)
.setLargeIcon(icon)
.setContentIntent(pendingIntent)
.setDefaults(Notification.DEFAULT_ALL)
.setSmallIcon(R.mipmap.ic_question);
Notification notification = builder.build();
notification.flags = Notification.FLAG_AUTO_CANCEL;
if (Build.VERSION.SDK_INT >= 16) {
notification.bigContentView = remoteViews;
}
notificationManager.notify(5, notification);
}
public static final int PLAYER_NOTIFY_ID = 10;
/**
* 一个播放器通知
*
* @param view
*/
public void showPlayerNotification(View view) {
RemoteViews normalView = new RemoteViews(getPackageName(), R.layout.player_notification_normal);
RemoteViews bigView = new RemoteViews(getPackageName(), R.layout.player_notification_big);
Intent closeIntent = new Intent(PlayerBroadCaseReceiver.ACTION_PLAYER_NOTIFICATION);
closeIntent.putExtra(PlayerBroadCaseReceiver.KEY_OPTIONS, "close");
PendingIntent closePendingIntent = PendingIntent.getBroadcast(this, 1, closeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent playIntent = new Intent(PlayerBroadCaseReceiver.ACTION_PLAYER_NOTIFICATION);
playIntent.putExtra(PlayerBroadCaseReceiver.KEY_OPTIONS, "play");
PendingIntent playPendingIntent = PendingIntent.getBroadcast(this, 2, playIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent preIntent = new Intent(PlayerBroadCaseReceiver.ACTION_PLAYER_NOTIFICATION);
preIntent.putExtra(PlayerBroadCaseReceiver.KEY_OPTIONS, "pre");
PendingIntent prePendingIntent = PendingIntent.getBroadcast(this, 3, preIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent nextIntent = new Intent(PlayerBroadCaseReceiver.ACTION_PLAYER_NOTIFICATION);
nextIntent.putExtra(PlayerBroadCaseReceiver.KEY_OPTIONS, "next");
PendingIntent nextPendingIntent = PendingIntent.getBroadcast(this, 4, nextIntent, PendingIntent.FLAG_UPDATE_CURRENT);
normalView.setOnClickPendingIntent(R.id.ivClose, closePendingIntent);
normalView.setOnClickPendingIntent(R.id.ivPlay, playPendingIntent);
normalView.setOnClickPendingIntent(R.id.ivNext, nextPendingIntent);
bigView.setOnClickPendingIntent(R.id.ivClose, closePendingIntent);
bigView.setOnClickPendingIntent(R.id.ivPlay, playPendingIntent);
bigView.setOnClickPendingIntent(R.id.ivNext, nextPendingIntent);
bigView.setOnClickPendingIntent(R.id.ivPre, prePendingIntent);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setWhen(System.currentTimeMillis())
.setDefaults(Notification.DEFAULT_ALL)
.setSmallIcon(R.mipmap.icon);
Notification notification = builder.build();
notification.flags = Notification.FLAG_NO_CLEAR;
notification.contentView = normalView;
if (Build.VERSION.SDK_INT >= 16) {
notification.bigContentView = bigView;
}
notificationManager.notify(PLAYER_NOTIFY_ID, notification);
}
}
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.TaskStackBuilder;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.NotificationCompat;
import android.view.View;
import android.widget.RemoteViews;
public class NotificationActivity extends AppCompatActivity {
private NotificationManager notificationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification);
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
System.out.println("classLoader:"+Thread.currentThread().getContextClassLoader());
}
private void simpleNotification() {
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.mipmap.icon);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 100, new Intent(this, MainActivity.class),
PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle("通知title")
.setContentText("最新通知栏消息,来自通知")
.setWhen(System.currentTimeMillis())
.setOngoing(true)
.setLargeIcon(icon)
.setDefaults(Notification.DEFAULT_ALL)
.setSmallIcon(R.mipmap.ic_question)
.setContentIntent(pendingIntent);
Notification notification = builder.build();
notification.flags = Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(1, notification);
}
public void showSimpleNotification(View view) {
simpleNotification();
}
public void showMutilNotification(View view) {
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.mipmap.icon);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 100, new Intent(this, NotificationActivity.class),
PendingIntent.FLAG_ONE_SHOT);
Intent resultIntent = new Intent(this, NotificationActivity.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent pi = stackBuilder.getPendingIntent(100, PendingIntent.FLAG_UPDATE_CURRENT);
String msg = "最新通知栏消息,来自通知,这里还有很多很多内容需要显示,这里还有很多很多内容需要显示,这里还有很多很多内容需要显示,这里还有很多很多内容需要显示,这里还有很多很多内容需要显示";
NotificationCompat.BigTextStyle bigTextStyle = new android.support.v4.app.NotificationCompat.BigTextStyle();
bigTextStyle.bigText(msg);
bigTextStyle.setSummaryText("点击查看更多详情");
bigTextStyle.setBigContentTitle("通知");
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle("通知")
.setWhen(System.currentTimeMillis())
.setOngoing(true)
.setLargeIcon(icon)
.setContentText(msg)
.setStyle(bigTextStyle)
.setDefaults(Notification.DEFAULT_ALL)
.setSmallIcon(R.mipmap.ic_question)
.setContentIntent(pi);
Notification notification = builder.build();
notification.flags = Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(2, notification);
}
public void showPriceNotification(View view) {
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.mipmap.icon);
Bitmap priceBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.my_top_bg);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 100, new Intent(this, NotificationActivity.class),
PendingIntent.FLAG_ONE_SHOT);
String msg = "最新通知栏消息,来自通知,这里还有很多很多内容需要显示,这里还有很多很多内容需要显示,这里还有很多很多内容需要显示,这里还有很多很多内容需要显示,这里还有很多很多内容需要显示";
NotificationCompat.BigPictureStyle bigPictureStyle = new android.support.v4.app.NotificationCompat.BigPictureStyle();
bigPictureStyle.setBigContentTitle("通知");
bigPictureStyle.setSummaryText("点击查看更多详情");
bigPictureStyle.bigLargeIcon(icon);
bigPictureStyle.bigPicture(priceBitmap);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle("通知")
.setWhen(System.currentTimeMillis())
.setOngoing(true)
.setLargeIcon(icon)
.setContentText(msg)
.setStyle(bigPictureStyle)
.setDefaults(Notification.DEFAULT_ALL)
.setSmallIcon(R.mipmap.ic_question)
.setContentIntent(pendingIntent);
Notification notification = builder.build();
notification.flags = Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(3, notification);
}
public void showSelfNotification(View view) {
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.mipmap.icon);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 100, new Intent(this, MainActivity.class),
PendingIntent.FLAG_ONE_SHOT);
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification);
String msg = "最新通知栏消息,来自通知,这里还有很多很多内容需要显示,这里还有很多很多内容需要显示,这里还有很多很多内容需要显示,这里还有很多很多内容需要显示,这里还有很多很多内容需要显示";
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setWhen(System.currentTimeMillis())
.setContentTitle("通知")
.setContentText(msg)
.setLargeIcon(icon)
.setContentIntent(pendingIntent)
.setDefaults(Notification.DEFAULT_ALL)
.setSmallIcon(R.mipmap.ic_question);
Notification notification = builder.build();
notification.flags = Notification.FLAG_AUTO_CANCEL;
if (Build.VERSION.SDK_INT >= 16) {
notification.bigContentView = remoteViews;
}
notificationManager.notify(5, notification);
}
public static final int PLAYER_NOTIFY_ID = 10;
/**
* 一个播放器通知
*
* @param view
*/
public void showPlayerNotification(View view) {
RemoteViews normalView = new RemoteViews(getPackageName(), R.layout.player_notification_normal);
RemoteViews bigView = new RemoteViews(getPackageName(), R.layout.player_notification_big);
Intent closeIntent = new Intent(PlayerBroadCaseReceiver.ACTION_PLAYER_NOTIFICATION);
closeIntent.putExtra(PlayerBroadCaseReceiver.KEY_OPTIONS, "close");
PendingIntent closePendingIntent = PendingIntent.getBroadcast(this, 1, closeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent playIntent = new Intent(PlayerBroadCaseReceiver.ACTION_PLAYER_NOTIFICATION);
playIntent.putExtra(PlayerBroadCaseReceiver.KEY_OPTIONS, "play");
PendingIntent playPendingIntent = PendingIntent.getBroadcast(this, 2, playIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent preIntent = new Intent(PlayerBroadCaseReceiver.ACTION_PLAYER_NOTIFICATION);
preIntent.putExtra(PlayerBroadCaseReceiver.KEY_OPTIONS, "pre");
PendingIntent prePendingIntent = PendingIntent.getBroadcast(this, 3, preIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent nextIntent = new Intent(PlayerBroadCaseReceiver.ACTION_PLAYER_NOTIFICATION);
nextIntent.putExtra(PlayerBroadCaseReceiver.KEY_OPTIONS, "next");
PendingIntent nextPendingIntent = PendingIntent.getBroadcast(this, 4, nextIntent, PendingIntent.FLAG_UPDATE_CURRENT);
normalView.setOnClickPendingIntent(R.id.ivClose, closePendingIntent);
normalView.setOnClickPendingIntent(R.id.ivPlay, playPendingIntent);
normalView.setOnClickPendingIntent(R.id.ivNext, nextPendingIntent);
bigView.setOnClickPendingIntent(R.id.ivClose, closePendingIntent);
bigView.setOnClickPendingIntent(R.id.ivPlay, playPendingIntent);
bigView.setOnClickPendingIntent(R.id.ivNext, nextPendingIntent);
bigView.setOnClickPendingIntent(R.id.ivPre, prePendingIntent);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setWhen(System.currentTimeMillis())
.setDefaults(Notification.DEFAULT_ALL)
.setSmallIcon(R.mipmap.icon);
Notification notification = builder.build();
notification.flags = Notification.FLAG_NO_CLEAR;
notification.contentView = normalView;
if (Build.VERSION.SDK_INT >= 16) {
notification.bigContentView = bigView;
}
notificationManager.notify(PLAYER_NOTIFY_ID, notification);
}
}
相关文章推荐
- Android实现表情 抓取新浪表情
- 详解Android解析Xml的三种方式——DOM、SAX以及XMLpull
- android开发环境遇到adt无法启动的问题分析及解决方法
- Android开发 旋转屏幕导致Activity重建解决方法
- Android开发技巧之在a标签或TextView控件中单击链接弹出Activity(自定义动作)
- Android开发技巧之ViewStub控件惰性装载
- 分享10个很棒的学习Android开发的网站
- android开发之横向滚动/竖向滚动的ListView(固定列头)
- Android开发技巧之我的菜单我做主(自定义菜单)
- Windows下使用Apache Cordova开发ANDROID之HelloWorld
- 根据根据图片的url怎么取得图片ImageView对象
- [转] ADB server didn't ACK
- android开发的相关链接
- Android数据存储之SharedPreferences 共享首选项/偏好设置
- 基于NDK的Android防破解
- 麦子学院android开发笔记:Android更换RatingBar图片
- android开发入门知识之AIDL详解
- android开发学习如何一体化?从入门到高端开发的路如何走?
- Android入门到精通详解 电子书下载
- --暴走漫画系列--高仿淘宝收货地址(附demo)