我正在使用下载管理器将一些文件下载到应用程序中 . 它适用于API级别> 10但是通知来自API 10.我必须在API 10上实现它所以请不要让我更改MinSdkVersion . 下载管理器的代码如下 .
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Uri uri = Uri
.parse(serverConnection.url + serverConnection.studentSearchService + "GetAssignmentFile/" + serverConnection.connectionString + fileList.get(i).getFileId());
DownloadManager downloadManager = (DownloadManager) getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request downloadReq = new DownloadManager.Request(uri);
downloadReq
.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI
| DownloadManager.Request.NETWORK_MOBILE);
// downloadReq.allowScanningByMediaScanner();
downloadReq.setMimeType(fileList.get(i).getFileType());
downloadReq.setTitle(fileList.get(i).getFileName());
NotificationView.fileTypeToOpen=fileList.get(i).getFileType();
NotificationView.fileToOpen=fileList.get(i).getFileName();
downloadReq.setDescription("attachment");
downloadReq.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileList.get(i).getFileName());
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
downloadReq.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE | DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
else
{
downloadReq.setShowRunningNotification(true);
}
long enqueue = downloadManager.enqueue(downloadReq);
}
}
我也尝试使用广播接收器,但无法处理通知的onclick事件 .
广播接收器
public class DownloadBroadcastReceiver extends BroadcastReceiver {@Override public void onReceive(Context context,Intent intent){String action = intent.getAction(); if(DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)){
if(android.os.Build.VERSION.SDK_INT> = android.os.Build.VERSION_CODES.HONEYCOMB){
}
其他
{
通知(上下文中, “下载”);
}
Toast.makeText(context,“Download Complete”,Toast.LENGTH_SHORT).show(); if(DownloadManager.ACTION_NOTIFICATION_CLICKED.equals(action))
{
Toast.makeText(context,“Notification Cliekd”,Toast.LENGTH_SHORT).show();
}
}
public void Notification(Context context,String message){
//设置通知 Headers
String strtitle =“附件下载完成”;
//在Notification Click上打开NotificationView类
Intent intent = new Intent(context,NotificationView.class);
//将数据发送到NotificationView类
intent.putExtra(“title”,strtitle);
intent.putExtra(“text”,message);
//打开NotificationView.java活动
PendingIntent pIntent = PendingIntent.getActivity(context,0,intent,
PendingIntent.FLAG_UPDATE_CURRENT);
//使用NotificationCompat.Builder创建通知
NotificationCompat.Builder builder = new NotificationCompat.Builder(
上下文)
//设置图标
.setSmallIcon(R.drawable.ic_action_attachment)
//设置Ticker消息
.setTicker(消息)
//设置 Headers
.setContentTitle( “附件”)
//设置文字
.setContentText(消息)
//在通知下添加一个动作按钮
.addAction(R.drawable.ic_action_attachment,“Action Button”,pIntent)
//将PendingIntent设置为通知
.setContentIntent(pIntent)
//驳回通知
.setAutoCancel(真);
//创建通知管理器
NotificationManager notificationmanager =(NotificationManager)上下文
.getSystemService(Context.NOTIFICATION_SERVICE);
//使用通知管理器构建通知
notificationmanager.notify(0,builder.build());
}
}
触发了下载完成事件,但未点击通知 . 我在清单中注册了接收器 .
<receiver android:name="appPath.DownloadBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>
<action android:name="android.intent.action.NOTIFICATION_CLICKED"/>
</intent-filter>
</receiver>