下载文件时的通知图标

2022-09-03 09:30:57

有谁知道如何在下载文件时使用状态栏中的通知图标?就像您在应用商店中下载文件一样。图标就像在gif中一样。


答案 1

只需为此使用默认的Android资源即可。android.R.drawable.stat_sys_download


答案 2

下面是一个功能齐全的示例,它将在状态栏中显示默认的“系统”下载通知图标。

private static void showProgressNotification(Context context, int notificationId, String title, String message)
{
    NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);

    mBuilder.setContentTitle(title)
    .setContentText(message)
    .setSmallIcon(android.R.drawable.stat_sys_download)
    .setTicker("")
    .setProgress(0, 0, true);

    manager.notify(notificationId, mBuilder.build());
}

完成“下载”操作后,清除通知:

private static void hideProgressNotification(final NotificationManager manager, final Context context, final int id)
{
    manager.cancel(id);
}

推荐