我有一个包含专业功能的免费应用程序,如果用户在Android Market为我的应用程序购买专业密钥,就可以使用。我在pro key app中设置了一个接收器,在主app中设置了另一个接收器。当用户在主app中点击"Validate key"按钮时,一个意图被广播到key app中的接收器,然后触发IntentService来检查pro key许可证是否有效。然后IntentService向主应用接收器广播一个意图,其中包含来自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();
}
}
我认为这个问题与IntentService有关。IntentService管理它自己的生命周期。本质上,它只是一个在非ui线程上运行的服务。在回调被处理之前,服务就被销毁了,所以当回调发生时,没有任何东西可以让它'回调'。
你可以尝试在START_STICKY或START_NOT_STICKY的服务中运行它,并自己管理生命周期。