android中的notificatio

本文介绍了一种在Android应用中创建不同类型通知的方法,包括基本通知、大文本样式通知、大图片样式通知及收件箱样式通知。通过具体的代码示例展示了如何使用Notification.Builder构造这些通知,并设置了各自的特性和行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先获得NotificationManager,然后通过PendingIntent 发出nofitication,在putextr里面把想通知的事情在里面说清楚,有4中notification

1、sendBasicNotification

2、sendBigTextStyleNotification

3、sendBigPictureStyleNotification

4、sendInboxStyleNotification


通过PendingIntent 回调回activity,实现了通知用户具体的事情。


public class NotificationMainActivity extends Activity {



@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}


public void sendBasicNotification(View view) {
Notification notification = new Notification.Builder(this)
.setContentTitle("Basic Notification")
.setContentText("Basic Notification, used earlier")
.setSmallIcon(R.drawable.ic_launcher_share).build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = getNotificationManager();
notificationManager.notify(0, notification);
}


public void sendBigTextStyleNotification(View view) {
String msgText = "Jeally Bean Notification example!! "
+ "where you will see three different kind of notification. "
+ "you can even put the very long string here.";


NotificationManager notificationManager = getNotificationManager();
PendingIntent pi = getPendingIntent();
Builder builder = new Notification.Builder(this);
builder.setContentTitle("Big text Notofication")
.setContentText("Big text Notification")
.setSmallIcon(R.drawable.ic_launcher)
.addAction(R.drawable.ic_launcher_web, "show activity", pi)
.setAutoCancel(true);
Notification notification = new Notification.BigTextStyle(builder)
.bigText(msgText)
.build();
notificationManager.notify(0, notification);
}


public void sendBigPictureStyleNotification(View view) {
PendingIntent pi = getPendingIntent();
Builder builder = new Notification.Builder(this);
builder.setContentTitle("BP notification")
// Notification title
.setContentText("BigPicutre notification")
// you can put subject line.
.setSmallIcon(R.drawable.ic_launcher)
// Set your notification icon here.
.addAction(R.drawable.ic_launcher_web, "show activity", pi)
.addAction(
R.drawable.ic_launcher_share,
"Share",
PendingIntent.getActivity(getApplicationContext(), 0,
getIntent(), 0, null));


// Now create the Big picture notification.
Notification notification = new Notification.BigPictureStyle(builder)
.bigPicture(
BitmapFactory.decodeResource(getResources(),
R.drawable.big_picture)).build();
// Put the auto cancel notification flag
notification.flags |= Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = getNotificationManager();
notificationManager.notify(0, notification);
}


public void sendInboxStyleNotification(View view) {
PendingIntent pi = getPendingIntent();
Builder builder = new Notification.Builder(this)
.setContentTitle("IS Notification")
.setContentText("Inbox Style notification!!")
.setSmallIcon(R.drawable.ic_launcher)
.addAction(R.drawable.ic_launcher_web, "show activity", pi);


Notification notification = new Notification.InboxStyle(builder)
.addLine("First message").addLine("Second message")
.addLine("Thrid message").addLine("Fourth Message")
.setSummaryText("+2 more").build();
// Put the auto cancel notification flag
notification.flags |= Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = getNotificationManager();
notificationManager.notify(0, notification);
}


public PendingIntent getPendingIntent() {
return PendingIntent.getActivity(this, 0, new Intent(this,
HandleNotificationActivity.class), 0);
}


public NotificationManager getNotificationManager() {
return (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值