【问题标题】:Implementing Licensing service for Android AppWidget为 Android AppWidget 实施许可服务
【发布时间】:2012-06-22 16:05:08
【问题描述】:

实施应用程序许可非常简单,但看起来示例代码是为成熟的 android 应用程序而不是小部件量身定制的。

例如,在应用程序中,我们主要在启动屏幕或主活动中添加许可代码,如果不允许用户,则我们只是完成活动并且不允许使用该应用程序。

同样,在小部件中,任何人都可以提供一些线索,说明在哪里添加许可代码以及如果不允许用户采取什么措施?

如果有人能举出例子就好了。

提前谢谢你。

【问题讨论】:

    标签: android android-appwidget


    【解决方案1】:

    将配置活动添加到您的小部件并在那里实现您的许可逻辑。如果您没有得到肯定的结果,请不要让他们添加小部件。您可以在小部件更新服务中进行定期许可证检查。

    【讨论】:

      【解决方案2】:

      感谢 Nikolay 指出配置活动。这就是实施许可服务的实际情况。以下是许可服务实施的配置活动:

      package com.mobisys.android.contactwidget;
      
      import com.google.android.vending.licensing.AESObfuscator;
      import com.google.android.vending.licensing.LicenseChecker;
      import com.google.android.vending.licensing.LicenseCheckerCallback;
      import com.google.android.vending.licensing.ServerManagedPolicy;
      
      import android.app.Activity;
      import android.app.AlertDialog;
      import android.app.Dialog;
      import android.app.ProgressDialog;
      import android.appwidget.AppWidgetManager;
      import android.content.DialogInterface;
      import android.content.Intent;
      import android.net.Uri;
      import android.os.Bundle;
      import android.provider.Settings.Secure;
      import android.widget.Toast;
      
      public class ConfigActivity extends Activity {
          private LicenseChecker mChecker;
          private LicenseCheckerCallback mLicenseCheckerCallback;
          public static final String BASE64_PUBLIC_KEY = "PUBLIC KEY OF YOUR ACCOUNT";
          public static final byte[] SALT = new byte[] {
          -120, 30, 50, -20, 33, -100, 32, -90, -98, 104, 12,
          110, 78, -34, 105, 21, 62, 35, -12, 97
          };
      
          private AESObfuscator mObsfuscator;
          private String android_id;
          ProgressDialog pg;
          int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
      
          @Override
          public void onCreate(Bundle bundle){
              super.onCreate(bundle);
      
              setResult(RESULT_CANCELED);
              Intent intent = getIntent();
              Bundle extras = intent.getExtras();
              if (extras != null) {
                   mAppWidgetId = extras.getInt(
                           AppWidgetManager.EXTRA_APPWIDGET_ID,
                           AppWidgetManager.INVALID_APPWIDGET_ID);
              }
      
              // If they gave us an intent without the widget id, just bail.
              if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
                   finish();
              }
      
              pg=ProgressDialog.show(this, "Verification", "Verifying purchase");
              pg.show();
              android_id = Secure.getString(this.getContentResolver(), Secure.ANDROID_ID);
              mObsfuscator = new AESObfuscator(SALT, getPackageName(), android_id);
              ServerManagedPolicy serverPolicy = new ServerManagedPolicy(this,mObsfuscator);
      
              mLicenseCheckerCallback = new MyLicenseCheckerCallback();
              mChecker = new LicenseChecker(
                  this, serverPolicy,
                  BASE64_PUBLIC_KEY  // Your public licensing key.
                  );
              mChecker.checkAccess(mLicenseCheckerCallback);
          }
      
          private void onAllow(){
               Intent resultValue = new Intent();
               resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
               setResult(RESULT_OK, resultValue);
               finish();
          }
      
          private class MyLicenseCheckerCallback implements LicenseCheckerCallback {
              @Override
              public void allow(int reason) {
                  if(pg!=null&&pg.isShowing())
                      pg.dismiss();
                  if (isFinishing()) {
                      // Don't update UI if Activity is finishing.
                      return;
                  }
                  runOnUiThread(new Runnable(){
      
                      @Override
                      public void run() {
                          onAllow();
                      }
      
                  });
              }
      
              @Override
              public void dontAllow(int reason) {
                  if(pg!=null&&pg.isShowing())
                      pg.dismiss();
      
                  if (isFinishing()) {
                      // Don't update UI if Activity is finishing.
                      return;
                  }
      
                  showDialog(0);
              }
      
              @Override
              public void applicationError(int errorCode) {
                  Toast.makeText(ConfigActivity.this, "Application Error:"+errorCode, Toast.LENGTH_SHORT).show();
                  if(pg!=null&&pg.isShowing())
                      pg.dismiss();
                  if (isFinishing()) return;
      
                  finish();
              }
          }
      
          @Override
          protected Dialog onCreateDialog(int id) {
              // We have only one dialog.
              AlertDialog.Builder builder=new AlertDialog.Builder(this)
              .setTitle("Application Not Licensed")
              .setCancelable(false)
              .setMessage("This application is not licensed. Please purchase it from Google play")
              .setPositiveButton("Buy App", new DialogInterface.OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialog,int which) {
                      Intent marketIntent = new Intent(
                          Intent.ACTION_VIEW,
                          Uri.parse("market://details?id=" + getPackageName()));
                          startActivity(marketIntent);
                          finish();
                      }})
                      .setNegativeButton("Exit",new DialogInterface.OnClickListener() {
                          @Override
                          public void onClick(DialogInterface dialog, int which) {
                              finish();
                      }
                  }
              );
      
              return builder.create();
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-12-28
        • 1970-01-01
        • 2023-03-11
        • 1970-01-01
        • 1970-01-01
        • 2022-01-27
        • 1970-01-01
        相关资源
        最近更新 更多