如何防止下载通知在Android API 10中消失

我正在使用下载管理器将一些文件下载到应用程序中。 它适用于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) {
            }
        else
        {
            Notification(context,"Downloaded");

        }

Toast.makeText(上下文,“下载完成”,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());

}

}

下载完成事件被触发,但点击的通知不是。 我已经在清单中注册了接收者。

   <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/50661.html

上一篇: How to prevent download notification from disappearing in Android API 10

下一篇: Button on Widget Android