我该如何在安卓状态栏中创建一个通知?

5

大家好,我想分享一下我的安卓通知构建器。具体内容如下:

请分享任何更改意见。

1个回答

2

最小使用:

NotificatorFacade nb = new NotificatorFacade(context);
nb.show(R.drawable.icon, "tickerText", new Date().getTime(), 
                 "contentTitle", "contentText", ERROR_NOTIFICATION_ID);

source:

package my.tools.android.notification;
import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.net.Uri;
/** * 通知构建器,用于创建和显示通知 */ public class NotificatorBuilder {
private final Context context; private Intent intent; private Integer flags;
private Integer defaults; private Uri sound;
public NotificatorBuilder(Context context) { this.context = context; }
/** * 设置 Notification.defaults 的标识 * * @param defaults 标识值 */ public void setDefaults(int defaults) { this.defaults = defaults; }
/** * 显示通知,使用给定的参数 * * @see http://developer.android.com/guide/topics/ui/notifiers/notifications.html * @param iconDrawable 图标 * @param tickerText 滚动文本 * @param when 时间 * @param contentTitle 标题 * @param contentText 内容 * @param NOTIFICATION_ID 通知 ID,用于后续识别 */ public void show(int iconDrawable, CharSequence tickerText, long when, CharSequence contentTitle, CharSequence contentText, int NOTIFICATION_ID) { // 获取 NotificationManager 引用: String ns = Context.NOTIFICATION_SERVICE; NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns); // 创建通知: Notification notification = new Notification(iconDrawable, tickerText, when); // 定义通知的扩展消息和意图:
if (sound != null) { notification.sound = sound; } if (flags != null) { notification.flags = flags; } if (defaults != null) { notification.defaults = defaults; } // 如果意图为 null,则创建一个并设置 FLAG_AUTO_CANCEL 标识 EXTENDS FLAGS!!! if (intent == null) { setIntent(new Intent(context, NotificatorBuilder.class)); notification.flags |= Notification.FLAG_AUTO_CANCEL; }
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, notification); }
/** * 设置通知标识,例如:NotificatorBuilder nb = new NotificatorBuilder(context); * nb.setFlags(Notification.DEFAULT_VIBRATE|Notification.FLAG_INSISTENT); * * @param flags 标识值 */ public void setFlags(int flags) { this.flags = flags; }
/** * 设置意图 * * PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0); * notification.setLatestEventInfo(context, contentTitle, contentText,contentIntent); * * @param intent 意图 */ public void setIntent(Intent intent) { this.intent = intent; }
/** * 设置通知声音 * * 使用默认通知调用方法:setDefaults(Notification.DEFAULT_SOUND); * * 示例: * 要使用不同的声音,请将 Uri 引用传递给 sound 字段。以下示例使用保存在设备 SD 卡上的已知音频文件: * notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3"); * * 在下一个示例中,从内部 MediaStore 的 ContentProvider 中选择音频文件: * notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6"); * * @param sound 声音 Uri */ public void setSound(Uri sound) { this.sound = sound; }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接