Create Notification
to download file
private void downloadFile(String url) { new AsyncTask<String, Integer, File>() { private Exception m_error = null; private static final int NOTIFY_ID = 1; NotificationManager myNotificationManager; Notification notification; Context context; @Override protected void onPreExecute() { myNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); int icon = android.R.drawable.stat_sys_download; CharSequence tickerText = ""; long when = System.currentTimeMillis(); context=getApplicationContext(); notification = new Notification(icon, tickerText, when); Intent notificationIntent = new Intent(context, WebBrawser.class); PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); RemoteViews contentView; contentView = new RemoteViews(getPackageName(), R.layout.notification); contentView.setProgressBar(R.id.progressBar1, 100, 0, false); contentView.setTextViewText(R.id.name,"FILENAME.mp3"); notification.contentIntent = contentIntent; notification.contentView = contentView; myNotificationManager.notify(NOTIFY_ID, notification); }
Update ProgressBar
protected void onProgressUpdate(final Integer... values) { new Thread(new Runnable() { public void run() { mHandler.post(new Runnable() { public void run() { int progress=(int) ((values[0] / (float) values[1]) * 100); Integer textProgress=new Integer(progress); notification.contentView.setProgressBar(R.id.progressBar1, 100, progress, false); notification.contentView.setTextViewText(R.id.progress, textProgress.toString()+"%"); myNotificationManager.notify(NOTIFY_ID, notification); } }); } }).start(); }
But at the beginning of the download, the application hangs, I update it in a separate thread. Downloading a file without Notification
is done correctly. Help me figure out what I did wrong.