使用滑动动作替换按钮以使用付费布局

时间:2018-05-15 16:47:01

标签: java android in-app-purchase swipe

我正在构建app。我拥有的一个功能是付费选项,用户可以访问其他布局。现在,用户可以通过单击按钮到达那里。相反,我想要一个滑动动作 - 如果用户向下滑动,它将要求获得付费布局的许可。如果他们付费,当他们向上滑动时,他们可以访问原始布局。

这是我的主要活动:

 public class MainActivity extends AppCompatActivity {
        EditText editText2;
        Button btn_purchase;
        ImageView imageView2;
        String skuId;
        SessionManager sessionManager;
        // create new Person
        private BillingClient mBillingClient;

        @RequiresApi(api = Build.VERSION_CODES.M)
        @Override
            protected void onCreate (Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                sessionManager=new SessionManager(MainActivity.this);
                if(sessionManager.getPurchased()){
                    Intent intent = new Intent(MainActivity.this, PaidActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                    finish();
                }
                btn_purchase=findViewById(R.id.btn_purchase);
            imageView2 = (ImageView) findViewById(R.id.imageView2);

            mBillingClient = BillingClient.newBuilder(MainActivity.this).setListener(new PurchasesUpdatedListener() {
                @Override
                public void onPurchasesUpdated(int responseCode, @Nullable List<Purchase> purchases) {
                    if (responseCode == BillingClient.BillingResponse.OK
                            && purchases != null) {
                        for (Purchase purchase : purchases) {
                            //TODO: Handle purchase
                            //        handlePurchase(purchase);
                            Log.e("Main", "handle Purchase ");
                            Log.e("MainAct", "onPurchasesUpdated() response: " + responseCode);
                            Log.e("dev", "successful purchase...");
                            String purchasedSku = purchase.getSku();
                            Log.e("dev", "Purchased SKU: " + purchasedSku);
                            String purchaseToken = purchase.getPurchaseToken();
                            sessionManager.savePurchased(true);
                            Intent intent = new Intent(MainActivity.this, PaidActivity.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                            finish();
                        }
                    } else if (responseCode == BillingClient.BillingResponse.USER_CANCELED) {
                        // Handle an error caused by a user cancelling the purchase flow.
                    } else {
                        // Handle any other error codes.
                    }
                }
            }).build();



            List<String> skuList = new ArrayList <> ();
            skuList.add("appsubscription2018");
            SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
            params.setSkusList(skuList).setType(BillingClient.SkuType.INAPP);
            mBillingClient.querySkuDetailsAsync(params.build(),
                    new SkuDetailsResponseListener() {
                        @Override
                        public void onSkuDetailsResponse(int responseCode, List<SkuDetails> skuDetailsList) {
                            // Process the result.

                            if (responseCode == BillingClient.BillingResponse.OK && skuDetailsList != null) {
                                Log.e("MainActivity", "onSkuDetailsResponse: success");
                                for (SkuDetails skuDetails : skuDetailsList) {

                                    Log.e("MainActivity", "products: "+skuDetailsList);
                                    String sku = skuDetails.getSku();
                                    skuId = skuDetails.getSku();
                                    String price = skuDetails.getPrice();
                                    if ("appsubscription2018 ".equals(sku)) {
                                        Log.e("MainActivity", "product is appsubscription2018 ");
                              //          mPremiumUpgradePrice = price;
                                    }
                                }
                            }
                        }
                    });




            btn_purchase.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    mBillingClient.startConnection(new BillingClientStateListener() {
                        @Override
                        public void onBillingSetupFinished(@BillingClient.BillingResponse int billingResponseCode) {
                            if (billingResponseCode == BillingClient.BillingResponse.OK) {
                                // The billing client is ready. You can query purchases here.
                                // The billing client is ready. You can query purchases here.
                                BillingFlowParams flowParams = BillingFlowParams.newBuilder()
                                        .setSku("appsubscription2018")
                                        .setType(BillingClient.SkuType.INAPP)
                                        .build();
                                int responseCode = mBillingClient.launchBillingFlow(MainActivity.this, flowParams);
                                if (responseCode != BillingClient.BillingResponse.OK) {
                                 Log.e("Main", "launchBillingFlow ok");
                                }
                            }
                        }
                        @Override
                        public void onBillingServiceDisconnected() {
                            // Try to restart the connection on the next request to
                            // Google Play by calling the startConnection() method.
                        }
                    });

                }
            });


    imageView2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                        if (checkFilePermission(MainActivity.this)){
                    getPhoto();
                }
        }
    });






            }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);


            if (requestCode ==1 && resultCode == RESULT_OK && data != null) {
                Uri selectedImage = data.getData();

                try {
                    Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);

                    imageView2.setImageBitmap(bitmap);


                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
        public static boolean checkFilePermission(final Context context) {
            int currentAPIVersion = Build.VERSION.SDK_INT;
            if(currentAPIVersion>= Build.VERSION_CODES.M)
            {
                if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                    if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, Manifest.permission.READ_EXTERNAL_STORAGE)) {
                        AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
                        alertBuilder.setCancelable(true);
                        alertBuilder.setTitle("Permission necessary");
                        alertBuilder.setMessage("External storage permission is necessary");
                        alertBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                            @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
                            public void onClick(DialogInterface dialog, int which) {
                                ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
                            }
                        });
                        AlertDialog alert = alertBuilder.create();
                        alert.show();
                    } else {
                        ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
                    }
                    return false;
                } else {
                    return true;
                }
            } else {
                return true;
            }
        }

        public void getPhoto() {
            Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(intent,1);
        }

        @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);

            if (requestCode ==1){
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED);{
                    getPhoto();
                }
            }
        }

    }

0 个答案:

没有答案