我在尝试处理下载的 .mp3 文件的通知点击事件时遇到问题。我已经注册了以下内容:
问题是,当我点击已完成下载时,我会得到一个可以处理下载 .mp3 文件(如VLC)的应用列表。
有没有办法限制通知项目点击,以便它只由我的应用程序处理?顺便说一句,我的应用程序甚至没有显示在列表中。
P.S。 如果解决方案不涉及必须创建我的自定义通知处理,那将是很好的。
编辑1: 这是开始下载的代码:
private void downloadRequest(int position, String url, String title, String description, String filename) {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription(description);
request.setTitle(title);
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
try {
request.setDestinationUri(Uri.fromFile(new File(FileUtils.getAudioDirectory(getActivity()) + filename)));
} catch (IOException e) {
e.printStackTrace();
}
enqueue = mDownloadManager.enqueue(request);
根据matty357的要求,请点击我的广播接收器点击:
private BroadcastReceiver receiverNotificationClicked = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String extraId = DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS;
long[] references = intent.getLongArrayExtra(extraId);
for(long reference : references) {
AppLog.d("DOWNLOAD_MANAGER", "reference (clicked): " + reference);
}
AppLog.d("DOWNLOAD_MANAGER", "Received click");
}
};
答案 0 :(得分:2)
解决方法是:
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
因此,下载完成后您将不会收到通知。和
context.registerReceiver(onComplete,
new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
因此,您可以在下载完成后发出通知。
BroadcastReceiver onComplete=new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
Log.d(TAG,"onComplete");
// AND HERE SET YOUR ONW NOTIFICATION WHICH YOU WILL ABLE TO HANDLE
}
};
答案 1 :(得分:0)
您可以通过编程方式自行打开它。
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File("/sdcard/test.mp4");
intent.setDataAndType(Uri.fromFile(file), "video/*");
startActivity(intent);
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File("/sdcard/test.mp3");
intent.setDataAndType(Uri.fromFile(file), "audio/*");
startActivity(intent);
使用intent.setPackage(" packageName")
http://developer.android.com/reference/android/content/Intent.html#setPackage(java.lang.String)