Я бы хотел, чтобы пользователь мог покупать монеты, но после нажатия кнопки платежа повторное нажатие не запросит новую покупку, а просто добавит новые монеты.Я использую " android-inapp-billing-v3 " для своей покупки.Я очень благодарен за вашу помощь.Если он не создает комитеты, я также очень благодарен, если кто-нибудь покажет мне, как я могу запросить несколько групп монет в приложении и выполнить соответствующий код.
Вот мой код MainActivity.class
:
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, BillingProcessor.IBillingHandler {
public static final String RTMP_BASE_URL = "rtmp://";
private FirebaseUser firebaseUser;
private TextView username, bio, coins, usd;
private ImageView image_profile;
private String test = "";
private Integer sum = 0;
BillingProcessor bp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
View header = navigationView.getHeaderView(0);
image_profile = header.findViewById(R.id.image_profile);
bio = header.findViewById(R.id.bio);
username = header.findViewById(R.id.username);
coins = header.findViewById(R.id.coins);
usd = header.findViewById(R.id.usd);
firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
setSupportActionBar(toolbar);
// Billing
bp = new BillingProcessor(this, "KEY", this);
bp.initialize();
//fragment
Bundle intent = getIntent().getExtras();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new HomeFragment()).commit();
FloatingActionButton floatingActionButton = findViewById(R.id.floatingActionButton);
floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, TaskActivity.class);
startActivity(intent);
}
});
logout();
userInfo();
}
private void userInfo() {
DatabaseReference reference = FirebaseDatabase
.getInstance()
.getReference("user")
.child(firebaseUser.getUid());
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (getContext() == null) {
return;
}
User user = dataSnapshot.getValue(User.class);
RequestOptions requestOptions = new RequestOptions();
requestOptions.diskCacheStrategy(DiskCacheStrategy.ALL);
Glide
.with(getApplicationContext())
.load(Objects.requireNonNull(user).getImageurl())
.apply(requestOptions)
.into(image_profile);
username.setText(user.getUsername());
bio.setText(user.getBio());
coins.setText(user.getCoins());
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
@Override
public void onBackPressed() {
DrawerLayout drawer = findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
public void logout(){
TextView mTextView = findViewById(R.id.logout);
mTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Log Out
Toast.makeText(getApplicationContext(),"Log Out",Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
bp.purchase(MainActivity.this,"android.test.purchased");
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.account) {
Intent intent = new Intent(MainActivity.this, EditProfileActivity.class);
startActivity(intent);
} else if (id == R.id.home) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new HomeFragment()).commit();
} else if (id == R.id.task) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new TaskFragment()).commit();
}
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
@Override
protected void onStart() {
super.onStart();
// delete listeneradd mAuth.addAuthStateListener(mAuthListener);
}
@Override
public void onProductPurchased(@NonNull String productId, @Nullable TransactionDetails details) {
Toast.makeText(this,"Gekauft", Toast.LENGTH_SHORT).show();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("user").child(firebaseUser.getUid());
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
test = user.getCoins();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
Integer wert = Integer.parseInt(test);
Integer pushcoins = 10;
sum = pushcoins + wert;
updateProfile(Objects.<Integer>requireNonNull(sum));
}
private void updateProfile(Integer sum){
DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("user").child(firebaseUser.getUid());
HashMap<String, Object> map = new HashMap<>();
String convert;
convert = Integer.toString(sum);
map.put("coins", convert );
reference.updateChildren(map);
Toast.makeText(MainActivity.this, "Successfully updated!", Toast.LENGTH_SHORT).show();
}
@Override
public void onPurchaseHistoryRestored() {
}
@Override
public void onBillingError(int errorCode, @Nullable Throwable error) {
}
@Override
public void onBillingInitialized() {
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (!bp.handleActivityResult(requestCode, resultCode, data)) {
super.onActivityResult(requestCode, resultCode, data);
}
}
@Override
public void onDestroy(){
if (bp != null){
bp.release();
}
super.onDestroy();
}
}