意图服务在完成任务之前停止?
问题描述:
我有一个IntentService
我在另一个线程做一些任务意图服务在完成任务之前停止?
onHandleIntent(Intent intent)
为什么是执行操作(任务)前的IntentService
停止?
这是我的代码:
public class SampleIntentService extends IntentService {
public static final int DOWNLOAD_ERROR = 10;
public static final int DOWNLOAD_SUCCESS = 11;
public SampleIntentService() {
super(SampleIntentService.class.getName());
}
@Override
protected void onHandleIntent(Intent intent) {
final String url = intent.getStringExtra("url");
final ResultReceiver receiver = intent.getParcelableExtra("receiver");
final Bundle bundle = new Bundle();
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
return downloadFile(url, receiver);
}
@Override
protected void onPostExecute(String filePath) {
super.onPostExecute(filePath);
bundle.putString("filePath", filePath);
receiver.send(DOWNLOAD_SUCCESS, bundle);
}
};
}
private String downloadFile(String url, ResultReceiver receiver) {
File downloadFile = new File(Environment.getExternalStorageDirectory() + File.pathSeparator + "test.png");
if (downloadFile.exists())
downloadFile.delete();
try {
downloadFile.createNewFile();
URL downloadURL = new URL(url);
HttpURLConnection conn = (HttpURLConnection) downloadURL
.openConnection();
int responseCode = conn.getResponseCode();
if (responseCode != 200)
throw new Exception("Error in connection");
InputStream is = conn.getInputStream();
FileOutputStream os = new FileOutputStream(downloadFile);
byte buffer[] = new byte[1024];
int byteCount;
while ((byteCount = is.read(buffer)) != -1) {
os.write(buffer, 0, byteCount);
}
os.close();
is.close();
String filePath = downloadFile.getPath();
return filePath;
} catch (Exception e) {
receiver.send(DOWNLOAD_ERROR, Bundle.EMPTY);
e.printStackTrace();
}
return null;
}
}
答
作为每文档:
abstract void onHandleIntent(Intent intent)
此方法工作线程上调用与处理请求。
由于此方法已在工作线程上调用,因此不需要启动另一个线程。
如果你这样做,onHandleIntent(Intent intent)
将返回并IntentService
将认为任务已完成,它会自行停止。
下面是更新后的代码:
public class SampleIntentService extends IntentService {
public static final int DOWNLOAD_ERROR = 10;
public static final int DOWNLOAD_SUCCESS = 11;
public SampleIntentService() {
super(SampleIntentService.class.getName());
}
@Override
protected void onHandleIntent(Intent intent) {
final String url = intent.getStringExtra("url");
final ResultReceiver receiver = intent.getParcelableExtra("receiver");
final Bundle bundle = new Bundle();
String filePath = downloadFile(url, receiver);
bundle.putString("filePath", filePath);
receiver.send(DOWNLOAD_SUCCESS, bundle);
}
private String downloadFile(String url, ResultReceiver receiver) {
File downloadFile = new File(Environment.getExternalStorageDirectory() + File.pathSeparator + "test.png");
if (downloadFile.exists())
downloadFile.delete();
try {
downloadFile.createNewFile();
URL downloadURL = new URL(url);
HttpURLConnection conn = (HttpURLConnection) downloadURL
.openConnection();
int responseCode = conn.getResponseCode();
if (responseCode != 200)
throw new Exception("Error in connection");
InputStream is = conn.getInputStream();
FileOutputStream os = new FileOutputStream(downloadFile);
byte buffer[] = new byte[1024];
int byteCount;
while ((byteCount = is.read(buffer)) != -1) {
os.write(buffer, 0, byteCount);
}
os.close();
is.close();
String filePath = downloadFile.getPath();
return filePath;
} catch (Exception e) {
receiver.send(DOWNLOAD_ERROR, Bundle.EMPTY);
e.printStackTrace();
}
return null;
}
嘿,这是unreleated这个问题。但是,你是如何解决你的主要活动泄漏问题作为GC根源的hj.o?我有同样的问题。感谢:) 我在搜索我的问题时遇到了您的问题从谷歌缓存。 – q126y