java - 如何将应用内购买添加到按钮?
问题描述
我想在我的应用程序中添加开始应用内购买的按钮,并且在成功支付后它应该执行一个操作。
我已经做了什么...
我在布局中添加了按钮,其 ID 为“doc_link1”到“doc_link11”。
问题:每个按钮都应该引起应用内购买,支付后应该在操作之前引起。我已经在 Google Play 管理中心设置了我的应用内购买帐户和 ID。
如何成功地将这些应用内购买 ID 添加到按钮中,当用户按下按钮时,它会执行应用内购买?
package com.test.keyboard;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
public class LinksFragment extends Fragment implements View.OnClickListener {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.roko_mojis_p2, null);
view.findViewById(R.id.doc_link_1).setOnClickListener(this);
view.findViewById(R.id.doc_link_2).setOnClickListener(this);
view.findViewById(R.id.doc_link_4).setOnClickListener(this);
view.findViewById(R.id.doc_link_5).setOnClickListener(this);
view.findViewById(R.id.doc_link_6).setOnClickListener(this);
view.findViewById(R.id.doc_link_7).setOnClickListener(this);
view.findViewById(R.id.doc_link_8).setOnClickListener(this);
view.findViewById(R.id.doc_link_9).setOnClickListener(this);
view.findViewById(R.id.doc_link_10).setOnClickListener(this);
view.findViewById(R.id.doc_link_11).setOnClickListener(this);
return view;
}
private void sendMail() {
String mailto = "mailto:" + getResources().getString(R.string.email_mailto) +
"?subject=" + Uri.encode(getResources().getString(R.string.email_subject)) +
"&body=" + Uri.encode("");
Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
emailIntent.setData(Uri.parse(mailto));
startActivity(Intent.createChooser(emailIntent, "Send Email"));
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.doc_link_1:
// About Me
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")));
break;
case R.id.doc_link_2:
// How to install
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")));
break;
case R.id.doc_link_4:
// Purchase Pack 1
sendMail();
break;
case R.id.doc_link_5:
// Purchase Pack 2
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")));
break;
case R.id.doc_link_6:
// Purchase Pack 3
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")));
break;
case R.id.doc_link_7:
// Purchase Pack 4
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")));
break;
case R.id.doc_link_8:
// Purchase Pack 5
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")));
break;
case R.id.doc_link_9:
// Purchase Pack 6
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")));
break;
case R.id.doc_link_10:
// Purchase Pack 7
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")));
break;
case R.id.doc_link_11:
// Purchase Pack 8
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")));
break;
default:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")));
break;
}
}
}
解决方案
如果您使用的是 googles 应用内计费库,您可以这样做:
case R.id.doc_link_1:
BillingFlowParams flowParams = BillingFlowParams.newBuilder()
.setSku(skuId)
.setType(SkuType.INAPP) // SkuType.SUB for subscription
.build();
int responseCode = mBillingClient.launchBillingFlow(flowParams);
请参阅https://developer.android.com/google/play/billing/billing_library_overview#Enable
推荐阅读
- python - 无法通过 Python 代码导入 xgboost 模块
- google-cloud-platform - 我的构建触发器无法识别 glob 模式
- python - PyQt6获取QTreeWidgetItem的父窗口?
- android - 如何知道用户点击了哪个按钮及其在 Android Studio 中的文本和 ID?
- python - 如何计算时间轴上重复次数最多的值的出现次数?- Xarray 蟒蛇
- javascript - Vue.js - 从 API 获取数据并在 Vuex 状态更改时重新加载组件
- javascript - 如何制作表格页面?
- sql - 在子查询中使用 MAX 的 SQL
- dynamic - BI Publisher RTF 模板中的动态超链接
- kubernetes - 如何查看 Kubernetes 上的所有可用资源?