0
私のアプリケーションでは、インテントサービスを使用してファイルをダウンロードしています。ダウンロードが開始されると、ダウンロード進捗通知も作成されます。すべてが正常に動作しますが、問題は、通知が電話を遅くすることである:以下は、私のコードです: 注:私はシンプルかつ明確にするために、コードの非関連部分を削除した:通知が遅くなります
public void startDownload(String name, String packageName, String path, boolean obb, boolean data) {
try {
notificationId = generateNoficationId(packageName);
Intent i = new Intent(getApplicationContext(), DownloadListView.class);
PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0, i, 0);
mBuilder = new NotificationCompat.Builder(this);
mBuilder.setContentTitle(name)
.setContentText("downloading...")
.setSmallIcon(R.drawable.newiconpurple)
.setContentInfo("0%")
.setProgress(100, 0, true)
.setContentIntent(pi)
.setOngoing(true)
.setAutoCancel(true);
nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notification = mBuilder.build();
startForeground(notificationId, notification);
url = new URL(IPClass.SERVERIP + path + "/" + packageName);
connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setReadTimeout(7000);
long fileLength = connection.getContentLength();
connection.connect();
input = connection.getInputStream();
output = new FileOutputStream("/sdcard/downloadtmp/" + packageName, true);
byte dataSize[] = new byte[16384];
int count;
downloadedSoFar = 0;
continueDownload = true;
while ((count = input.read(dataSize)) > 0 && continueDownload) {
if (!downloadCancelList.contains(packageName)) {
downloadedSoFar = downloadedSoFar + count;
output.write(dataSize, 0, count);
progress = (int) ((downloadedSoFar * 100L)/fileLength);
progressChange(name, progress, packageName, obb, data);
} else {
continueDownload = false;
int startId = downloadIdList.get(packageName);
stopSelf(startId);
nm.cancel(notificationId);
downloadCancelList.remove(packageName);
stopForeground(true);
}
Thread.sleep(250);
}
}
} catch (Exception ex) {
nm.cancel(notificationId);
} finally {
try {
if (output != null) output.close();
if (input != null) input.close();
} catch (IOException ignored) {
}
if (connection != null) {
connection.disconnect();
} else if (resumeConnection != null) {
resumeConnection.disconnect();
}
}
}
そして次progressChangeメソッドです:
void progressChange(final String name, final int progress, final String packageName, boolean obb, boolean data) {
if (lastupdate != progress) {
lastupdate = progress;
if (progress < 100) {
mBuilder.setProgress(100, Integer.valueOf(progress), false).setContentInfo(progress + "%");
nm.notify(notificationId, mBuilder.build());
} else if (progress == 100) {
stopForeground(true);
mBuilder.setContentText("download finished").setProgress(0, 0, false).setOngoing(false).setContentInfo("");
nm.notify(notificationId, mBuilder.build());
new DownloadController().removeFromDownloadList(packageName);
new DownloadFinished(name, packageName, obb, data).execute();
}
}
}
私を助けてください!
返信ありがとうございますが、より具体的にすることができます – Shoaib
たとえば、進行状況が完全に10%進歩するたびに通知が送信されます。 – ShibbyUK
通知の更新回数をカウントします。私は約100の更新があると思います。だから、通知に表示されているものは、膨らませなければなりません。だから、イメージを100回何回か作成すると、50回後には本当に間違ってしまうでしょう。テストするには、簡単なアクティビティを開いて画面を回転させ、メモリプロファイラを見るだけです。値はGC(漏れがない場合)まで増加します。 したがって、進捗状況の変更頻度は少なくなります。すべての10%または何のように。 – Ekalips