gpt4 book ai didi

android - 如何将暂停和取消按钮添加到自定义下载通知android

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:37:56 24 4
gpt4 key购买 nike

我已经为从给定 URL 下载 mp3 文件创建了自定义通知。但我需要知道如何将 pausecancel 按钮添加到我创建的自定义通知中。

这是自定义通知的部分代码:

 if (!downloadUrl.toString().isEmpty()) {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(downloadUrl));
request.setMimeType("audio/MP3");
request.setTitle(vMeta.getTitle());
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
request.setDestinationInExternalPublicDir(storage, vMeta.getTitle() + extension);
final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);

final long id = manager.enqueue(request);


registerReceiver(new DownloadReceiver(id, storage, vMeta.getTitle() + extension),
new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
mBuilder = new NotificationCompat.Builder(getApplicationContext());
Intent intent = new Intent();
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
mBuilder.setContentIntent(pendingIntent);
mBuilder.setSmallIcon(R.drawable.ic_music_video_white_24dp);
mBuilder.setContentTitle("Downloading");
mBuilder.setContentText(vMeta.getTitle());
mBuilder.setOngoing(false);
//mBuilder.addAction();
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

new Thread(new Runnable() {
@Override
public void run() {

boolean downloading = true;

while (downloading) {
DownloadManager.Query q = new DownloadManager.Query();
q.setFilterById(id);
Cursor cursor = manager.query(q);
cursor.moveToFirst();
if( cursor != null && cursor.moveToFirst() ) {
bytes_downloaded = cursor.getInt(cursor
.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
bytes_total = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
dl_progress = (int) ((bytes_downloaded * 100l) / bytes_total);
}
mNotificationManager.notify(001, mBuilder.build());
if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL) {
downloading = false;
mBuilder.setContentTitle("Download complete")
.setOngoing(false)
.setAutoCancel(true)
.setProgress(100,100,false);
mNotificationManager.notify(001, mBuilder.build());
}


runOnUiThread(new Runnable() {

@Override
public void run() {
mBuilder.setContentTitle("Downloading: "+dl_progress+"%");
mBuilder.setProgress(100,dl_progress,false);
mNotificationManager.notify(001, mBuilder.build());

}
});

cursor.close();
}

}
}).start();



}
}
}
}.extract(ytLink, true, true);
}
});
}
}
protected void onDestroy() {

super.onDestroy();
}
public void onBackPressed(){
super.onBackPressed();
}

public class DownloadReceiver extends BroadcastReceiver {
private long id;
private String dirType;
private String subPath;

public DownloadReceiver(long id, String dirType, String subPath) {
this.id = id;
this.dirType = dirType;
this.subPath = subPath;
}

@Override
public void onReceive(Context context, Intent intent) {
if (intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1) == id) {
MainActivity.this.unregisterReceiver(this);
File oldFile = new File(Environment.getExternalStoragePublicDirectory(dirType), subPath);
String newSubPath = subPath.substring(0, subPath.lastIndexOf('.')) +"|MEGA"+".mp3";
File newFile = new File(Environment.getExternalStoragePublicDirectory(dirType), newSubPath);
Boolean result = oldFile.renameTo(newFile);
Toast.makeText(context, "Download " + (result ? "succeeded" : "failed"), Toast.LENGTH_SHORT).show();
}
}
}

最佳答案

您需要将特定操作设置为带有媒体控件的通知您可以添加具有相对未决 Intent 的特定操作

.addAction(R.drawable.ic_prev, "Previous", prevPendingIntent) 
.addAction(R.drawable.ic_pause, "Pause", pausePendingIntent)
.addAction(R.drawable.ic_next, "Next", nextPendingIntent)

您还需要使用以下代码设置媒体样式

 .setStyle(MediaNotificationCompat.MediaStyle()
.setShowActionsInCompactView(1 /* #1: pause button \*/)
.setMediaSession(mMediaSession.getSessionToken()))

您还可以检查this link获取更多说明。

关于android - 如何将暂停和取消按钮添加到自定义下载通知android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49344616/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com