Java 如何知道inapp subscriptionperiod是否已完成或用户已取消?

Java 如何知道inapp subscriptionperiod是否已完成或用户已取消?,java,android,Java,Android,简单地说,我制作了一个带有订阅的程序,当用户订阅时,布尔值变为true。 当我测试软件时,如果取消订阅或订阅自动完成,布尔值仍然返回true 我需要在代码中输入一个检查,看看订阅是否仍然可用 由于我是android studio的新手,我已经寻找这个问题两周了,但没有找到解决方案。 所有的解决方案和帖子都在用这条神奇的线谈论旧的应用程序库(AIDL) Bundle-ownedItems=mService.getPurchases(3,getPackageName(),“inapp”,null)

简单地说,我制作了一个带有订阅的程序,当用户订阅时,布尔值变为true。 当我测试软件时,如果取消订阅或订阅自动完成,布尔值仍然返回true

我需要在代码中输入一个检查,看看订阅是否仍然可用

由于我是android studio的新手,我已经寻找这个问题两周了,但没有找到解决方案。 所有的解决方案和帖子都在用这条神奇的线谈论旧的应用程序库(AIDL)

Bundle-ownedItems=mService.getPurchases(3,getPackageName(),“inapp”,null)

但在新的Google Play计费库中,这似乎不起作用

以下是我的计费活动:

)

公共类BillingActivity扩展AppCompative实现PurchasesUpdatedListener{

private static final String TAG = "BillingActivity";

private Button button;
protected SharedPreferences mSharedPreferences;
private boolean checkActivation;
private BillingClient mBillingClient;
private List<String> skuList;
private SkuDetailsParams.Builder skuParams;
private BillingFlowParams flowParams;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_billing);


    // Shred Preferences & Active user Initialize
    checkActivation = false;
    mSharedPreferences = this.getSharedPreferences("com.aterosolutions.customerspremiums", Context.MODE_PRIVATE);
    if (mSharedPreferences.getInt("activeUser", 0) == 1) { // 0 ==> InActiveUser   1 ==> ActiveUser
        //mSharedPreferences.edit().putInt("activeUser", 1).apply();
        checkActivation = true;
    } else {
        //mSharedPreferences.edit().putInt("activeUser", 0).apply();
        checkActivation = false;
    }
    Toast.makeText(this, checkActivation + "", Toast.LENGTH_SHORT).show();


    // SKU List
    skuList = new ArrayList<>();
    skuList.add("premiums_subscribe");
    skuParams = SkuDetailsParams.newBuilder();
    skuParams.setSkusList(skuList).setType(BillingClient.SkuType.SUBS);

    // Establish connection to billing client
    mBillingClient = BillingClient.newBuilder(this).enablePendingPurchases().setListener(this).build();
    mBillingClient.startConnection(new BillingClientStateListener() {
        @Override
        public void onBillingSetupFinished(BillingResult billingResult) {
            Log.i(TAG, "onBillingSetupFinished: start" + billingResult.getResponseCode());
            if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
                Log.i(TAG, "onBillingSetupFinished: second congrat");
            } else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.ITEM_ALREADY_OWNED) {
                Log.i(TAG, "onBillingSetupFinished: you own it");
            } else {
                Log.i(TAG, "onBillingSetupFinished: not your product");
            }
        }

        @Override
        public void onBillingServiceDisconnected() {
            Toast.makeText(BillingActivity.this, "Connection Error", Toast.LENGTH_SHORT).show();
            Log.i(TAG, "onBillingServiceDisconnected: Connection Error");

        }
    });


    queryPurchases();
    //checkPurchsedItem();

    // Button Handle
    button = findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mBillingClient.querySkuDetailsAsync(skuParams.build(), new SkuDetailsResponseListener() {
                @Override
                public void onSkuDetailsResponse(BillingResult billingResult, List<SkuDetails> skuDetailsList) {
                    flowParams = BillingFlowParams.newBuilder().setSkuDetails(skuDetailsList.get(0)).build();
                    BillingResult response = mBillingClient.launchBillingFlow(BillingActivity.this, flowParams);
                    Log.i(TAG, "onSkuDetailsResponse: " + billingResult.getResponseCode());
                    Log.i(TAG, "onSkuDetailsResponse: response OK");
                    Log.i(TAG, "onSkuDetailsResponse: my test" + response);
                    Log.i(TAG, "onSkuDetailsResponse: queryPurshase01 " + mBillingClient.queryPurchases(BillingClient.SkuType.SUBS));

                    /* (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK && skuDetailsList != null) {
                        flowParams = BillingFlowParams.newBuilder().setSkuDetails(skuDetailsList.get(0)).build();
                        mBillingClient.launchBillingFlow(BillingActivity.this, flowParams);
                        Log.i(TAG, "onSkuDetailsResponse: response OK");

                    }else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.ITEM_ALREADY_OWNED && skuDetailsList != null){
                        Log.i(TAG, "onSkuDetailsResponse: response already Owned");
                    }else {
                        Log.i(TAG, "onSkuDetailsResponse: response something else");
                    }*/
                }
            });
        }
    });

}


