在我的Android应用程序中,使用进度通知,它在除了android O之外的所有Android版本中工作。在android O中,文件正在下载但是进度条没有更新它既不显示"下载完成"。下面是我在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());
}
}
完整的程序在其他版本中运行良好,但只在android o。
中解决问题答案 0 :(得分:0)
它可能对您有帮助,我正在当前正在进行的项目中显示通知。它显示从4.1到最新8.0奥利奥的通知。在每个平台上测试。
以下是我的班级私人会员:
private static final int NOTIFICATION_ID = 1;
private static final String NOTIFICATION_CHANNEL_ID = "1";
在同一个班级,我正在使用以下代码创建和显示通知
try
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
startForeground(1,new Notification());
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_DEFAULT);
// Configure the notification channel.
notificationChannel.setDescription("Channel description");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationManager.createNotificationChannel(notificationChannel);
// notification.defaults = Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE;
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_service_success)
.setContentTitle("Insta Promo")
.setContentText("We are ready to help you.");
notificationManager.notify(NOTIFICATION_ID, builder.build());
}
else
{
final Intent intent = new Intent(this, WatchMan.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder b = new NotificationCompat.Builder(this,"mychannel");
b.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_service_success)
.setTicker("Success")
.setContentTitle("Insta Promo")
.setContentText("We are ready to help you.")
.setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_SOUND).setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_SOUND)
.setContentIntent(contentIntent)
.setContentInfo("Info");
NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, b.build());
}
}
catch(Exception e)
{
Log.d(TAG, "Null pointer exception xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...\n");
System.out.println("Null Pointer ");
}
不要使用startForeground(1,new Notification());
,因为我在服务中并希望继续onstart命令才能开始使用。
您可能会在android 8.0 oreo上看到开发者警告TOAST消息,如果您或任何其他成员也可以解决此问题,那么对我们双方来说都会很棒。
答案 1 :(得分:0)
问题所在:
mBuilder = new NotificationCompat.Builder(context, null);
您应该在构建器构造函数中使用channel_id(您已经在下面将其添加为notificationChannel
),而且我也看到了您创建的波纹管,如下所示:
notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_HIGH);
我认为最好将NotificationManager.IMPORTANCE_LOW
用于进度通知,因为NotificationManager.IMPORTANCE_HIGH
会弹出显示。
因此,我认为在这里使用两个渠道的最佳方法。第一个用于启动/完成/错误通知,第二个仅用于进度。并且第一个频道将使用NotificationManager.IMPORTANCE_DEFAULT
或NotificationManager.IMPORTANCE_HIGH
优先级,但会使用频道-NotificationManager.IMPORTANCE_LOW
。在此实现中,我们可以在系统托盘中看到带有声音振动等的开始通知和没有声音的进度通知。