Android billing consuming items - java

I am trying to have an consumable item for my app.
i currently use this code:
private void BuyManager() {
mHelper.flagEndAsync();
mHelper.launchPurchaseFlow(this, "jm.admin", 1011, new IabHelper.OnIabPurchaseFinishedListener() {
#Override
public void onIabPurchaseFinished(IabResult result, Purchase info) {
AlertDialog.Builder succces = new AlertDialog.Builder(MainActivity.this);
int proccesed = 1;
try {
proccesed=mservice.consumePurchase(3,getPackageName(), "inapp:"+getPackageName()+":jm.admin");
} catch (RemoteException e) {
e.printStackTrace();
}
String succs = "nee";
if (proccesed==0){
succs = "ja";
}
succces.setMessage("We checken uw aankoop Aankoop gegevens: " + result + " Aankoop succesvol: "+succs);
succces.setPositiveButton("Oke", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
succces.show();
if (result.isSuccess()) {
AlertDialog.Builder succces2 = new AlertDialog.Builder(MainActivity.this);
succces2.setMessage("Uw aankoop is voltooid");
succces2.setPositiveButton("Oke", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
succces2.show();
}
}
});
}
but when i buy the item it not consumed at all and it haves the code 7 response (item already bought).
Whats wrong with my code?

Related

How to clear app cache on exit pressed button?

I just want to clear app cache on exit when pressed, tried many answers out there but got errors and didn't work, so here is my code:
if (Config.ENABLE_EXIT_DIALOG) {
AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
dialog.setIcon(R.mipmap.ic_launcher);
dialog.setTitle(R.string.app_name);
dialog.setMessage(R.string.dialog_close_msg);
dialog.setPositiveButton(R.string.dialog_option_yes, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
MainActivity.this.finish();
}
});
dialog.setNegativeButton(R.string.dialog_option_rate_us, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
final String appName = getPackageName();
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appName)));
} catch (android.content.ActivityNotFoundException anfe) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + appName)));
}
MainActivity.this.finish();
}
});
dialog.setNeutralButton(R.string.dialog_option_more, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.play_more_apps))));
MainActivity.this.finish();
}
});
dialog.show();
} else {
super.onBackPressed();
}
got solved, i think it's old way but it's working fine found it here
i actually used his question code at the end of the my mainActivity file and it worked like a charm, though i guess it's an old way but it's ok.
and here is the code
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
clearApplicationData();
}
public void clearApplicationData() {
File cache = getCacheDir();
File appDir = new File(cache.getParent());
if (appDir.exists()) {
String[] children = appDir.list();
for (String s : children) {
if (!s.equals("lib")) {
deleteDir(new File(appDir, s));
Log.i("EEEEEERRRRRRROOOOOOORRRR", "**************** File /data/data/APP_PACKAGE/" + s + " DELETED *******************");
}
}
}
}
public static boolean deleteDir(File dir) {
if (dir != null && dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
return dir.delete();
}

Data from Google Drive's Appdata folder is disappearing

I'm trying to create Spreadsheet file in Google Drive's Appdata folder for my aplication, to use it as a remote database.
Just at the beggining and I'm stuck already. Files are created properly(?), but they seems to be dissapearing after a moment.
I'm doing it like this:
First, on onCreate method I log into my Google Account:
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestScopes(Drive.SCOPE_APPFOLDER)
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, this)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.addApi(Drive.API)
.addConnectionCallbacks(this)
.build();
signIn();
private void signIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
onActivityResult handles updating UI(displaying user's name and profile pic, all works great on the login/logout side)
Then, in onConnected(Bundle bundle) method, I try to find out if user already has existing database in AppFolder, if not, I'm asking him to create one.
#Override
public void onConnected(Bundle bundle) {
queryDrive();
}
public void queryDrive() {
Query query = new Query.Builder().addFilter(Filters.eq(SearchableField.TITLE, DreamDbSchema.NAME_REMOTE)).build();
Drive.DriveApi.query(mGoogleApiClient, query).setResultCallback(new ResultCallback<DriveApi.MetadataBufferResult>() {
#Override
public void onResult(DriveApi.MetadataBufferResult metadataBufferResult) {
MetadataBuffer buffer = metadataBufferResult.getMetadataBuffer();
if (buffer != null) {
if (buffer.getCount() == 0) {
new AlertDialog.Builder(ListActivity.this).setTitle(R.string.no_database_on_gdrive)
.setMessage(R.string.no_database_on_gdrive_message)
.setCancelable(false)
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Drive.DriveApi.newDriveContents(mGoogleApiClient).setResultCallback(driveContentsCallback);
}
})
.setNeutralButton(R.string.cancel_and_disconnect, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
signOut();
}
})
.show();
} else {
for (int i = 0; i < buffer.getCount(); i++) {
Log.d("DRIVE_FILES: ", buffer.get(i).getTitle());
}
}
} else {
Log.d("DRIVE_BUFFER: ", "null");
}
}
});
}
final private ResultCallback<DriveApi.DriveContentsResult> driveContentsCallback =
new ResultCallback<DriveApi.DriveContentsResult>() {
#Override
public void onResult(DriveApi.DriveContentsResult result) {
if (!result.getStatus().isSuccess()) {
Toast.makeText(ListActivity.this, R.string.error_while_creating_GDrive_file, Toast.LENGTH_SHORT).show();
return;
}
MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
.setTitle(DreamDbSchema.NAME_REMOTE)
.setMimeType("vnd.google-apps.spreadsheet")
.build();
Drive.DriveApi.getAppFolder(mGoogleApiClient)
.createFile(mGoogleApiClient, changeSet, result.getDriveContents())
.setResultCallback(fileCallback);
}
};
final private ResultCallback<DriveFolder.DriveFileResult> fileCallback = new
ResultCallback<DriveFolder.DriveFileResult>() {
#Override
public void onResult(DriveFolder.DriveFileResult result) {
if (!result.getStatus().isSuccess()) {
Toast.makeText(ListActivity.this, R.string.error_while_creating_GDrive_file, Toast.LENGTH_SHORT).show();
return;
}
Toast.makeText(ListActivity.this, getString(R.string.database_created_GD), Toast.LENGTH_SHORT).show();
}
};
Everything seems to be working good for about 10 minutes. In another activity I can successfully remove this file like this:
#Override
public void onClick(View v) {
if (!mGoogleApiClient.isConnected()) {
Toast.makeText(SettingsActivity.this, R.string.not_loged_in, Toast.LENGTH_SHORT).show();
} else {
Drive.DriveApi.getAppFolder(mGoogleApiClient).listChildren(mGoogleApiClient).setResultCallback(new ResultCallback<DriveApi.MetadataBufferResult>() {
#Override
public void onResult(final DriveApi.MetadataBufferResult metadataBufferResult) {
Log.d("DRIVE_FILES: ", String.valueOf(metadataBufferResult.getMetadataBuffer().getCount()));
for (int i = 0; i < metadataBufferResult.getMetadataBuffer().getCount(); i++) {
final int j = i;
DriveId fileId = metadataBufferResult.getMetadataBuffer().get(i).getDriveId();
Drive.DriveApi.getFile(mGoogleApiClient, fileId).delete(mGoogleApiClient).setResultCallback(new ResultCallback<Status>() {
#Override
public void onResult(Status status) {
if (status.isSuccess()) {
Toast.makeText(SettingsActivity.this, R.string.database_removed_GD, Toast.LENGTH_SHORT).show();
}
}
});
}
}
});
}
}
and it also prints in log "1" for a while. But give it a ten minutes or so, and I'm once again prompted to create database, because it finds Appdata folder to be empty.

