In my android app, using a progress notification, its working in all android versions except android O. In android O, file is getting downloaded but progress bar is not updating neither it shows "Download Complete". Below is my code which is under AsyncTask --
private class BackTask extends AsyncTask<String,Integer,Void> {
NotificationManager mNotifyManager;
NotificationCompat.Builder mBuilder;
NotificationChannel notificationChannel;
protected void onPreExecute() {
super.onPreExecute();
mNotifyManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
Uri selectedUri = Uri.parse(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString());
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(selectedUri, "resource/folder");
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
mBuilder = new NotificationCompat.Builder(context, null);
mBuilder.setContentTitle("Downloading - "+lblTitle.getText())
.setContentText("Download in progress")
.setSmallIcon(R.drawable.doupnowlogo)
.setOnlyAlertOnce(true)
.setContentIntent(pendingIntent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_HIGH);
// Configure the notification channel.
notificationChannel.setDescription("Channel description");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
mNotifyManager.createNotificationChannel(notificationChannel);
}
else {
mBuilder.setContentTitle("Downloading - "+lblTitle.getText())
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setColor(ContextCompat.getColor(context, R.color.red))
.setVibrate(new long[]{100, 250})
.setLights(Color.YELLOW, 500, 5000)
.setAutoCancel(true);
}
mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
mNotifyManager.notify(1, mBuilder.build());
Toast.makeText(getActivity().getApplicationContext(), "Downloading the file...", Toast.LENGTH_SHORT).show();
}
protected Void doInBackground(String...params){
URL url;
int count;
try {
url = new URL(params[0].replaceAll(" ", "%20"));
String pathl="";
try {
File f=new File(storeDir);
if(f.exists()){
HttpURLConnection con=(HttpURLConnection)url.openConnection();
InputStream is=con.getInputStream();
String pathr=url.getPath();
String filename=pathr.substring(pathr.lastIndexOf('/')+1);
pathl=storeDir+"/"+filename;
FileOutputStream fos=new FileOutputStream(pathl);
int lenghtOfFile = con.getContentLength();
byte data[] = new byte[1024];
long total = 0;
while ((count = is.read(data)) != -1) {
total += count;
// publishing the progress
publishProgress((int)((total*100)/lenghtOfFile));
// writing data to output file
fos.write(data, 0, count);
}
is.close();
fos.flush();
fos.close();
}
else{
Log.e("Error","Not found: "+storeDir);
}
} catch (Exception e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
protected void onProgressUpdate(Integer... progress) {
mBuilder.setProgress(100, progress[0], false);
// Displays the progress bar on notification
mNotifyManager.notify(NOTIFICATION_ID, mBuilder.build());
}
protected void onPostExecute(Void result){
mBuilder.setContentText("Download complete");
// Removes the progress bar
mBuilder.setProgress(0,0,false);
mNotifyManager.notify(NOTIFICATION_ID, mBuilder.build());
}
}
The full program is working great in other versions, but getting the problem only in android o.