2011-11-09 33 views
3

我有一個免費的應用程序,其中還包含專業功能,如果他們購買我的應用程序在Android電子市場的專業密鑰將提供給用戶。我在專業應用程序中設置了一個接收器,並在主應用程序中設置了另一個接收器。當用戶點擊主應用程序中的「驗證密鑰」按鈕時,意圖會在關鍵應用程序中廣播給接收方,然後啓動IntentService以檢查專業密鑰許可證是否有效。然後,IntentService向主應用程序接收器廣播包含LVL檢查額外「響應」的意圖。從意向服務的LVL許可證檢查

我快到了。只有當我嘗試在IntentService中執行LVL檢查時,出現錯誤:

W/MessageQueue(2652): java.lang.RuntimeException: Handler{4052de20} sending message to a Handler on a dead thread 
W/MessageQueue(2773): at com.android.vending.licensing.LicenseChecker$ResultListener.verifyLicense(LicenseChecker.java:207) 

有什麼建議嗎?

的IntentService來源:

public class CheckerService extends IntentService { 

private static final byte[] SALT = ....; 
private static final String BASE64_PUBLIC_KEY = ...; 
private String device_id; 

public CheckerService() { 
    super("CheckerService"); 
} 

@Override 
protected void onHandleIntent(Intent intent) { 
    SharedPreferences data = getSharedPreferences("data", MODE_PRIVATE); 
    device_id = data.getString("device_id", null); 

    if (device_id == null) { 
     SharedPreferences.Editor editor = data.edit(); 
     device_id = new DeviceId().generate(this); 
     editor.putString("device_id", device_id); 
     editor.commit(); 
    } 

    ServerManagedPolicy smp = new ServerManagedPolicy(this, 
      new AESObfuscator(SALT, getPackageName(), device_id)); 
    LicenseChecker checker = new LicenseChecker(this, smp, BASE64_PUBLIC_KEY); 
    checker.checkAccess(new LicenseCheckerCallback(){ 

     public void allow() { 
      Intent i = new Intent();     
      i.setAction("com.mainapp.intent.action.LICENSE_RESPONSE"); 
      i.putExtra("response", "LICENSE_OK"); 
      sendBroadcast(i); 
     } 

     public void dontAllow() { 
      Intent i = new Intent();     
      i.setAction("com.mainapp.intent.action.LICENSE_RESPONSE"); 
      i.putExtra("response", "LICENSE_NOT_OK"); 
      sendBroadcast(i); 
     } 

     public void applicationError(ApplicationErrorCode errorCode) { 
      Intent i = new Intent();     
      i.setAction("com.mainapp.intent.action.LICENSE_RESPONSE"); 
      i.putExtra("response", "LICENSE_ERROR"); 
      sendBroadcast(i); 
     }}); 

} 


@Override 
public void onDestroy() { 
    super.onDestroy(); 
    checker.onDestroy(); 
} 
} 
+0

你能做到嗎?需要更多的LVL示例 –

回答

0

我相信這個問題是與IntentService。一個IntentService管理它自己的生命週期。本質上它只是一個在非UI線程上運行的服務。服務在回調可以被處理之前被銷燬,所以當回調發生時什麼都不存在,以便「回叫」。

您可以嘗試使用START_STICKY或START_NOT_STICKY在服務中運行它,並自行管理生命週期。