Ich hatte Download-Task mit Intent-Service erstellt. das zeigt Benachrichtigung mit Fortschrittsbalken mit Prozent. Ich verwende lokalen Broadcast-Manager, um Daten während des Downloads zu übergeben.Ich füge auch eine Schaltfläche, um den Download in der Benachrichtigung abzubrechen, aber das Problem ist, während ich auf Abbrechen klicken Download es nicht Stop Intent-Service. Wie kann ich den beabsichtigten Service stoppen? Hier stelle ich meinen Code of Intent Service und auch den Broadcast Receiver in den Dienst.Intent-Service nicht explizit gestoppt
public class DownloadService extends IntentService {
public DownloadService() {
super(DownloadService.class.getName());
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
InputStream input;
OutputStream output;
HttpURLConnection connection;
Intent intent1 = new Intent();
intent1.setAction("com.demo.downloading");
try {
URL url = new URL(urlToDownload);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
return;
}
final int fileLength = connection.getContentLength();
input = connection.getInputStream();
output = new FileOutputStream(file);
byte data[] = new byte[8192];
long total = 0;
int count, latestPercentDone;
int percentDone = -1;
while ((count = input.read(data)) != -1) {
total += count;
latestPercentDone = (int) (total * 100/fileLength);
if (percentDone != latestPercentDone) {
percentDone = latestPercentDone;
if (percentDone < 100) {
if (percentDone != 0) {
intent1.putExtra("progress", "" + percentDone);
intent1.putExtra("IsCancel", false);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent1);
}
}
if (percentDone == 100) {
intent1.putExtra("progress", "" + 0);
intent1.putExtra("IsCancel", false);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent1);
}
}
output.write(data, 0, count);
if (StaticFields.cancelDownload) {
Log.d(TAG, "onHandleIntent: Download Cancel");
this.stopSelf();
}
}
output.close();
} catch (IOException e) {
intent1.putExtra("progress", "" + (-1));
intent1.putExtra("IsCancel", true);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent1);
e.printStackTrace();
is_all_download = false;
}
}
}
Meine Rundfunkempfänger Klasse OnReceive Methode()
if (intent.getAction() != null) {
if (action.equals("notification_cancelled")) {
Global.cancelDownload = true;
Intent intent1 = new Intent();
intent1.setAction("com.demo.downloading");
intent1.putExtra("IsCancel", true);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent1);
}
}
}
LocalBroadcast Manager
boolean isCancel = intent.getBooleanExtra("IsCancel", false);
if (isCancel) {
Global.cancelDownload = true;
mContext.stopService(serviceIntent);
}
eigentlich habe ich keine Ahnung auf Download-Manager und auch ich möchte den Fortschritt in der Benachrichtigung mit Abbrechen Download-Button so ... –