许可证检查专业密钥

时间:2011-11-07 09:28:43

标签: android android-lvl

所以我想制作一个加载了全部功能的免费应用程序。在应用程序检测到许可的专业密钥之前,专业版功能将被禁用。当然我想让pro key使用LVL检查它的许可证。虽然我知道如何在此之前做正确的事情,但我不知道如何让专业密钥与应用程序通信它应该启用专业功能。

这是主要的应用代码(com.test.mainapp):

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    context = getApplicationContext();

    final PackageManager pacman = getPackageManager();
    final int signatureMatch = pacman.checkSignatures(getPackageName(),
            "com.test.mainapp_key");

    if (signatureMatch == PackageManager.SIGNATURE_MATCH) {
        Toast.makeText(context, "Pro key detected!", Toast.LENGTH_SHORT)
                .show();
    } else {
        Toast.makeText(context, "Free version", Toast.LENGTH_SHORT).show();
    }
}

虽然这会阻止其他人为我的应用制作假钥匙,但他们仍然可以在线共享关键应用程序给其他人并且它会起作用。因为我们不能从另一个应用程序执行LVL检查,我希望许可证密钥应用程序检查它自己的许可证,如果它是正确的,那么只有用户才能获得专业功能。如何让许可证密钥应用程序与主应用程序相互通信?

我试图获得的功能就像Titanium Backup一样。

2 个答案:

答案 0 :(得分:10)

您可以在主应用和关键应用之间发送意图。如果您在关键应用程序中使用LVL,那么示例回调将是:

private class MyLicenseCheckerCallback implements LicenseCheckerCallback {
    public void allow() {
        Log.i("Key", "License Accepted");
        Intent i = new Intent();
        i.setAction("intent.to.call.on.success");
        i.putExtra("licenseresult", 0);
        c.sendBroadcast(i);
        mChecker.onDestroy();
    }

    public void dontAllow() {
        Log.e("Key", "License Denied");
        Intent i = new Intent();
        i.setAction("intent.to.call.on.failure");
        i.putExtra("licenseresult", 1);
        c.sendBroadcast(i);
        mChecker.onDestroy();
    }

    public void applicationError(ApplicationErrorCode errorCode) {
        Log.i("Key", "LR Error");
        Intent i = new Intent();
        i.setAction("intent.to.call.on.error");
        i.putExtra("licenseresult", 2);
        c.sendBroadcast(i);
        mChecker.onDestroy();
    }
}

您可以在两个应用中设置广播接收器以启动许可检查并处理结果。 e.g。

public class License extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    if (intent.getAction().equals("intent.called.to.initiate.request")) {
                // Initiate the license request here (possibly using a service)
        return;
    }
}

} 

您应该使用基于证书的权限限制对意图的访问,以防止其他应用程序发送欺骗性的“许可证成功”意图。

http://developer.android.com/reference/android/content/BroadcastReceiver.html#Permissions

短版。

在清单中定义权限

< permission android:name="my.package.LicenseCheck" android:protectionLevel="signature"/> 

在接收者声明中使用权限:

<receiver android:name="name.of.your.receiver" android:permission="my.package.LicenseCheck"> 
<intent-filter>
    <action android:name="name.of.intent"/>
</intent-filter>
</receiver>

答案 1 :(得分:1)

@MainApp

<permission android:name="myapp.permission.RESPOND" protectionLevel="signature" />

<receiver android:name=".receiver.WhichHandlesValidationResponse" android:permisssion="myapp.permission.RESPOND">
    <intent-filter>
            <action android:name="myapp.action.VALIDATION_RESPONSE" />
    </intent-filter>
</receiver>

@KeyApp

<uses-permission android:name="myapp.permission.RESPOND" />

创建包含以下内容的LvlValidationHandler.class:

Intent i = new Intent("myapp.action.VALIDATION_RESPONSE");
sendBroadcast(i);