Android Notification 通知封装成工具类
2016-09-09 14:51
381 查看
public class NotificationUtils {
private static NotificationManager mNotificationManager;
private static Notification notification;
/**
* 创建通知, * 请在调用此方法时开启子线程
*
* @param context 上下文
* @param icon 通知图片
* @param tickerText 通知未拉开的内容
* @param title 通知标题
* @param content 通知主内容
* @param intent 意图
* @param id
* @param time 控制线程睡眠时间
*/
public static void createNotif(Context context, int icon, String tickerText, String title, String content, Intent intent, int id, long time) {
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setAction(Intent.ACTION_MAIN);
//intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
PendingIntent pendingIntent = PendingIntent.getActivity(context, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
mBuilder.setContentTitle(title)
.setContentText(content)
.setContentIntent(pendingIntent)
.setTicker(tickerText)
.setWhen(System.currentTimeMillis())
.setPriority(Notification.PRIORITY_DEFAULT)
.setOngoing(false)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_SOUND)
.setSmallIcon(icon);
notification = mBuilder.build();
notification.flags = Notification.FLAG_ONGOING_EVENT;
notification.flags = Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(id, notification);
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private static NotificationManager mNotificationManager;
private static Notification notification;
/**
* 创建通知, * 请在调用此方法时开启子线程
*
* @param context 上下文
* @param icon 通知图片
* @param tickerText 通知未拉开的内容
* @param title 通知标题
* @param content 通知主内容
* @param intent 意图
* @param id
* @param time 控制线程睡眠时间
*/
public static void createNotif(Context context, int icon, String tickerText, String title, String content, Intent intent, int id, long time) {
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setAction(Intent.ACTION_MAIN);
//intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
PendingIntent pendingIntent = PendingIntent.getActivity(context, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
mBuilder.setContentTitle(title)
.setContentText(content)
.setContentIntent(pendingIntent)
.setTicker(tickerText)
.setWhen(System.currentTimeMillis())
.setPriority(Notification.PRIORITY_DEFAULT)
.setOngoing(false)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_SOUND)
.setSmallIcon(icon);
notification = mBuilder.build();
notification.flags = Notification.FLAG_ONGOING_EVENT;
notification.flags = Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(id, notification);
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
相关文章推荐
- android Notification 通知封装
- 系出名门Android(3) - 对话框(Dialog)和通知(Notification)
- Android的状态栏通知(Notification)
- 系出名门Android(3) - 对话框(Dialog)和通知(Notification)
- Android 之 Notification通知栏
- 从J2EE转向Android的第十天-----对话框(Dialog)和通知(Notification)
- Android开发——通知Notification
- android之Notification通知
- 系出名门Android(3) - 对话框(Dialog)和通知(Notification)
- 【Android】封装一个简单好用的打印Log的工具类
- 定制android通知(Notification)
- Android NotificationManager与Notification(通知栏) 的使用
- android开发(11) 消息栏通知(Notification)
- 状态栏Notification | 点击通知后跳转(未写) | 自定义Notification布局(未写) | 封装
- 系出名门Android(3) - 对话框(Dialog)和通知(Notification)
- 定制android通知(Notification)
- 26. android Notification 状态栏通知
- Android中的通知—Notification
- 【Android】SQLite的工具类 ---- 通过反射把Cursor封装到VO对象
- Android应用开发基础篇(2)-----Notification(状态栏通知)