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

时间:2014-11-28 03:47:24

标签: android android-notifications android-2.3-gingerbread download-manager

我正在使用下载管理器将一些文件下载到应用程序中。它适用于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事件。

广播接收器

  

公共类DownloadBroadcastReceiver扩展BroadcastReceiver {       @覆盖       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(上下文中,#34;下载   完整&#34;,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>

0 个答案:

没有答案