Skip to content

Instantly share code, notes, and snippets.

@john990
Created March 13, 2014 02:09
Show Gist options
  • Save john990/9520724 to your computer and use it in GitHub Desktop.
Save john990/9520724 to your computer and use it in GitHub Desktop.
android,显示通知
private void showNotification(Context context){
Bitmap btm = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(context.getString(R.string.please_record_today_event))
.setContentText(context.getString(R.string.alarm_summary));
// 第一次提示消息的时候显示在通知栏上
builder.setTicker(context.getString(R.string.please_record_today_event));
// builder.setNumber(12);
builder.setLargeIcon(btm);
// 自己维护通知的消失
builder.setAutoCancel(true);
builder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
// 构建一个Intent
Intent resultIntent = new Intent(context,MainActivity.class);
// 封装一个Intent
PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// 设置通知主题的意图
builder.setContentIntent(resultPendingIntent);
// 获取通知管理器对象
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment