我有一个免费的应用程序,其中还包含专业版功能,如果用户在 Android Market 中为我的应用程序购买了专业版 key ,就可以使用这些功能。我在专业 key 应用程序中设置了一个接收器,在主应用程序中设置了另一个接收器。当用户单击主应用程序中的“验证 key ”按钮时, Intent 将广播到 key 应用程序中的接收器,然后启动 IntentService 以检查专业 key 许可证是否有效。然后 IntentService 向主应用程序接收器广播一个 Intent ,其中包含来自 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 的服务中运行它并自行管理生命周期。
关于android - Intent Service 的 LVL 许可证检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8073014/