La barra de progreso no se muestra en el teléfono después de hacer clic en la barra de estado de notificación

Cuando estoy descargando el archivo del servidor, se está descargando bien, en la barra de estado de notificación se muestra la barra de progreso, pero cuando selecciono de la barra de estado de notificación, abrirá esa aplicación, pero en ese momento la barra de progreso no se muestra en el teléfono, pero funciona bien en el emulador. Por favor, ayúdenme...

Gracias..

  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); 


    }
}

}

preguntado el 03 de mayo de 12 a las 09:05

0 Respuestas

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.