为Android AppWidget实施许可服务

时间:2012-06-20 14:24:38

标签: android android-appwidget

实现应用程序许可是非常简单的,但看起来好像示例代码是为完整的Android应用程序而不是小部件定制的。

同样,在应用程序中,我们主要在Splash屏幕或Main活动中添加许可代码,如果不允许用户,那么我们只是完成活动而不允许使用该应用程序。

同样,在小部件中,任何人都可以提供一些线索,说明添加许可代码的位置以及不允许用户采取的操作吗?

如果有人可以指出榜样,那将会很棒。

提前谢谢。

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

感谢Nikolay指向Configuration Activity。这就是实施许可服务的实际情况。以下是具有许可服务实现的配置活动:

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();
    }

}