Remember checkbox within a dialog

I try to remember the checkbox within a dialogue but in comparison remember == false the variable remember still has not changed its value. In the loop is iterated as many times as the size of lelements_tmp before call showDialogSameFile() so neither can I use the variable remember within the loop. How can I do it? Thanks in advance.
private boolean remember = false;
// in the program
// add files to List lelements_tmp
while (i < lelements_tmp.size()) {
File fto = new File(lelements_tmp.get(i).getFile());
String to = null;
try {
to = fto.getCanonicalPath();
} catch (IOException e) {
e.printStackTrace();
}
if (fto.exists()) {
showDialogSameFile(fto, to, i);
i++;
continue;
}
thread(i);
i++;
}
private void showDialogSameFile(File f, String to, final int i) {
TextView title = null;
if (f.isDirectory())
title = this.getTitle("The directory " + to + " already exists. Do you want to replace the contents?");
else if (f.isFile())
title = this.getTitle("The file " + to + " already exists. Do you want to replace it?");
String[] item = {"Apply to all"};
AlertDialog ad = new AlertDialog.Builder(context)
.setCustomTitle(title)
.setMultiChoiceItems(item, null,
new DialogInterface.OnMultiChoiceClickListener() {
#Override
public void onClick(DialogInterface dialog, int indexSelected, boolean isChecked) {
if (isChecked)
remember = true;
}
})
.setPositiveButton("Aceptar",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
thread(i);
}
})
.setNegativeButton("Cancelar",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
})
.create();
if (remember == false)
ad.show();
}
I've had to refactor the code a bit, but basically with this work. It's necessary to call to the method from non-UI thread. Based on this response https://stackoverflow.com/a/11369224/5089855
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import java.util.concurrent.Semaphore;
public class Proof {
private Semaphore mutex = new Semaphore(0, true);
private boolean remember = false;
private Activity context;
public Proof(Activity context) {
this.context = context;
}
private boolean showDialogMismoArchivo() {
context.runOnUiThread(new Runnable() {
public void run() {
String[] item = {"Apply all"};
AlertDialog ad = new AlertDialog.Builder(context)
.setTitle("¿Reemplazar?")
.setMultiChoiceItems(item, null,
new DialogInterface.OnMultiChoiceClickListener() {
#Override
public void onClick(DialogInterface dialog, int indexSelected, boolean isChecked) {
if (isChecked)
remember = true;
else
remember = false;
}
})
.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
mutex.release();
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
mutex.release();
}
})
.create();
ad.show();
}
});
try {
mutex.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
return remember;
}
}

