Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why another service on top of IMarketBillingService?

Google's market_billing sample, just like others as this one, connects to the remote service IMarketBillingService through a local service wrapper, BillingService.

I understand that services have the advantage of doing things in the background, but isn't the remote IMarketBillingService enough?

What is the advantage of adding yet another layer to this onion?

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

If it isn't advisable to connect to the remote IMarketBillingService directly in the UI thread, can the local BillingService be replaced with just another thread in the main activity?

like image 217
Bill The Ape Avatar asked Jan 15 '12 16:01

Bill The Ape


1 Answers

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

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

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

If for example you invoke a RESTORE_TRANSACTIONS billing request, the responses from the Android Market Service may take some time to Arrive. By using a service, you know that you'll always be around to handle responses, regardless of Activity Lifecycle.

Just for fun, I tried to write a small test-app and was sureprised. A running thread can call methods on a paused- or stopped activity. The thread can also modify it's UI even when the activity is not in the foreground. Run the following application, press the home screen to stop the app. Go back after 10 seconds, and 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");
    }
}
like image 170
Glenn Bech Avatar answered Nov 04 '22 12:11

Glenn Bech