在Android 10中,可以通过以下步骤从通知开始活动:
以下是一个示例代码:
// 创建通知渠道
NotificationChannel channel = new NotificationChannel("channel_id", "Channel Name", NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
// 构建通知
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "channel_id")
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("Notification Title")
.setContentText("Notification Content")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent) // 设置点击事件
.setAutoCancel(true); // 点击后自动取消通知
// 启动活动
Intent intent = new Intent(this, TargetActivity.class);
intent.putExtra("key", "value");
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// 发送通知
notificationManager.notify(1, builder.build());
在上述示例中,需要替换"channel_id"为自定义的通知渠道ID,替换R.drawable.notification_icon为自定义的通知图标,替换"Notification Title"和"Notification Content"为自定义的通知标题和内容,替换TargetActivity.class为目标活动的类名。
注意:以上示例代码仅涵盖了从通知开始活动的基本步骤,实际应用中可能需要根据具体需求进行适当的修改和扩展。
领取专属 10元无门槛券
手把手带您无忧上云