android - 在线交易 : asynctask vs service

标签 android android-asynctask android-service

考虑到用户必须使用该应用执行在线交易(例如在线支付)的用例。

这应该在不阻塞 UI 的情况下完成,所以我打算使用 AsyncTask。问题在于以下情况: - 用户在交易过程中旋转手机或接到来电,从而导致 Activity 被破坏。

如果我理解正确的话,asynctask 现在引用了一个陈旧的对象。所以交易完成后,没有办法通知用户结果。是吗?

应该改用服务吗?

最佳答案

What is a Service?

Most confusion about the Service class actually revolves around what it is not:

  • A Service is not a separate process. The Service object itself does not imply it is running in its own process; unless otherwise specified, it runs in the same process as the application it is part of.
  • A Service is not a thread. It is not a means itself to do work off of the main thread (to avoid Application Not Responding errors).

Should you use a service or a thread?

A service is simply a component that can run in the background even when the user is not interacting with your application. Thus, you should create a service only if that is what you need.

If you need to perform work outside your main thread, but only while the user is interacting with your application, then you should probably instead create a new thread and not a service. For example, if you want to play some music, but only while your activity is running, you might create a thread in onCreate(), start running it in onStart(), then stop it in onStop(). Also consider using AsyncTask or HandlerThread, instead of the traditional Thread class. See the Processes and Threading document for more information about threads.

Remember that if you do use a service, it still runs in your application's main thread by default, so you should still create a new thread within the service if it performs intensive or blocking operations.

What causing the activity to be destryed when user rotates the phone?

If the configuration of the device (as defined by the Resources.Configuration class) changes, then anything displaying a user interface will need to update to match that configuration. Because Activity is the primary mechanism for interacting with the user, it includes special support for handling configuration changes.

Unless you specify otherwise, a configuration change (such as a change in screen orientation, language, input devices, etc) will cause your current activity to be destroyed, going through the normal activity lifecycle process of onPause(), onStop(), and onDestroy() as appropriate. If the activity had been in the foreground or visible to the user, once onDestroy() is called in that instance then a new instance of the activity will be created, with whatever savedInstanceState the previous instance had generated from onSaveInstanceState(Bundle).

Possible workaround to solve AsyncTask interruption due to Activity recreation:

In some special cases, you may want to bypass restarting of your activity based on one or more types of configuration changes. This is done with the android:configChanges attribute in its manifest. For any types of configuration changes you say that you handle there, you will receive a call to your current activity's onConfigurationChanged(Configuration) method instead of being restarted. If a configuration change involves any that you do not handle, however, the activity will still be restarted and onConfigurationChanged(Configuration) will not be called.

关于android - 在线交易 : asynctask vs service,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11255438/

相关文章:

android - Gradle 构建类型或风格哪个优先?

android - 从 Activity 启动动态壁纸服务

android - 在 Android 的锁定屏幕上显示警告对话框

android - 如何删除未使用的 GcmTaskService 服务?

android - facebook sdk登录nativecrypto错误

android - 无法创建 F# Android 项目 - Visual Studio 2015 Xamarin Community

android - 抱歉,无法播放此视频 - 将 mp4 流式传输到 android

java - 我想从 facebook api 请求多个页面名称和页面照片

android - ASynctask 的doInBackground() 中return 语句的作用是什么?

android - 如何使用AsyncTask显示ProgressBar和加载Rss