我有一个MainActivity和一个DownloadService。在MainActivity被破坏之后,DownloadService似乎被冻结了。我没有看到任何进展。当我去设置>管理应用程序>运行服务,我不再在运行列表中看到我的服务,但是,我也没有看到来自logcat的[onDestroy]。应该是什么问题?
public class DownloadService extends Service {
public static final String ACTION = "action";
public static final int ACTION_DOWNLOAD = 0;
private NotificationManager notificationManager;
private Notification notification;
private final int NOTIFICATION_ID = 1;
@Override
public void onCreate() {
super.onCreate();
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notification = new Notification();
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
int action = intent.getIntExtra(ACTION, ACTION_DOWNLOAD);
switch (action) {
case ACTION_DOWNLOAD:
startDownload(intent);
break;
}
return START_NOT_STICKY;
}
@Override
public void onLowMemory() {
super.onLowMemory();
Log.i("leapkh", "[onLowMemory]");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i("leapkh", "[onDestroy]");
}
private void startDownload(Intent intent) {
String fileUrl = intent.getIntExtra(FILE_URL, "");
FileDownloadAsyncTask task = new FileDownloadAsyncTask(fileUrl);
task.execute();
}
private class FileDownloadAsyncTask extends AsyncTask<Void, Integer, Boolean> {
private String fileUrl;
public FileDownloadAsyncTask(String fileUrl) {
this.fileUrl = fileUrl;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
startNotification(station);
}
@Override
protected Boolean doInBackground(Void... params) {
try {
URL url = new URL(fileUrl);
URLConnection conection = url.openConnection();
conection.connect();
int fileLength = conection.getContentLength();
InputStream input = new BufferedInputStream(url.openStream(), 8192);
String fileName = FileManager.getDownloadFileName(fileUrl);
OutputStream output = new FileOutputStream(fileName);
byte data[] = new byte[1024];
long total = 0;
int count, tmpPercentage = 0;
while ((count = input.read(data)) != -1) {
total += count;
output.write(data, 0, count);
int percentage = (int) ((total * 100) / fileLength);
if (percentage > tmpPercentage) {
publishProgress(percentage);
tmpPercentage = percentage;
}
}
output.flush();
output.close();
input.close();
return true;
}
catch (Exception e) {
Log.i("leapkh", "[Download Error]: " + e.getMessage());
return false;
}
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
int percentage = values[0];
updateNotification(station, percentage);
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
Log.i("leapkh", "Download finished: " + result);
completeNotification(station, result);
stopSelf();
}
private void startNotification(Station station) {
int icon = android.R.drawable.stat_sys_download;
CharSequence tickerText = "Start downloading...";
long when = System.currentTimeMillis();
notification = new Notification(icon, tickerText, when);
String contentTitle = "Downloading from " + fileUrl;
CharSequence contentText = "0%";
Intent notificationIntent = new Intent();
PendingIntent contentIntent = PendingIntent.getActivity(DownloadService.this, 0, notificationIntent, 0);
notification.setLatestEventInfo(DownloadService.this, contentTitle, contentText, contentIntent);
notification.flags = Notification.FLAG_ONGOING_EVENT;
notificationManager.notify(NOTIFICATION_ID, notification);
}
private void updateNotification(Station station, int percentage) {
String contentTitle = "Downloading from " + fileUrl;
CharSequence contentText = percentage + "%";
Intent notificationIntent = new Intent();
PendingIntent contentIntent = PendingIntent.getActivity(DownloadService.this, 0, notificationIntent, 0);
notification.setLatestEventInfo(DownloadService.this, contentTitle, contentText, contentIntent);
notificationManager.notify(NOTIFICATION_ID, notification);
}
private void completeNotification(Station station, boolean success) {
String contentTitle = "Downloaded from " + fileUrl;
Intent notificationIntent = new Intent();
PendingIntent contentIntent = PendingIntent.getActivity(DownloadService.this, 0, notificationIntent, 0);
String statusText = success ? "Completed" : "Fail";
notification.icon = success ? android.R.drawable.stat_sys_download_done : android.R.drawable.stat_notify_error;
notification.flags = Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(DownloadService.this, contentTitle, statusText, contentIntent);
notificationManager.notify(NOTIFICATION_ID, notification);
}
}
}