2013-04-11 42 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有點奇怪,但也更簡單。

相關問題