【android开发-20】android中notification的用法讲解

不管现实多么惨不忍睹,都要持之以恒地相信,这只是黎明前短暂的黑暗而已。不要惶恐眼前的难关迈不过去,不要担心此刻的付出没有回报,别再花时间等待天降好运。真诚做人,努力做事!你想要的,岁月都会给你。【android开发-20】android中notification的用法讲解,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

1,notification的基本用法

在Android中,通知(Notification)是一种在状态栏上显示的消息提示,用户点击通知后可以展开更多详细信息。以下是基本的通知用法:

1,创建通知
创建通知需要使用Notification类,可以通过以下代码创建一个简单的通知:

Notification notification = new Notification.Builder(context)  
    .setSmallIcon(R.drawable.notification_icon)  // 设置小图标  
    .setContentTitle("通知标题")  // 设置标题  
    .setContentText("这是通知内容")  // 设置内容  
    .build();

2,创建通知通道
从Android 8.0开始,需要在通知中创建通知通道。通知通道用于定义通知的显示方式、声音和振动方式等。可以使用以下代码创建一个通知通道:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {  
    CharSequence name = getString(R.string.channel_name);  
    String description = getString(R.string.channel_description);  
    int importance = NotificationManager.IMPORTANCE_DEFAULT;  
    NotificationChannel channel = new NotificationChannel("channel_id", name, importance);  
    channel.setDescription(description);  
    // Register the channel with the system; you can't change the importance  
    // or other notification behaviors after this  
    NotificationManager notificationManager = getSystemService(NotificationManager.class);  
    notificationManager.createNotificationChannel(channel);  
}

3,创建通知请求
创建通知请求需要使用NotificationManager类和NotificationChannel对象。可以使用以下代码创建一个通知请求:

NotificationManager notificationManager = getSystemService(NotificationManager.class);  
NotificationChannel channel = notificationManager.getNotificationChannel("channel_id");  
if (channel != null) {  
    notificationManager.notify("notification_id", channel, notification);  
}

其中,”notification_id”是一个唯一的标识符,用于区分不同的通知;”channel_id”是之前创建的通知通道的ID;”notification”是之前创建的通知对象。

2,notification设置铃声和振动
在Android中,你可以在Notification对象中设置铃声和振动。以下是一个例子:

// 创建一个新的Notification对象  
Notification notification = new Notification.Builder(context)  
    .setSmallIcon(R.drawable.notification_icon)  // 设置小图标  
    .setContentTitle("通知标题")  // 设置标题  
    .setContentText("这是通知内容")  // 设置内容  
    .setSound(soundUri)  // 设置铃声  
    .setVibrate(new long[]{100, 200, 300, 400, 500})  // 设置振动模式  
    .build();  

在这个例子中,setSound()方法用于设置通知的铃声,参数是一个Uri对象,表示铃声文件的路径。

同样,setVibrate()方法用于设置通知的振动模式。参数是一个长整数数组,表示不同的振动持续时间。第一个元素表示第一个振动的时间,第二个元素表示第二个振动的时间,以此类推。最后一个元素表示最后一个振动的时间。振动还需要声明权限:

<uses-permission android.name="android.permission.VIBRATE">

3,notification点击事件
当用户点击Android通知时,你可以通过在Notification对象中设置Intent来启动一个活动或执行其他操作。以下是一个例子:

// 创建Intent对象以启动一个活动  
Intent intent = new Intent(context, ExampleActivity.class);  
// 添加一个额外的数据字段,以便在活动中使用  
intent.putExtra("extra_data", "This is additional data.");  
// 创建PendingIntent对象,以便稍后启动这个活动  
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);  
  
// 创建一个新的Notification对象并设置点击事件  
Notification notification = new Notification.Builder(context)  
    .setSmallIcon(R.drawable.notification_icon)  
    .setContentTitle("通知标题")  
    .setContentText("这是通知内容")  
    .setSound(soundUri)  
    .setVibrate(new long[]{100, 200, 300, 400, 500})  
    .setContentIntent(pendingIntent)  // 设置点击事件  
    .build();  
  
// 发送通知  
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);  
notificationManager.notify("notification_id", notification);

在这个例子中,我们创建了一个Intent对象以启动名为ExampleActivity的活动。然后,我们添加了一个额外的数据字段,以便在启动活动时可以使用。接下来,我们创建了一个PendingIntent对象,以便稍后启动这个活动。最后,我们将PendingIntent对象设置为Notification对象的contentIntent属性,这样当用户点击通知时就会启动这个活动。

PendingIntent是Android中的一个类,它表示一种处于pending状态(待定、等待、即将发生)的意图。与Intent不同的是,PendingIntent是将在未来某个不确定时刻发生,而Intent是立刻发生。

PendingIntent支持三种待定意图:启动Activity、启动Service和发送广播。要得到PendingIntent的实例,必须传入一个Intent作为参数,同时还需要有Context作为参数。

当创建PendingIntent时,需要使用Context、Intent和额外的参数来创建PendingIntent对象。这些参数可以控制PendingIntent的行为。例如,可以使用getActivity()方法创建一个用于启动Activity的PendingIntent,或者使用getBroadcast()方法创建一个用于发送广播的PendingIntent。

当用户点击一个通知时,可以使用PendingIntent来启动一个活动或执行其他操作。在通知中设置PendingIntent的contentIntent属性,然后在用户点击通知时调用PendingIntent的execute()方法来启动活动。

总之,PendingIntent是一种方便的方式来在Android应用程序中执行延迟操作,例如启动活动、启动服务和发送广播等。

4,notification的取消
在Android中,取消通知可以通过使用通知的cancel()方法实现。你需要保留对通知的引用,以便稍后取消它。下面是一个简单的例子:

// 创建一个新的通知  
Notification notification = new Notification.Builder(context)  
    .setSmallIcon(R.drawable.notification_icon)  
    .setContentTitle("通知标题")  
    .setContentText("这是通知内容")  
    .build();  
  
// 发送通知  
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);  
notificationManager.notify("notification_id", notification);  
  
// 在某个时间点,取消通知  
notificationManager.cancel("notification_id");

在这个例子中,“notification_id”是通知的唯一标识符。当你调用cancel()方法时,所有具有相同标识符的通知都将被取消。如果你想取消所有的通知,可以传递一个空字符串(””)作为标识符。

请注意,你只能在应用程序的上下文中取消通知,不能在其他应用程序的上下文中取消通知。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/181977.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!