在线交易:AsyncTask的VS服务 [英] Online Transaction: asynctask vs service

查看:128
本文介绍了在线交易:AsyncTask的VS服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于使用的情况下,用户必须执行在线交易,例如网上支付,使用的应用程序。

这应该不会阻塞UI做的,所以我打算用AsyncTask的。问题是与以下情形:
- 用户旋转电话或在transacion中间获取来电,从而使活动待destryed。

如果我理解正确的AsyncTask现在有一个陈旧的对象的引用。因此事务完成后,没有办法来通知用户有关的结果。是吗?

应该是使用服务呢?


解决方案

是什么服务?


  

有关服务类大多数的混淆实际上都是围绕着它不是什么:


  
  

      
  • 系统服务不是一个单独的进程。服务对象本身并不意味着它是在它自己的进程运行;除非另外指明,它运行在相同的过程,因为它是一部分的应用。

  •   
  • 系统服务不是一个线程。它不是指自己做了主线程的工作(以避免应用程序不响应错误)。

  •   

你应该使用服务或线程?


  

一个服务仅仅是可以在后台,即使用户不与应用程序交互运行的组件。因此,你应该创建一个服务只如果那是你所需要的。


  
  

如果您需要在主线程之外执行的工作,但只有在用户与应用程序交互,那么你或许应该,而不是创建一个新的线程,而不是服务。例如,如果你要玩一些音乐,但你的活动正在运行仅在,你也许可以在一个的onCreate线程(),开始在onStart()运行它,然后停止它的onStop()。也可以考虑使用,而不是传统的Thread类的AsyncTask或HandlerThread。查看进程和线程文件关于线程的详细信息。


  
  

请记住,如果你使用一个服务,它仍然在默认情况下,您的应用程序的主线程中运行,所以你还是应该在服务中创建一个新的线程,如果它执行密集或阻塞操作。


什么导致活动时,用户旋转手机被destryed <? / A>


  

如果该装置的结构(如由Resources.Configuration类中定义)的变化,那么任何显示用户界面将需要更新以匹配的配置。由于活动是与用户交互的主要机制,它包括用于处理配置更改的特殊支持。


  
  

除非你指定,配置变更(如屏幕方向,语言,输入设备等的变化)将导致破坏你的当前活动,通过在onPause()的onStop(正常活动的生命周期过程中去),和的onDestroy(),为适当。如果活动已经在前台或对用户可见,一旦的onDestroy()在该实例称为那么活动的新实例将被创建,与任何savedInstanceState的previous实例已经从的onSaveInstanceState(包)中产生


可能的解决方法来解决的AsyncTask中断,由于活动娱乐:


  

在某些特殊情况下,你可能想绕过基于一个或多个类型的配置更改重新启动您的活动。这是与Android进行:configChanges在它的属性。对于你说你处理没有任何类型的配置更改,您将收到一个电话到你当前活动的onConfigurationChanged(配置)方法,而不是被重新启动。如果配置变化涉及任何你不处理,但是,该活动仍然会重新启动并onConfigurationChanged(配置)将不会被调用。


Given the use case where the user has to perform an online transaction, eg online payment, using the app.

This should be done without blocking the UI, so i was going to use AsyncTask. The problem is with the following scenario: - the user rotates the phone or gets an incoming call in the middle of the transacion, thus causing the activity to be destryed.

If I understood correctly the asynctask now has a reference to a stale object. So after the transaction is done, there is no way to inform the user about the result. Is it?

Should be use a service instead?

解决方案

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.

这篇关于在线交易:AsyncTask的VS服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