android alert box with multiple options, how to use those options?

I am a beginner, so if anyone would help me out. I created a list in the dialogue box , now how do i use those options? Like click one and it does something , click another and it does something else.
CharSequence features[] = new CharSequence[] {"Save", "Send", "Something", "Something"};
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
alertDialog.setTitle("Options");
alertDialog.setItems(features, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this,"Eta chu ma aile",
Toast.LENGTH_LONG).show();
}
});
alertDialog.show();
return true;
}
If you know exact position of every item, just compare it with which param.
public void onClick(DialogInterface dialog, int which) {
if (which == 0) {
// handle "Save" option
} else if (which == 1) {
// handle "Send" option
} ...
}
You can use following code:
Somewhere in another function:
String title = "My Alert Box";
String msg = "Choose Option";
alertfunc(title,msg);
The main alert function:
private void alertfunc(String title, String msg) {
if (title.equals(TASK_VIEW_PROFILE)) {
new AlertDialog.Builder(MainActivity.this)
.setTitle(title)
.setMessage(msg)
.setPositiveButton("Save",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
//Do something
}
})
.setNegativeButton("Send",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which)
{
//Do something
}
}).create().show();
.setNegativeButton("Something",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which)
{
//Do something
}
}).create().show();
//...and so on
}
}

Dialog only first option works

I'm trying to make a SingleChoiceItems dialog activated from a listview onItemLongCLick which has 3 options via radiobuttons 1.View Profile 2.Send Message 3.Delete Friends and from the way I have it set up,only the first option toast works on all 3 options.
How would I make it that options 2 and 3 does their actions instead of doing option 1's actions?
Here is the code to my dialog,
#Override
public Dialog onCreateDialog(int id) {
String[] items = { "View Profile", "Send Message", "Remove Friend" };
final DBAdapter db = new DBAdapter(this);
db.open();
switch (id) {
case 0:
return new AlertDialog.Builder(this)
.setIcon(R.drawable.icon)
.setTitle("Select a option")
.setSingleChoiceItems(items, id,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
int choice = 0;
// TODO Auto-generated method stub
mChoice = choice;
}
})
.setPositiveButton("OK", new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int choice) {
// TODO Auto-generated method stub
if (mChoice == 0) {
Toast.makeText(getBaseContext(), "Test 1",
Toast.LENGTH_SHORT).show();
} else if (mChoice == 1) {
Toast.makeText(getBaseContext(), "Test2",
Toast.LENGTH_SHORT).show();
} else if (mChoice == 2) {
TextView friends = (TextView) findViewById(R.id.textview_friends);
String deletedfriend = friends.getText()
.toString();
db.DeleteFriends(deletedfriend);
Toast.makeText(getBaseContext(),
"Friend Removed", Toast.LENGTH_SHORT)
.show();
}
}
}
)
.setNegativeButton("Cancel", new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int choice) {
}
})
.create();
}
return null;
}
Remove choice = 0;
.setSingleChoiceItems(items, id,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
mChoice = which;
}
})

Categories