Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>After a long search i finished the task of showing progress bar in notification area.All we need is <code>asynctask</code>.May be the code iam showing here will not work perfectly,It works fine when iam testing.Please check and upvote for this answer if it works good.</p> <p>My code:</p> <pre><code>public class loadVideo extends AsyncTask&lt;Void, Integer, Void&gt; { int progress = 0; Notification notification; NotificationManager notificationManager; int id = 10; protected void onPreExecute() { } @Override protected Void doInBackground(Void... params) { HttpURLConnection conn = null; DataOutputStream dos = null; DataInputStream inStream = null; String lineEnd = "\r\n"; String twoHyphens = "--"; String boundary = "*****"; int bytesRead; int sentData = 0; byte[] buffer; String urlString = "http://xxxxx/xxx/xxxxxx.php"; try { UUID uniqueKey = UUID.randomUUID(); fname = uniqueKey.toString(); Log.e("UNIQUE NAME", fname); FileInputStream fileInputStream = new FileInputStream(new File( selectedPath)); int length = fileInputStream.available(); URL url = new URL(urlString); conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true); conn.setDoOutput(true); conn.setUseCaches(false); conn.setRequestMethod("POST"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); dos = new DataOutputStream(conn.getOutputStream()); dos.writeBytes(twoHyphens + boundary + lineEnd); dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + fname + "" + lineEnd); dos.writeBytes(lineEnd); buffer = new byte[8192]; bytesRead = 0; while ((bytesRead = fileInputStream.read(buffer)) &gt; 0) { dos.write(buffer, 0, bytesRead); sentData += bytesRead; int progress = (int) ((sentData / (float) length) * 100); publishProgress(progress); } dos.writeBytes(lineEnd); dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); Log.e("Debug", "File is written"); fileInputStream.close(); dos.flush(); dos.close(); } catch (MalformedURLException ex) { Log.e("Debug", "error: " + ex.getMessage(), ex); } catch (IOException ioe) { Log.e("Debug", "error: " + ioe.getMessage(), ioe); } // ------------------ read the SERVER RESPONSE try { inStream = new DataInputStream(conn.getInputStream()); String str; while ((str = inStream.readLine()) != null) { Log.e("Debug", "Server Response " + str); } inStream.close(); } catch (IOException ioex) { Log.e("Debug", "error: " + ioex.getMessage(), ioex); } return null; } @Override protected void onProgressUpdate(Integer... progress) { Intent intent = new Intent(); final PendingIntent pendingIntent = PendingIntent.getActivity( getApplicationContext(), 0, intent, 0); notification = new Notification(R.drawable.video_upload, "Uploading file", System.currentTimeMillis()); notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT; notification.contentView = new RemoteViews(getApplicationContext() .getPackageName(), R.layout.upload_progress_bar); notification.contentIntent = pendingIntent; notification.contentView.setImageViewResource(R.id.status_icon, R.drawable.video_upload); notification.contentView.setTextViewText(R.id.status_text, "Uploading..."); notification.contentView.setProgressBar(R.id.progressBar1, 100, progress[0], false); getApplicationContext(); notificationManager = (NotificationManager) getApplicationContext() .getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(id, notification); } protected void onPostExecute(Void result) { Notification notification = new Notification(); Intent intent1 = new Intent(MultiThreadActivity.this, MultiThreadActivity.class); final PendingIntent pendingIntent = PendingIntent.getActivity( getApplicationContext(), 0, intent1, 0); int icon = R.drawable.check_16; // icon from resources CharSequence tickerText = "Video Uploaded Successfully"; // ticker-text CharSequence contentTitle = getResources().getString( R.string.app_name); // expanded message // title CharSequence contentText = "Video Uploaded Successfully"; // expanded // message long when = System.currentTimeMillis(); // notification time Context context = getApplicationContext(); // application // Context notification = new Notification(icon, tickerText, when); notification.flags |= Notification.FLAG_AUTO_CANCEL; notification.setLatestEventInfo(context, contentTitle, contentText, pendingIntent); String notificationService = Context.NOTIFICATION_SERVICE; notificationManager = (NotificationManager) context .getSystemService(notificationService); notificationManager.notify(id, notification); } } </code></pre> <p>Thanks and Regards Sundar.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload