0

我正在尝试通过将付费版本作为简单的许可服务器来管理免费/付费应用程序。

付费应用有一个接收器:

public class LicenseRequest extends BroadcastReceiver
{
    private static final String TAG = LicenseRequest.class.getSimpleName();

    @Override
    public void onReceive(Context context, Intent intent)
    {
        if (!intent.getAction().equals(App.LICENSE_REQUEST))
        {
            return;
        }
        Intent licenseRequest = new Intent(context, LicenseService.class);
        context.startService(licenseRequest);
    }
}

调用 IntentService:

public class LicenseService extends IntentService
{
    private static final String TAG = LicenseService.class.getSimpleName();

    private LicenseCheckerCallback mLicenseCheckerCallback;
    private LicenseChecker mChecker;

    ...

    public LicenseService()
    {
        super(TAG);
    }

    @Override
    protected void onHandleIntent(Intent intent)
    {
        // Try to use more data here. ANDROID_ID is a single point of attack.
        String deviceId = ...;

        // Library calls this when it's done.
        mLicenseCheckerCallback = new MyLicenseCheckerCallback();
        // Construct the LicenseChecker with a policy.
        mChecker = new LicenseChecker(this, new ServerManagedPolicy(this, new AESObfuscator(SALT, getPackageName(), deviceId)),
                BASE64_PUBLIC_KEY);
        mChecker.checkAccess(mLicenseCheckerCallback);
    }

    private class MyLicenseCheckerCallback implements LicenseCheckerCallback
    {   
        public void allow(int policyReason)
        {
            Log.i(TAG, "License Accepted");
            Intent i = new Intent();
            i.setAction(App.LICENSE_RECEIVER);
            i.putExtra(App.LICENSE_RESULT, App.LICENSE_ALLOW);
            sendBroadcast(i);
//          mChecker.onDestroy();
        }

        public void dontAllow(int policyReason)
        {
            Log.e(TAG, "License Denied");
            Intent i = new Intent();
            i.setAction(App.LICENSE_RECEIVER);
            i.putExtra(App.LICENSE_RESULT, App.LICENSE_DISALLOW);
            sendBroadcast(i);
//          mChecker.onDestroy();
        }

        public void applicationError(int errorCode)
        {
            Log.i(TAG, "LR Error");
            Intent i = new Intent();
            i.setAction(App.LICENSE_RECEIVER);
            i.putExtra(App.LICENSE_RESULT, App.LICENSE_ERROR);
            sendBroadcast(i);
//          mChecker.onDestroy();
        }
    }

//    @Override
//    public void onDestroy() {
//        super.onDestroy();
//        mChecker.onDestroy();
//    }
}

我尝试过两种方法来处理 onDestroy()。如果我在 LicenseService.onDestroy() 中调用它,我会得到:

04-11 15:35:13.604: W/MessageQueue(30689): Handler (android.os.Handler) {41388638} sending message to a Handler on a dead thread
04-11 15:35:13.604: W/MessageQueue(30689): java.lang.RuntimeException: Handler (android.os.Handler) {41388638} sending message to a Handler on a dead thread

这是由于 IntentService 的生命周期在我相信的回调之前结束。

如果我在回调中调用它:

04-11 15:49:52.554: E/ActivityThread(32595): Service app.LicenseService has leaked ServiceConnection com.google.android.vending.licensing.LicenseChecker@41385168 that was originally bound here

这个看不懂 我认为这是管理付费版本的一种相当常见的方式,人们是如何管理这里的生命周期的?谢谢!

4

1 回答 1

0

答案是使用 ContentProvider 并在查询中提供一个简单的 MatrixCursor。BroadcastReceivers 是不可能的,因为要求它们必须首先由用户显式运行,这在不启动的应用程序中是不可能的。ContentProvider 有点奇怪但也更简单。

于 2013-09-18T16:44:51.817 回答