How to prevent download notification from disappearing in Android API 10
I am using download manager to download some files into the app. It works fine for API level > 10 but the notification comes and goes for API 10. I have to implement it on API 10 so please dont ask me to change the MinSdkVersion. The code for the download manager is as follows.
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);
}
}
I also tried using a broadcast receiver but am unable to handle the onclick event of the notification.
Broadcast Receiver
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) {
}
else
{
Notification(context,"Downloaded");
}
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) {
// Set Notification Title
String strtitle = "Attachment Download Complete";
// Open NotificationView Class on Notification Click
Intent intent = new Intent(context, NotificationView.class);
// Send data to NotificationView Class
intent.putExtra("title", strtitle);
intent.putExtra("text", message);
// Open NotificationView.java Activity
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
// Create Notification using NotificationCompat.Builder
NotificationCompat.Builder builder = new NotificationCompat.Builder(
context)
// Set Icon
.setSmallIcon(R.drawable.ic_action_attachment)
// Set Ticker Message
.setTicker(message)
// Set Title
.setContentTitle("Attachment")
// Set Text
.setContentText(message)
// Add an Action Button below Notification
.addAction(R.drawable.ic_action_attachment, "Action Button", pIntent)
// Set PendingIntent into Notification
.setContentIntent(pIntent)
// Dismiss Notification
.setAutoCancel(true);
// Create Notification Manager
NotificationManager notificationmanager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
// Build Notification with Notification Manager
notificationmanager.notify(0, builder.build());
}
}
the Download complete event is triggered, but the notification clicked is not. I have registered the receivers in the manifest.
<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>
链接地址: http://www.djcxy.com/p/50662.html
上一篇: 如何最好地使用文件版本和组件版本?