不显示多个通知

时间:2014-06-04 06:20:59

标签: android android-notifications

我有一个编辑框,用户可以在其中指定要下载的链接(以逗号分隔)。我的要求是每次下载时都必须出现单独的通知。在我当前的代码中,问题是我无法一次显示多个通知。它们按顺序出现,即一旦下载完成,则会出现另一个通知。我尝试了很多东西,但似乎没有什么对我有用。任何与此相关的建议都非常感谢。

代码:

class DownloadTask extends AsyncTask<String, Integer, String> {
        private String filePath;
        private Context mdcontext;
        private PowerManager.WakeLock mWakeLock;
        NotificationManager nManager;
        Notification notif;
        android.support.v4.app.NotificationCompat.Builder mBuilder;
        int Id;

        Notification not;

        public DownloadTask(Context context, int id) {
            mdcontext = context;
            this.Id = id;

            nManager = (NotificationManager) context
                    .getSystemService(Context.NOTIFICATION_SERVICE);
            mBuilder = new NotificationCompat.Builder(context);
            mBuilder.setContentTitle("Download")
                    .setContentText("Download in progress")
                    .setSmallIcon(R.drawable.ic_launcher);
            mBuilder.setProgress(0, 0, true);
            notif = mBuilder.build();

            notif.flags = Notification.FLAG_AUTO_CANCEL;
            Log.i("NotificationCreator", "building notification");
        }

        @Override
        protected String doInBackground(String... sUrl) {
            InputStream input = null;
            OutputStream output = null;
            HttpURLConnection connection = null;
            String apkNames = sUrl[0];

            String files[] = apkNames.split("/");
            String apkName = files[files.length - 1];

            try {

                URL url = new URL(sUrl[0]);
                connection = (HttpURLConnection) url.openConnection();
                connection.connect();

                // expect HTTP 200 OK, so we don't mistakenly save error report
                // instead of the file
                if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
                    return "Server returned HTTP "
                            + connection.getResponseCode() + " "
                            + connection.getResponseMessage();
                }

                // this will be useful to display download percentage
                // might be -1: server did not report the length
                int fileLength = connection.getContentLength();

                // download the file
                input = connection.getInputStream();
                // try this
                if (!Environment.MEDIA_MOUNTED.equals(Environment
                        .getExternalStorageState())) {

                    filePath = context.getFilesDir() + "/" + apkName;
                    output = new FileOutputStream(filePath);

                } else {
                    filePath = Environment.getExternalStorageDirectory() + "/"
                            + apkName;
                    output = new FileOutputStream(filePath);

                }

                byte data[] = new byte[4096];
                int total = 0;
                int count;
                synchronized (nManager) {
                    nManager.notify(Id, notif);
                }
                Log.i("Installer", "Showing notification");
                while ((count = input.read(data)) != -1) {
                    // allow canceling with back button
                    if (isCancelled()) {
                        input.close();
                        return null;
                    }
                    total += count;
                    if (fileLength > 0)
                        output.write(data, 0, count);
                }
                SharedPreferences manag = context.getSharedPreferences(
                        "Paths file", Context.MODE_PRIVATE);
                Editor edt = manag.edit();
                edt.putString("apk path 1", filePath);
                edt.commit();

            } catch (Exception e) {
                return null;
            } finally {
                try {
                    if (output != null)
                        output.close();
                    if (input != null)
                        input.close();
                } catch (IOException ignored) {

                }

                if (connection != null)
                    connection.disconnect();
            }

            return filePath;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // take CPU lock to prevent CPU from going off if the user
            // presses the power button during download
            PowerManager pm = (PowerManager) context
                    .getSystemService(Context.POWER_SERVICE);
            mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                    getClass().getName());
            mWakeLock.acquire();

        }

        @Override
        protected void onPostExecute(String result) {
            mWakeLock.release();
            if (result == null) {
                nManager.cancel(Id);
            } else {
                Uri uri = Uri.fromFile(new File(result));
                try {
                    Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
                    intent.setDataAndType(uri,
                            "application/vnd.android.package-archive");
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intent);
                    nManager.cancel(Id);
                } catch (Exception e) {
                    nManager.cancel(Id);
                    Log.e("Installer ", e.toString());
                }
            }
        }
    }

0 个答案:

没有答案