Why another service on top of IMarketBillingService?

Google market_billing sample , like others, like this one , connects to the remote IMarketBillingService service through the local BillingService wrapper.

I understand that services have the advantage of doing something in the background, but is IMarketBillingService not enough for this remote control?

What is the advantage of adding another layer to this bow?

What will I lose if I try to connect to a remote IMarketBillingService directly from my main action in the UI thread?

If it is not recommended to connect to the remote IMarketBillingService directly in the user interface thread, is it possible to replace the local BillingService with another thread in the main action?

+6
source share
1 answer

The local BillingService processes callbacks from IMarketBillingService when your activity is not running.

The link ( http://developer.android.com/reference/android/app/Activity.html ) says:

“If an action is paused or stopped, the system can refuse the action from memory, either asking it to finish, or simply killing it to process. When it is displayed to the user again, it must be completely restarted and restored to its previous state."

If you, for example, call a RESTORE_TRANSACTIONS billing request, responses from the Android Market service may take some time to arrive. Using the service, you know that you will always be responsible for the answers, regardless of the life cycle of the activity.

Just for fun, I tried to write a small test application and was sure. A running thread can invoke methods with paused or stopped activity. This thread can also change its UI, even if activity is not in the foreground. Launch the following application, press the main screen to stop the application. Go back after 10 seconds and you will see that the TextView has changed ...

 package com.example.playground; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.widget.TextView; public class MyActivity extends Activity { private static String TAG = MyActivity.class.getName(); /** * Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Thread t = new Thread(new Runnable() { public void run() { try { Thread.sleep(10000); someMethod(); } catch (InterruptedException e) { Log.e(TAG, e.getMessage(), e); } } }); t.start(); } private void someMethod() { Log.d(TAG, "Some method called"); TextView tv = (TextView) findViewById(R.id.textfield); tv.setText("Called later"); } } 
+1
source

Source: https://habr.com/ru/post/906035/


All Articles