PSN*_*PSN 9 notifications android
我已经创建了一个自定义通知,用于从给定的URL下载mp3文件.但我需要知道如何添加pause
和cancel
按钮到我创建的自定义通知.
以下是自定义通知的部分代码:
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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
您需要使用媒体控制将特定操作设置为通知,您可以添加具有相对挂起意图的特定操作
.addAction(R.drawable.ic_prev, "Previous", prevPendingIntent)
.addAction(R.drawable.ic_pause, "Pause", pausePendingIntent)
.addAction(R.drawable.ic_next, "Next", nextPendingIntent)
Run Code Online (Sandbox Code Playgroud)
您还需要使用以下代码设置媒体样式
.setStyle(MediaNotificationCompat.MediaStyle()
.setShowActionsInCompactView(1 /* #1: pause button \*/)
.setMediaSession(mMediaSession.getSessionToken()))
Run Code Online (Sandbox Code Playgroud)
您还可以查看此链接以获取更多说明。
归档时间: |
|
查看次数: |
569 次 |
最近记录: |