Android-重试IntentService的最佳实践
我的Android应用程序向AmazonS3发送大量文件。每个文件URI通过单独的调用传递给执行上载的Android-重试IntentService的最佳实践,android,intentservice,Android,Intentservice,我的Android应用程序向AmazonS3发送大量文件。每个文件URI通过单独的调用传递给执行上载的IntentService 然而,我想知道什么是处理失败的最好方法。。。我应该使用我的IntentService的onHandleIntent()方法检测故障并在该方法内重试,还是应该允许在该方法之外处理故障(如果是,如何处理?) 我个人倾向于第一个建议,因为我希望在尝试上载后续文件之前成功上载任何文件,但我不确定在onHandleIntent()方法中检测错误和执行重试是否是良好的做法(?)。
IntentService
然而,我想知道什么是处理失败的最好方法。。。我应该使用我的IntentService
的onHandleIntent()
方法检测故障并在该方法内重试,还是应该允许在该方法之外处理故障(如果是,如何处理?)
我个人倾向于第一个建议,因为我希望在尝试上载后续文件之前成功上载任何文件,但我不确定在
onHandleIntent()
方法中检测错误和执行重试是否是良好的做法(?)。这是一个非常好的问题。我在一次采访中被问到这个问题,但我没有回答。但我会在这里尝试回答,在寻找答案之后
步骤1:您启动了一个IntentService。您可以从活动或片段启动IntentService
/* Starting Download Service */
DownloadResultReceiver mReceiver = new DownloadResultReceiver(new Handler());
mReceiver.setReceiver(this);
Intent intent = new Intent(Intent.ACTION_SYNC, null, this, DownloadService.class);
/* Send optional extras to Download IntentService */
intent.putExtra("url", url);
intent.putExtra("receiver", mReceiver);
intent.putExtra("requestId", 101);
startService(intent);
步骤2:创建扩展IntentService的类
public class DownloadService extends IntentService {
public static final int STATUS_RUNNING = 0;
public static final int STATUS_FINISHED = 1;
public static final int STATUS_ERROR = 2;
private static final String TAG = "DownloadService";
public DownloadService() {
super(DownloadService.class.getName());
}
@Override
protected void onHandleIntent(Intent intent) {
Log.d(TAG, "Service Started!");
final ResultReceiver receiver = intent.getParcelableExtra("receiver");
String url = intent.getStringExtra("url");
Bundle bundle = new Bundle();
if (!TextUtils.isEmpty(url)) {
/* Update UI: Download Service is Running */
receiver.send(STATUS_RUNNING, Bundle.EMPTY);
try {
String[] results = downloadData(url);//make your network call here and get the data or download a file.
/* Sending result back to activity */
if (null != results && results.length > 0) {
bundle.putStringArray("result", results);
receiver.send(STATUS_FINISHED, bundle);
}
} catch (Exception e) {
/* Sending error message back to activity */
bundle.putString(Intent.EXTRA_TEXT, e.toString());
receiver.send(STATUS_ERROR, bundle);
}
}
Log.d(TAG, "Service Stopping!");
this.stopSelf();
}
}
步骤3:要从IntentService接收结果,我们可以使用ResultReciever的子类。从服务发送结果后,将调用onReceiveResult()方法。您的活动将处理此响应并从包中获取结果。收到结果后,活动实例将相应地更新UI
public class DownloadResultReceiver extends ResultReceiver {
private Receiver mReceiver;
public DownloadResultReceiver(Handler handler) {
super(handler);
}
public void setReceiver(Receiver receiver) {
mReceiver = receiver;
}
public interface Receiver {
public void onReceiveResult(int resultCode, Bundle resultData);
}
@Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
if (mReceiver != null) {
mReceiver.onReceiveResult(resultCode, resultData);
}
}
}
第4步:在您的主要活动中:
@Override
public void onReceiveResult(int resultCode, Bundle resultData) {
switch (resultCode) {
case DownloadService.STATUS_RUNNING:
//progress bar visible.
break;
case DownloadService.STATUS_FINISHED:
/* Hide progress & extract result from bundle */
/* Update ListView with result */
break;
case DownloadService.STATUS_ERROR:
/* Handle the error */
String error = resultData.getString(Intent.EXTRA_TEXT);
Toast.makeText(this, error, Toast.LENGTH_LONG).show();
/*It is here, i think, that you can again check (eg your net connection) and call the IntentService to restart fetching of data from the network. */
break;
}
}
我希望上面的答案对你有帮助。任何改进答案的建议都是非常受欢迎的。谢谢 如果文件未能从处理程序上载,请使用处理程序跟踪每个文件的上载状态。您能给我一个这样的处理程序的示例吗?谢谢分享。你得到这份工作了吗?!:)哈哈..没人..不是在我被问这个问题的地方。但我在以后的面试中表现不错。