启动服务后,待服务执行完onHandleIntent 会自动退出
public class MyIntentService extends IntentService {
@Override
public void onDestroy() {
super.onDestroy();
Log.i("Service","MyIntentServiceDestroy");
}
public MyIntentService() {
super("MyIntentServiceName");
}
@Override
protected void onHandleIntent(Intent intent) {
try{
int result = DownloadFile(new URL("http://www.x-motion.com/somefile.pdf"));
Log.i("Service","Download "+result +" bytes");
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
private int DownloadFile(URL url){
try{
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return 100;
}
}