@Override
public void onPurchasesUpdated(BillingResult billingResult, @Nullable List<Purchase> purchases) {
    Log.i(TAG, "onPurchasesUpdated: start /// purchses"+ billingResult.getResponseCode() );

    if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK /*&& purchases != null*/) {
        mSharedPreferences.edit().putInt("activeUser", 1).apply();
        MainScreenActivity.activeUser = true;
        for (Purchase purchase : purchases) {
            handleNewPurchase(purchase);
        }
    } else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.ITEM_ALREADY_OWNED /*&& purchases != null*/) {
        Log.i(TAG, "onPurchasesUpdated: You Already Own It");
        Toast.makeText(this, "Already Owned", Toast.LENGTH_SHORT).show();
        mSharedPreferences.edit().putInt("activeUser", 1).apply();
        MainScreenActivity.activeUser = true;

    } else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.USER_CANCELED /*&& purchases != null*/) {
        Log.i(TAG, "onPurchasesUpdated: User Canceled");
        Toast.makeText(this, "User Canceled", Toast.LENGTH_SHORT).show();

    } else {
        Log.i(TAG, "onPurchasesUpdated: other error " + billingResult.getResponseCode());

    }


}

private void handleNewPurchase(Purchase purchase) {

    Log.i(TAG, "handleNewPurchase: queryPurshase00 " + mBillingClient.queryPurchases(BillingClient.SkuType.SUBS).getBillingResult());

    for (int i = 0; i < skuList.size(); i++) {
        if (purchase.getSku() == skuList.get(i)) {
            mSharedPreferences.edit().putInt("activeUser", 1).apply();
            MainScreenActivity.activeUser = true;
            Toast.makeText(this, "congrat dear", Toast.LENGTH_SHORT).show();
            Log.i(TAG, "handleNewPurchase: product purchsed ");

            // Acknowledge the purchase if it hasn't already been acknowledged.
            if (!purchase.isAcknowledged()) {
                Log.i(TAG, "handlePurchase: ok02");
                AcknowledgePurchaseParams acknowledgePurchaseParams = AcknowledgePurchaseParams.newBuilder()
                        .setPurchaseToken(purchase.getPurchaseToken())
                        .build();

                AcknowledgePurchaseResponseListener acknowledgePurchaseResponseListener = new AcknowledgePurchaseResponseListener() {
                    @Override
                    public void onAcknowledgePurchaseResponse(BillingResult billingResult) {


                    }

                };
                Log.i(TAG, "handleNewPurchase: aknowledge done");
                mBillingClient.acknowledgePurchase(acknowledgePurchaseParams, acknowledgePurchaseResponseListener);
            } else {
                Log.i(TAG, "handleNewPurchase: no need to aknowledge");
            }
        }
    }
}

private void queryPurchases() {
    Purchase.PurchasesResult purchasesResult = mBillingClient.queryPurchases(BillingClient.SkuType.SUBS);
    if (purchasesResult != null) {
        List<Purchase> purchaseList = purchasesResult.getPurchasesList();
        if (purchaseList == null) {
            return;
        }

        if (!purchaseList.isEmpty()){
            for (Purchase purchase : purchaseList){
                if (purchase.getSku().equals(skuList.get(0))){
                    //mSharedPreferences.edit().putInt("activeUser", 1).apply();
                    //MainScreenActivity.activeUser = true;
                }
            }
        }

    }

}

@Override
protected void onDestroy() {
    super.onDestroy();
    mBillingClient.endConnection();

}

