在NotificationCompat操作中切换暂停和继续操作

时间:2018-11-26 13:57:47

标签: java android android-notifications

大家!

因此,我已经成功创建了音乐应用程序的下载管理器,并且可以从通知中查看下载进度,并在 DownloadListener 类中创建了意图和操作来帮助处理暂停并继续下载。所以,目前,这就是通知的样子 Current Notification with 3 actions

它目前已经执行了3个操作(暂停,继续,取消),但是我希望它只是2个可见操作(暂停和取消),因此,当点击暂停时,它会执行“暂停”操作,然后将文本和操作更改为继续,这样,如果暂停下载,则下一个要执行的操作将是继续操作。 / p>

这是 DownloadListener 类(这是我创建Notification动作的地方)。

private DownloadService downloadService = null;

private int lastProgress = 0;

public void setDownloadService(DownloadService downloadService) {
    this.downloadService = downloadService;
}

public DownloadService getDownloadService() {
    return downloadService;
}

public void onSuccess() {
    downloadService.stopForeground(true);
    sendDownloadNotification("Download success.", -1);
}

public void onFailed() {
    downloadService.stopForeground(true);
    sendDownloadNotification("Download failed.", -1);
}
public void onPaused() {
    sendDownloadNotification("Download paused.", lastProgress);
}
public void onCanceled() {
    downloadService.stopForeground(true);
    sendDownloadNotification("Download canceled.", -1);
}

public void onUpdateDownloadProgress(int progress) {
    try {
        lastProgress = progress;
        sendDownloadNotification("Downloading...", progress);

        // Thread sleep 0.2 seconds to let Pause, Continue and Cancel button in notification clickable.
        Thread.sleep(200);
    } catch(Exception ex) {
        ex.printStackTrace();
    }
}


public void sendDownloadNotification(String title, int progress) {
    Notification notification = getDownloadNotification(title, progress);
    NotificationManager notificationManager = (NotificationManager)downloadService.getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(1, notification);
}

public Notification getDownloadNotification(String title, int progress) {
    Intent intent = new Intent();
    PendingIntent pendingIntent = PendingIntent.getActivity(downloadService, 0, intent, 0);
    NotificationCompat.Builder notifyBuilder = new NotificationCompat.Builder(downloadService);
    notifyBuilder.setSmallIcon(R.drawable.ic_file_download);

    Bitmap bitmap = BitmapFactory.decodeResource(downloadService.getResources(), R.mipmap.ic_launcher_round);
    notifyBuilder.setLargeIcon(bitmap);

    notifyBuilder.setContentIntent(pendingIntent);
    notifyBuilder.setContentTitle(title);
    notifyBuilder.setFullScreenIntent(pendingIntent, true);

    if (progress > 0 && progress < 100) {
        StringBuilder stringBuffer = new StringBuilder();
        stringBuffer.append("Download progress ");
        stringBuffer.append(progress);
        stringBuffer.append("%");

        notifyBuilder.setContentText("Download progress " + progress + "%");

        notifyBuilder.setProgress(100, progress, false);

        // Add Pause download button intent in notification.
        Intent pauseDownloadIntent = new Intent(getDownloadService(), DownloadService.class);
        pauseDownloadIntent.setAction(DownloadService.ACTION_PAUSE_DOWNLOAD);
        PendingIntent pauseDownloadPendingIntent = PendingIntent.getService(getDownloadService(), 0, pauseDownloadIntent, 0);
        NotificationCompat.Action pauseDownloadAction = new NotificationCompat.Action(android.R.drawable.ic_media_pause, "Pause", pauseDownloadPendingIntent);
        notifyBuilder.addAction(pauseDownloadAction);

        // Add Continue download button intent in notification.
        Intent continueDownloadIntent = new Intent(getDownloadService(), DownloadService.class);
        continueDownloadIntent.setAction(DownloadService.ACTION_CONTINUE_DOWNLOAD);
        PendingIntent continueDownloadPendingIntent = PendingIntent.getService(getDownloadService(), 0, continueDownloadIntent, 0);
        NotificationCompat.Action continueDownloadAction = new NotificationCompat.Action(android.R.drawable.ic_media_pause, "Continue", continueDownloadPendingIntent);
        notifyBuilder.addAction(continueDownloadAction);

        // Add Cancel download button intent in notification.
        Intent cancelDownloadIntent = new Intent(getDownloadService(), DownloadService.class);
        cancelDownloadIntent.setAction(DownloadService.ACTION_CANCEL_DOWNLOAD);
        PendingIntent cancelDownloadPendingIntent = PendingIntent.getService(getDownloadService(), 0, cancelDownloadIntent, 0);
        NotificationCompat.Action cancelDownloadAction = new NotificationCompat.Action(android.R.drawable.ic_delete, "Cancel", cancelDownloadPendingIntent);
        notifyBuilder.addAction(cancelDownloadAction);
    }

    Notification notification = notifyBuilder.build();
    return notification;
}

然后,我有一个 DownloadService 类,该类调用通知操作并实际执行这些操作(如Pause(暂停),在单击时将暂停下载)。与继续取消操作相同。

public class DownloadService extends Service {

public static final String ACTION_PAUSE_DOWNLOAD = "ACTION_PAUSE_DOWNLOAD";

public static final String ACTION_CONTINUE_DOWNLOAD = "ACTION_CONTINUE_DOWNLOAD";

public static final String ACTION_CANCEL_DOWNLOAD = "ACTION_CANCEL_DOWNLOAD";

private DownloadBinder downloadBinder = new DownloadBinder();
private DownloadListener downloadListener = new DownloadListener();

public DownloadService() {
}

@Override
public IBinder onBind(Intent intent) {
    downloadBinder.getDownloadListener().setDownloadService(this);
    return downloadBinder;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    String action = intent.getAction();
    if (ACTION_PAUSE_DOWNLOAD.equals(action)) {
        downloadBinder.pauseDownload();
        Toast.makeText(getApplicationContext(), "Download is paused", Toast.LENGTH_LONG).show();
    } else if(ACTION_CANCEL_DOWNLOAD.equals(action)) {
        downloadBinder.cancelDownload();
        Toast.makeText(getApplicationContext(), "Download is canceled", Toast.LENGTH_LONG).show();
    } else if(ACTION_CONTINUE_DOWNLOAD.equals(action)) {
        downloadBinder.continueDownload();
        Toast.makeText(getApplicationContext(), "Download continue", Toast.LENGTH_LONG).show();
    }

    return super.onStartCommand(intent, flags, startId);
}

因此,请问我如何使通知仅显示暂停取消。这样,当我单击暂停时,它将通过暂停下载执行暂停操作,将文本和操作更改为仅继续操作当下载被暂停并且单击继续时,如果下载被暂停,请通过恢复下载来执行继续操作。

0 个答案:

没有答案