Android学习之Notifications(超详细)

当后台服务需要提示用户来响应某个事件时,应该使用状态栏通知。后台服务不应该自己去启动一个activity来与用户互动,它应该创建一个状态栏通知,当用户选择这个通知去启动activity

创建一个普通的通知栏
  NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
Notification notification = builder
        .setSmallIcon(R.drawable.tubiao)                                                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.fengmian,null))                                        .setWhen(System.currentTimeMillis())
.setContentTitle("this is title")
.setContentText("this is content")
 .build();
   manager.notify(1,notification);

效果:
这里写图片描述
这是一个最普通的通知栏,一般都会为通知添加至少一个动作(Action),这个动作可以是跳转到Activity、启动一个Service或发送一个Broadcas等。 通过以下方式为通知添加动作:
使用PendingIntent
通过大视图通知的 Action Button //仅支持Android 4.1 (API level 16)及更高版本

NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
   NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 
Intent intent = new Intent(this,geciActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,1,intent,0);
Notification notification = builder.setSmallIcon(R.drawable.tubiao)                                          .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.fengmian,null))                                         .setWhen(System.currentTimeMillis())
.setContentTitle("this is title")
.setContentText("this is content")                                          .setContentIntent(pendingIntent)
.build();
manager.notify(1,notification);
更新通知

更新通知很简单,只需再次发送相同ID的通知即可,如果之前的通知依然存在则会更新通知属性,如果之前通知不存在则重新创建。

Notification notification2 =
                 builder.setSmallIcon(R.drawable.tubiao)
 .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.fengmian,null))
                .setWhen(System.currentTimeMillis())
                .setContentTitle("this is title22222")
                .setContentText("this is content222222")
                .setContentIntent(pendingIntent)
                .build();
        manager.notify(1,notification2);

效果:
这里写图片描述

取消通知:

取消通知有如下4种方式:
1. 点击通知栏的清除按钮,会清除所有可清除的通知0
2. 设置了 setAutoCancel() 或 FLAG_AUTO_CANCEL的通知,点击该通知时会清除它
3. 通过 NotificationManager 调用 cancel() 方法清除指定ID的通知
4. 通过 NotificationManager 调用 cancelAll() 方法清除所有该应用之前发送的通知

大视图通知:

通知有两个类型,一种是普通视图,一种是大视图:
普通视图:
这里写图片描述
大视图:
这里写图片描述

默认情况下是普通视图,可以通过NotificationCompat.Bulider.setStyle()设置大视图
例子:

Notification notification3 =
 builder.setSmallIcon(R.drawable.tubiao)
 .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.fengmian,null))
.setContentTitle("this is title2222233333")
 .setContentText("this is content22222233333")
 .setContentIntent(pendingIntent)
 .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(),R.drawable.fengmian,null)))
.setStyle(new NotificationCompat.BigTextStyle().bigText("ddsfsdvdvddadaergadvadfaasdferavadgarvaergadvaergvasgqrefaerhtrrthbsdbsrtgadgw"))                       .addAction(R.drawable.zuo,null,pendingIntent)                       .addAction(R.drawable.you,null,pendingIntent)
.build();
manager.notify(1,notification3);

效果:
这里写图片描述

这个大视图是通过双手上下平移展开的。

进度条通知
  • 明确进度的进度条
    使用setProgress(max, progress, false)来更新进度。
    max: 最大进度值
    progress: 当前进度
    false: 是否是不明确的进度条
    模拟下载过程:
int id = 1;
...
mNotifyManager = (NotificationManager) 
    getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(this);
mBuilder.setContentTitle("Picture Download")
    .setContentText("Download in progress")
    .setSmallIcon(R.drawable.ic_notification);

// Start a lengthy operation in a background thread
new Thread(
    new Runnable() {
 
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很抱歉,我的理解出现了误差,我之前的回答是关于关闭通知的权限设置。如果您需要禁止用户关闭 Android 上的通知,请按照以下步骤操作: 1. 在 AndroidManifest.xml 文件中,为您的应用程序添加一个权限声明: ```xml <uses-permission android:name="android.permission.STATUS_BAR" /> ``` 2. 在您的应用程序中创建一个 Service 类,并在其中实现以下代码: ```java public class NotificationService extends Service { private static final String TAG = "NotificationService"; private NotificationManager notificationManager; @Override public void onCreate() { super.onCreate(); notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); } @Override public int onStartCommand(Intent intent, int flags, int startId) { // 创建一个通知并显示在状态栏中 NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_notification) .setContentTitle("通知标题") .setContentText("通知内容") .setAutoCancel(true); Notification notification = builder.build(); notificationManager.notify(1, notification); // 将服务设置为前台服务,这样通知将无法被关闭 startForeground(1, notification); return super.onStartCommand(intent, flags, startId); } @Nullable @Override public IBinder onBind(Intent intent) { return null; } } ``` 3. 在您的应用程序中启动服务: ```java Intent serviceIntent = new Intent(this, NotificationService.class); startService(serviceIntent); ``` 通过上述方式,您可以创建一个前台服务,并在其中显示一个通知。由于该服务已被标记为前台服务,因此用户将无法在 Android 系统中关闭通知。但是,这种方式可能会引起用户反感,并且可能被视为滥用通知的行为。因此,我们建议您在使用此方法时要谨慎处理,确保您的应用程序不会滥用通知功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值