private void checkPurchsedItem(){
    mBillingClient.queryPurchaseHistoryAsync(BillingClient.SkuType.SUBS, new PurchaseHistoryResponseListener() {
        @Override
        public void onPurchaseHistoryResponse(BillingResult billingResult, List<PurchaseHistoryRecord> purchaseHistoryRecordList) {
            Log.i(TAG, "onPurchaseHistoryResponse: " + billingResult.getResponseCode());
        }
    });

    Purchase.PurchasesResult purchasesResult = mBillingClient.queryPurchases(BillingClient.SkuType.SUBS);
    Log.i(TAG, "checkPurchsedItem: " + purchasesResult.getBillingResult());
    try {
        Log.i(TAG, "checkPurchsedItem: " + purchasesResult.getPurchasesList().size());
    }catch (Exception e){
        e.printStackTrace();
    }
    Log.i(TAG, "checkPurchsedItem: " + purchasesResult.getResponseCode());
    Log.i(TAG, "checkPurchsedItem: " + purchasesResult.getBillingResult());
}
}
private static final String TAG=“BillingActivity”;
私人按钮;
受保护的共享引用mSharedPreferences;
私有布尔校验激活;
私人计费客户mBillingClient;
私人名单;
私人SkuDetailsParams.Builder skuParams;
私有BillingFlowParams flowParams;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_billing);
//分解首选项和活动用户初始化
checkActivation=false;
mSharedPreferences=this.getSharedReferences(“com.aterosolutions.customerspreferences”,Context.MODE\u PRIVATE);
if(mSharedPreferences.getInt(“activeUser”,0)==1){//0=>InActiveUser 1==>activeUser
//mSharedPreferences.edit().putInt(“activeUser”,1.apply();
checkActivation=true;
}否则{
//mSharedPreferences.edit().putInt(“activeUser”,0.apply();
checkActivation=false;
}
Toast.makeText(this,checkActivation+“”,Toast.LENGTH_SHORT).show();
//SKU列表
skuList=新的ArrayList();
skuList.添加(“保费认购”);
skuParams=skutailsparams.newBuilder();
skuParams.setskulist(skuList).setType(BillingClient.SkuType.SUBS);
//建立与计费客户端的连接
mBillingClient=BillingClient.newBuilder(this.enablePendingPurchases().setListener(this.build());
mBillingClient.startConnection(新的BillingClientStateListener(){
@凌驾
BillingSetupFinished(BillingResult BillingResult)上的公共无效{
Log.i(标记“onBillingSetupFinished:start”+billingResult.getResponseCode());
if(billingResult.getResponseCode()==BillingClient.BillingResponseCode.OK){
Log.i(标记“onBillingSetupFinished:second congreat”);
}else if(billingResult.getResponseCode()==BillingClient.BillingResponseCode.ITEM_已拥有){
i(标记“onBillingSetupFinished:您拥有它”);
}否则{
Log.i(标记“onBillingSetupFinished:不是您的产品”);
}
}
@凌驾
公共无效onBillingServiceDisconnected(){
Toast.makeText(BillingActivity.this,“连接错误”,Toast.LENGTH_SHORT.show();
Log.i(标记“onBillingServiceDisconnected:连接错误”);
}
});
queryPurchases();
//checkpurcheditem();
//按钮手柄
按钮=findViewById(R.id.button);
setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
mBillingClient.querySkuDetailsAsync(skuParams.build(),新的SkuDetailsResponseListener()){
@凌驾
公共无效onSkuDetailsResponse(BillingResult BillingResult,列表skuDetailsList){
flowParams=BillingFlowParams.newBuilder().setSkuDetails(skuDetailsList.get(0)).build();
BillingResult response=mBillingClient.launchBillingFlow(BillingActivity.this,flowParams);
Log.i(标记“onSkuDetailsResponse:+billingResult.getResponseCode());
Log.i(标记“onSkuDetailsResponse:response OK”);
Log.i(标记“onSkuDetailsResponse:我的测试”+响应);
Log.i(标记“onsku详细信息响应:queryPurshase01”+mBillingClient.queryPurchases(BillingClient.SkuType.SUBS));
/*(billingResult.getResponseCode()==BillingClient.BillingResponseCode.OK&&skuDetailsList!=null){
flowParams=BillingFlowParams.newBuilder().setSkuDetails(skuDetailsList.get(0)).build();
mBillingClient.launchBillingFlow(BillingActivity.this,flowParams);
Log.i(标记“onSkuDetailsResponse:response OK”);
}else if(billingResult.getResponseCode()==BillingClient.BillingResponseCode.ITEM_已拥有&&skuDetailsList!=null){
Log.i(标记“onSkuDetailsResponse:响应已拥有”);
}否则{
Log.i(标记“onskuedetailsresponse:response-something”);
}*/
}
});
}
});
}
@凌驾
购买时公共作废已过期(BillingResult BillingResult,@Nullable列表购买){
Log.i(标记“onPurchasesUpdated:start///purches”+billingResult.getResponseCode());
if(billingResult.getResponseCode()==BillingClient.BillingResponseCode.OK/*&&purchases!=null*/){
mSharedPreferences.edit().putInt(“activeUser”,1.apply();
MainScreenActivity.activeUser=true;
用于(采购:采购){
handleNewPurchase(采购);
}
}else if(billingResult.getResponseCode()==BillingClient.BillingResponseCode.ITEM_已拥有/*&&purchases!=null*/){
Log.i(标记“onPurchasesUpdated:您已经拥有它”);
Toast.makeText(此“已拥有”,Toast.LENGTH_SHORT).show();
mSharedPreferences.edit().putInt(“活动
<dependency>
    <groupId>com.google.apis</groupId>
    <artifactId>google-api-services-androidpublisher</artifactId>
    <version>v2-rev50-1.22.0</version>
</dependency>
@Singleton
@Startup
public class WebserverConfiguration
{
    private String serviceAccountPrivateKeyFilePath;

    /** Global instance of the HTTP transport. */
    public static HttpTransport HTTP_TRANSPORT;

    /** Global instance of the JSON factory. */
    public static JsonFactory JSON_FACTORY;

    private GoogleCredential credential;

    @PostConstruct
    public void init()
    {
        assignServiceAccountFileProperty();
        initGoogleCredentials();
    }

    public String getServiceAccountPrivateKeyFilePath()
    {
        return serviceAccountPrivateKeyFilePath;
    }

    public GoogleCredential getCredential()
    {
        return credential;
    }

    private void initGoogleCredentials()
    {
        try
        {
            newTrustedTransport();
            newJsonFactory();

            String serviceAccountContent = new String(Files.readAllBytes(Paths.get(getServiceAccountPrivateKeyFilePath())));
            InputStream inputStream = new ByteArrayInputStream(serviceAccountContent.getBytes());

            credential = GoogleCredential.fromStream(inputStream).createScoped(Collections.singleton(AndroidPublisherScopes.ANDROIDPUBLISHER));

        }
        catch (IOException | GeneralSecurityException e)
        {
            throw new InitializationException(e);
        }
    }

    private void newJsonFactory()
    {
        JSON_FACTORY = JacksonFactory.getDefaultInstance();
    }

    private void assignServiceAccountFileProperty()
    {
        serviceAccountPrivateKeyFilePath = System.getProperty("service.account.file.path");
        if (serviceAccountPrivateKeyFilePath == null)
        {
            throw new IllegalArgumentException("service.account.file.path UNKNOWN - configure it as VM startup parameter in Wildfly");
        }
    }

    private static void newTrustedTransport() throws GeneralSecurityException, IOException
    {
        if (HTTP_TRANSPORT == null)
        {
            HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
        }
    }
}
6.) Now I am able the fetch Google Play Developer API information, e.g. reviews:

private void invokeGoogleApi() throws IOException
{       
    AndroidPublisher publisher = new AndroidPublisher.Builder(WebserverConfiguration.HTTP_TRANSPORT, WebserverConfiguration.JSON_FACTORY, configuration.getCredential()).setApplicationName("The name of my app on Google Play").build();
    AndroidPublisher.Reviews reviews = publisher.reviews();
    ReviewsListResponse reviewsListResponse = reviews.list("the.packagename.of.my.app").execute();
    logger.info("review list response = " + reviewsListResponse.toPrettyString());
}
This worked.

I cannot test it yet, but I'm sure that fetching the billing information works as well:

private SubscriptionPurchase getPurchase() throws IOException
{
    AndroidPublisher publisher = new AndroidPublisher.Builder(WebserverConfiguration.HTTP_TRANSPORT, WebserverConfiguration.JSON_FACTORY, configuration.getCredential()).setApplicationName("The name of my app on Google Play").build();
    AndroidPublisher.Purchases purchases = publisher.purchases();

    SubscriptionPurchase purchase = purchases.subscriptions().get("the.packagename.of.my.app", "subscriptionId", "billing token sent by the app").execute();

    //do something or return
    return purchase;
}