2013-04-11 74 views
0

我试图通过让付费版本成为简单的许可服务器来管理免费/付费应用程序。LVL付费应用程序的BroadcastReceiver/IntentService

的付费应用程序有一个接收器:

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 [email protected] that was originally bound here 

这一次我不明白。我认为这是管理薪酬版本的一种相当常见的方式,人们在这里如何管理生命周期?谢谢!

回答

0

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

相关问题