0

進行状況ダイアログを開いてファイルをダウンロードするスレッドがあります。スレッドがファイルをダウンロードしている間、進行状況バーが更新されます。ただし、進行状況ダイアログが非表示の場合、スレッドは通知を作成し、通知内の進行状況バーを更新します。私はこれを作りたい: ユーザーが通知をクリックすると、Android はアクティビティを開き、進行状況ダイアログを表示します。これどうやってするの?

それがファイルをダウンロードする私の方法です:

    public void downloadAction(int id) {
    if(id<0 || id>data.length) { IO.showNotify(MusicActivity.this, getResources().getStringArray(R.array.errors)[4]); return; }
    final int itemId = id;

    AsyncTask<Void, Integer, Boolean> downloadTask = new AsyncTask<Void, Integer, Boolean>() {
        ProgressDialog progressDialog = new ProgressDialog(MusicActivity.this);
        String error = null;
        int nId = -1;
        int progressPercent = 0;
        boolean notificated = false;
        int urlFileLength;
        String FILE_NAME = fileName(data.getName(itemId));

        @Override
        protected void onPreExecute() {
            progressDialog.setTitle(data.getName(itemId));
            progressDialog.setIndeterminate(false);
            progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            progressDialog.setCancelable(true);
            progressDialog.setOnCancelListener(new OnCancelListener() {
                public void onCancel(DialogInterface dialog) {
                    cancel(true);
                }
            });

            progressDialog.setButton(Dialog.BUTTON_POSITIVE, getResources().getString(R.string.hide), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    progressDialog.hide();
                }
            });
            progressDialog.setButton(Dialog.BUTTON_NEGATIVE, getResources().getString(R.string.cancel), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    if(progressDialog.isShowing()) { cancel(true); progressDialog.dismiss();}
                }
            });

            progressDialog.show();
            progressDialog.setProgressNumberFormat("");                             
        }

        @Override
        protected Boolean doInBackground(Void... params) {
            int localFileLength, len;
            int fullProgress = 0;
            byte[] bytes = new byte[1024];
            File rootDir = new File(PATH);

            if(!rootDir.isDirectory()) rootDir.mkdir();

            try {
                URLConnection urlConnection = new URL(data.getUrl(itemId)).openConnection();
                urlConnection.setConnectTimeout(20000);
                urlConnection.setReadTimeout(60000);

                localFileLength = (int) new File(FILE_NAME).length();
                urlFileLength = urlConnection.getContentLength();

                if (urlFileLength == 169 || urlFileLength == 0 || urlFileLength == -1) {
                    error = getResources().getStringArray(R.array.errors)[5];
                    return false;
                }
                if (urlFileLength == localFileLength) {
                    error = getResources().getString(R.string.file_exist);
                    return false;
                } else {
                    publishProgress(0, urlFileLength);
                    InputStream in = urlConnection.getInputStream();
                    OutputStream out = new FileOutputStream(FILE_NAME);

                    while((len=in.read(bytes))!=-1) {
                        if(!isCancelled()) {
                            out.write(bytes, 0, len);
                            fullProgress += len;
                            publishProgress(fullProgress);
                        } else {
                            new File(FILE_NAME).delete();
                            error = getResources().getString(R.string.stopped);
                            return false;                               
                        }
                    }

                }

            } catch (MalformedURLException e) {
                new File(FILE_NAME).delete();
                error = getResources().getStringArray(R.array.errors)[2];
            } catch (IOException e) {
                new File(FILE_NAME).delete();
                error = getResources().getStringArray(R.array.errors)[3];
            }



            return true;
        }

        @Override
        protected void onProgressUpdate(Integer ... progress) {
            int tmp;

            if (progress.length==2) {
                progressDialog.setProgress(progress[0]);
                progressDialog.setMax(progress[1]);
            } else if(progress.length==1) {
                if(!progressDialog.isShowing()) {
                    if(!notificated) {
                        nId = NotificationUtils.getInstace(MusicActivity.this).createDownloadNotification(data.getName(itemId));
                        notificated = true;
                    } else {
                        tmp = (int) (progress[0]/(urlFileLength*0.01));
                        if(progressPercent!=tmp) {
                            progressPercent = tmp;
                            NotificationUtils.getInstace(MusicActivity.this).updateProgress(nId, progressPercent);
                        }
                    }
                } else {
                    progressDialog.setProgress(progress[0]);
                }
            }
        }

        @Override
        protected void onPostExecute(Boolean result) {
            if(result==true && error == null) {
                if(progressDialog.isShowing()) {
                    IO.showNotify(MusicActivity.this, getResources().getString(R.string.downloaded) + " " + PATH);
                    progressDialog.dismiss();
                } else if (nId!=-1) {
                    NotificationUtils.getInstace(MusicActivity.this).cancelNotification(nId);
                    NotificationUtils.getInstace(MusicActivity.this).createMessageNotification(data.getName(itemId) + " " + getResources().getString(R.string.finished));
                }
            } else {
                if(progressDialog.isShowing()) {
                    IO.showNotify(MusicActivity.this, error);
                    progressDialog.dismiss();
                } else if (nId!=-1){
                    NotificationUtils.getInstace(MusicActivity.this).cancelNotification(nId);
                    NotificationUtils.getInstace(MusicActivity.this).createMessageNotification(getResources().getString(R.string.error) + "! " + error);
                }
            }

        }

        @Override
        protected void onCancelled() {
            IO.showNotify(MusicActivity.this, getResources().getString(R.string.stopped));
        }

    };

    if(downloadTask.getStatus().equals(AsyncTask.Status.PENDING) || downloadTask.getStatus().equals(AsyncTask.Status.FINISHED))
        downloadTask.execute();
}

そして、それは通知を作成する2つの方法です:

public int createDownloadNotification(String fileName) {
    String text = context.getString(R.string.notification_downloading).concat(" ").concat(fileName); 
    RemoteViews contentView = createProgressNotification(text, text);
    contentView.setImageViewResource(R.id.notification_download_layout_image, android.R.drawable.stat_sys_download);

    return lastId++;
}


private RemoteViews createProgressNotification(String text, String topMessage) {
    Notification notification = new Notification(android.R.drawable.stat_sys_download, topMessage, System.currentTimeMillis());
    RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.notification_download_layout);
    contentView.setProgressBar(R.id.notification_download_layout_progressbar, 100, 0, false);
    contentView.setTextViewText(R.id.notification_download_layout_title, text);

    notification.contentView = contentView;
    notification.flags = Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT | Notification.FLAG_ONLY_ALERT_ONCE;

    Intent notificationIntent = new Intent(context, NotificationUtils.class);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

    notification.contentIntent = contentIntent;

    manager.notify(lastId, notification);
    notifications.put(lastId, notification);

    return contentView;
}

お願い助けて...

4

1 に答える 1

0

よくわかりませんが、私の通知コードにはありません。行notification.contentIntent = contentIntent;の前にこれが表示されていないようです 。manager.notify(lastId, notification);

notification.setLatestEventInfo(context, MyNotifyTitle, MyNotifiyText, contentIntent );

MyNotifyTitleは通知のタイトル、MyNotifyTextはテキストです。contentIntentの前にそれらを追加します

MyIntent.putExtra("extendedTitle", notificationIntent );
MyIntent.putExtra("extendedText" , notificationIntent );

お役に立てれば。

于 2012-05-19T06:58:30.693 に答える