为什么要在 IMarketBillingService 之上提供另一个服务?

发布于 2024-12-27 06:20:08 字数 678 浏览 0 评论 0原文

Google 的 market_billing 示例,就像其他这个,连接通过本地服务包装器BillingService远程服务IMarketBillingService

我知道服务具有在后台执行操作的优点,但是远程 IMarketBillingService 还不够吗?

在洋葱上再加一层有什么好处?

如果我尝试在 UI 线程中直接从主活动连接到远程 IMarketBillingService,我会失去什么?

如果不建议直接在 UI 线程中连接到远程 IMarketBillingService,是否可以将本地 BillingService 替换为主活动中的另一个线程?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十年不长 2025-01-03 06:20:08

当您的活动未运行时,本地 BillingService 会处理来自 IMarketBillingService 的回调。

参考( http://developer.android.com/reference/android/app/ Activity.html)说:

“如果活动暂停或停止,系统可以删除该活动
从记忆中要求它完成,或者简单地杀死它
过程。当再次显示给用户时,它必须是完整的
重新启动并恢复到之前的状态。”

例如,如果您调用 RESTORE_TRANSACTIONS 计费请求,来自 Android Market 服务的响应可能需要一些时间才能到达。通过使用服务,您知道您将始终在附近处理响应,无论 Activity 生命周期如何

,我尝试编写一个小型测试应用程序,并且确信正在运行的线程可以调用暂停或停止的 Activity 上的方法,即使 Activity 处于运行状态。不在前台运行以下应用程序,按主屏幕停止应用程序 10 秒后返回,看到 TextView 已更改...

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");
    }
}

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");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文