单击通知状态栏后,未在电话上显示进度条

时间:2012-05-03 08:14:28

标签: android download android-notifications android-progressbar

当我从服务器下载文件时,它下载得很好,在通知状态栏中显示进度条,但是当我从通知状态栏中选择时,它将打开该应用程序,但此时进度条未显示在电话,但它在模拟器上工作正常。请帮帮我......

谢谢..

  public class FileDownloadTestActivity extends Activity {

private static String url = "http://www.xxxxx.in/build/android/android/xyz.apk";
public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
private Button startBtn;
private ProgressDialog mProgressDialog;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    startBtn = (Button) findViewById(R.id.download);
    Boolean boolean1=true;
    if(boolean1){
    startBtn.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            new DownloadFileAsync().execute(url);
        }
    });
    boolean1=false;
    }
}

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DIALOG_DOWNLOAD_PROGRESS:
        mProgressDialog = new ProgressDialog(this);
        mProgressDialog.setMessage("Downloading file..");
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        mProgressDialog.setCancelable(false);
        mProgressDialog.show();
        return mProgressDialog;
    default:
        return null;
    }

}

class DownloadFileAsync extends AsyncTask<String, String, String> {


    NotificationManager notificationManager;
    Notification notification2;

    @Override
    protected void onPreExecute() { 
        super.onPreExecute();
         notification2 = new Notification(android.R.drawable.stat_sys_download, "downloading..." , System.currentTimeMillis());

         Intent intent = new Intent(getApplicationContext(),FileDownloadTestActivity.class);
           intent .setAction("android.intent.action.MAIN");
           intent .addCategory("android.intent.category.LAUNCHER");      
            final PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);

            notification2 = new Notification(android.R.drawable.stat_sys_download, "downloading..." , System.currentTimeMillis());
            notification2.flags = notification2.flags | Notification.FLAG_ONGOING_EVENT;
            notification2.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.custom_notification_layout);
            notification2.contentIntent = pendingIntent;
            notification2.contentView.setImageViewResource(R.id.status_icon,R.drawable.junow );
            notification2.contentView.setProgressBar(R.id.status_progress, 100, 1, false);
            getApplicationContext();
            notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
            showDialog(DIALOG_DOWNLOAD_PROGRESS);

    }

    @Override
    protected String doInBackground(String... aurl) {
        int count;

        try {

            URL url = new URL(aurl[0]);
            URLConnection conexion = url.openConnection();
            conexion.connect();

            int lenghtOfFile = conexion.getContentLength();

            InputStream input = new BufferedInputStream(url.openStream());
            OutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory()+"/xyz.apk");

            byte data[] = new byte[1024];
            long total = 0;

            while ((count = input.read(data)) != -1) {
                total += count;

                long var=(int) ((total * 100) / lenghtOfFile);
                publishProgress("" +var);
                output.write(data, 0, count);
                notificationManager.notify(1, notification2);   
            }
            output.flush();
            output.close();
            input.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    protected void onProgressUpdate(String... progress) {
         mProgressDialog.setProgress(Integer.parseInt(progress[0]));
         notification2.contentView.setProgressBar(R.id.status_progress, 100,Integer.parseInt(progress[0]), false);
         notification2.contentView.setTextViewText(R.id.percent,"Downloading in progress  "+progress[0]+" %");

    }

    @Override
    protected void onPostExecute(String unused) {
        dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
        notificationManager.cancel(1); 
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/xyz.apk")), "application/vnd.android.package-archive");
        startActivity(intent); 


    }
}

}

0 个答案:

没有答案