When integrating Google Play Games Sign-in, I needed to get the player name for my app, but the getCurrentPlayer() task never completed (playerName is perpetually null). OnActivityResult is ran from a startActivityForResult with the intent from the getSignInIntent() method of a GoogleSignInClient.
Here's the relevant code from onActivityResult (playerName is Global):
#Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from signInClient
if (requestCode == SIGN_IN) {
Task<GoogleSignInAccount> resTask =
GoogleSignIn.getSignedInAccountFromIntent(data);
if (resTask.isSuccessful()) {
GoogleSignInAccount resAcct = resTask.getResult();
PlayersClient playersClient = Games.getPlayersClient(this, resAcct);
playersClient.getCurrentPlayer()
.addOnSuccessListener(new OnSuccessListener<Player>() {
#Override
public void onSuccess(Player taskPlayer) {
playerName = taskPlayer.getName();
}
});
while(playerName==null){
Log.d("OH GOD WHY", "WHY???");
}
//Do something with the name
...
} else {
Toast.makeText(getApplicationContext(), "Unable to sign in. Please try again later.", Toast.LENGTH_SHORT).show();
}
}
}
Before using OnSuccess, I was using OnComplete, but that never ran either (the isSuccessful() check never ran here):
playersClient.getCurrentPlayer()
.addOnCompleteListener(new OnCompleteListener<Player>() {
#Override
public void onComplete(Task<Player> pTask) {
if(pTask.isSuccessful()){
Player player = pTask.getResult();
playerName = player.getName();
} else {
//Handle error
...
}
}
});
Related
So I have the zxing barcode scanner running and in my main activity I have the onResultActivity function telling my activity to push to a new activity with a result from the scanner.
The problem is that my scanner just scans any old QR code regardless of what it is.
I need the scanner to only accept my QR code to pass a successful result and ignore all other QR codes (this should pass a toaster to say "incorrect QR code, try again").
Here's what I currently have:
MainActivity
...
static final int SCAN_RESULT = 1; // The request code
...
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Check which request we're responding to
if (requestCode == SCAN_RESULT) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// Action to take if result successful
Intent intent = new Intent(this, ResultActivity.class);
startActivity(intent);
}
}
}
ScannerActivity
...
public class ScanBarcodeActivity extends AppCompatActivity {
Button mBtnClose;
private CaptureManager capture;
private DecoratedBarcodeView barcodeScannerView;
private ViewfinderView viewfinderView;
private void initViews() {
mBtnClose = findViewById(R.id.barcode_header_close);
barcodeScannerView = findViewById(R.id.zxing_barcode_scanner);
viewfinderView = findViewById(R.id.zxing_viewfinder_view);
}
private void initListener() {
mBtnClose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_barcode);
initViews();
initListener();
capture = new CaptureManager(this, barcodeScannerView);
capture.initializeFromIntent(getIntent(), savedInstanceState);
capture.decode();
changeMaskColor(null);
}
#Override
protected void onResume() {
super.onResume();
capture.onResume();
}
#Override
protected void onPause() {
super.onPause();
capture.onPause();
}
#Override
protected void onDestroy() {
super.onDestroy();
capture.onDestroy();
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
capture.onSaveInstanceState(outState);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return barcodeScannerView.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
}
public void changeMaskColor(View view) {
}
}
EDIT: I've tried this but it's obviously not working, this is basically what I'm looking to get working. If the SCAN_RESULT = the QR_CODE then go to next activity, else pop a message saying try again.
static final int SCAN_RESULT = 1; // The request code
String QR_CODE = "EC0111-1234567899";
int RESULT = Integer.parseInt(QR_CODE);
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Check which request we're responding to
if (requestCode == SCAN_RESULT) {
// Make sure the request was successful
if (SCAN_RESULT == RESULT) {
Intent intent = new Intent(this, ResultActivity.class);
startActivity(intent);
} else {
Toast.makeText(this, "Incorrect QR code, please try again", Toast.LENGTH_LONG).show();
}
}
}
There are some approaches you can try.
Encrypt information: You can encrypt information coded in QR so that other can't read it as well as you can identify your own QR. To do so
Encrypt information with a key
Generate QR with encrypted information
Read and try to decrypt information. If you can decrypt than it's your QR.
Develop your own QR: It may be costly for you but it is a wonderful idea to generate your own styled QR like facebook messenger , snapchat and whatsapp etc. In that case you can't use standard ZXING library. You have to customised ZXING library or develop a new one.
Add tag to information: You can add a unique tag(text) in your QR information. By which you can identify your QR code.
So I'm trying to build a QR Code Scanner, but my result only showing a text. Even the QR Code is have a URL link on it. It can't be open to a browser.
Here's my code for the scanner:
public class ReaderActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reader);
Button scan_btn = findViewById(R.id.scan_btn);
final Activity activity = this;
scan_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
IntentIntegrator integrator = new IntentIntegrator(activity);
integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
integrator.setPrompt("Scan");
integrator.setCameraId(0);
integrator.setBeepEnabled(false);
integrator.setBarcodeImageEnabled(false);
integrator.initiateScan();
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result != null) {
if (result.getContents() == null) {
Toast.makeText(this, "You cancelled the scanning", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, result.getContents(), Toast.LENGTH_LONG).show();
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}}
What should I add or change to get clickable URL result? Can someone fix this? Please help me to get my project done. I really need it.
You will get QR results on result.getContents() as soon as you get it you should have to use action intent for view to open URL in browser
Im creating an intent to an activity by clicking on a button which should open google places, but it closes again really fast and says no location selected, and returns to the main activity, and then nothing happens if i click again.
My api should be fine, I have checked that it's the correct SHA1-fingerprint thats connected to the api key.
The result code is 2
It worked earlier in the activity before this one, but I needed it to open when i click on a button instead, and now when I try to open this new activity as an intent it wont work.
public class MapActivity extends AppCompatActivity {
int PLACE_PICKER_REQUEST = 1;
int status;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_events);
status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (status != ConnectionResult.SUCCESS) {
if (GooglePlayServicesUtil.isUserRecoverableError(status)) {
GooglePlayServicesUtil.getErrorDialog(status, this,
100).show();
}
}
if (status == ConnectionResult.SUCCESS) {
int PLACE_PICKER_REQUEST = 199;
PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
Context context = this;
try {
startActivityForResult(builder.build(context), PLACE_PICKER_REQUEST);
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
System.out.println("Result code: " + resultCode);
System.out.println("Request code: " + requestCode);
if (requestCode == 100) {
status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
}
if (requestCode == 199) {
//process Intent......
if (data != null) {
Place place = PlacePicker.getPlace(data, this);
String toastMsg = String.format("Place: %s", place.getName());
Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show();
} else {
String toastMsg = ("No location selected.");
Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show();
}
}
}
}
This is from the intent which create the new intent to maps
public void onClick(View view) {
Intent i = new Intent(this, MapActivity.class);
startActivity(i);
}
I think its happening because of you are using old method of getPlace
try to swap the arguments, by changing it from:
Place place = PlacePicker.getPlace(data, this);
to
Place place = PlacePicker.getPlace(getContext(), data);
Update #2
Enable Google places API in the developer console and add these lines to AndroidManifest
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="ADD_YOUR_API_KEY_HERE" />
Update #3
after some search, it looks like there is others having same issue. Look at these links:
https://github.com/zhangtaii/react-native-google-place-picker/issues/21
https://stackoverflow.com/a/32751164/
https://github.com/googlesamples/android-play-places/issues/13
I have fragment listener implemented in activity and im initializing it in onAttach() method. In fragment i have been making asynchronous calls and also starting another activity for result. After onActivityResult is called and process the request code then im calling listener implementation in the activity but listener throws NPE always. im very confused to know how the listener variable becomes null after it get results from another activity.
any idea on how its happening?
class ReviewFragment extends Fragment {
private OnReviewContinueButtonClickListener mListener;
/* many other variables */
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == REQUEST_CODE_INSTAMOJO_PAYMENT) {
mListener.onContinueButtonClick(mailId);
showSuccessScreen(mailId);
}
if (requestCode == PaytmWalletManager.REQUEST_CODE_REGISTER_PAYTM_WALLET || requestCode == PaytmWalletManager.REQUEST_CODE_ADD_MONEY) {
initiatePaytmPayment();
}
}
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnReviewContinueButtonClickListener) {
mListener = (OnReviewContinueButtonClickListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnReviewContinueButtonClickListener");
}
}
/* im calling this function middle of fragment from asynchronous network call onsuccess method without runonuithread() */
private void pay(JSONObject paymentObject) throws JSONException {
Intent payment = new Intent(getActivity().getApplicationContext(), WebviewActivity.class);
Bundle bundle = new Bundle(3);
bundle.putString(WebviewActivity.TITLE, "Make Payment");
startActivityForResult(payment, REQUEST_CODE_INSTAMOJO_PAYMENT);
hideProgressDialog();
}
// on calling this method from asynchronous network result onSuccess()
private void registerForFree(String orderId, String paymentId) {
AppEventTracker.logEvent("register for free clicked", "success", "Flight Booking");
getApp().getAccountManager().registerFreeTicketPatchCall(orderId, paymentId, getFreeTicketRegisterBody(), new Callback<JsonElement>() {
#Override
public void success(JsonElement jsonElement, Response response) {
if (getActivity() == null) return;
hideProgressDialog();
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
mListener.onContinueButtonClick(mailId); // mlistener is null here even though it has been initialized on onAttach().
}
});
}
#Override
public void failure(RetrofitError error) {
if (getActivity() == null) return;
hideProgressDialog();
if (showErrorDialog) {
showErrorDialog = false;
showPriceChangeDiaglog("Error in payment generation");
}
AppEventTracker.logEvent("register for free clicked", "failure", "Explara buy Ticket");
}
});
}
Activity which implements above fragment is
class booking extends Activity implements ReviewFragment.OnReviewContinueButtonClickListener{
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == PaytmWalletManager.REQUEST_CODE_REGISTER_PAYTM_WALLET || requestCode == PaytmWalletManager.REQUEST_CODE_ADD_MONEY) {
ReviewItineraryFragment fragment = (ReviewItineraryFragment) getFragmentManager().findFragmentByTag("ReviewItinerary");
fragment.initiatePaytmPayment();
}
}
}
#Override
public void onContinueButtonClick(String mail) {
showSuccessScreen(mail);
}
private void showSuccessScreen(String mail) {
if (this != null && !this.isFinishing()) {
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setTitle("Order Confirmation");
FragmentTransaction transaction = this.getFragmentManager().beginTransaction();
String email = mail;
String type = "Order";
transaction.replace(R.id.container, PaymentSuccess.newInstance(email, type));
transaction.addToBackStack(null);
transaction.commit();
}
}
}
I am getting the following error when I try to purchase an item again. Please dont close this as duplicate. I know there are many such questions in stack overflow, but none seem to help. Please refer my code.
public class BtnListener implements View.OnClickListener
{
// On-click event handler for all the buttons
#Override
public void onClick(View view)
{
switch (view.getId())
{
case R.id.TwoSeconds:
mHelper.launchPurchaseFlow(TimeBoosterActivity.this, ITEM_SKU, 10001,
mPurchaseFinishedListener, "2");
break;
}
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data)
{
if (!mHelper.handleActivityResult(requestCode,
resultCode, data)) {
super.onActivityResult(requestCode, resultCode, data);
}
}
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener
= new IabHelper.OnIabPurchaseFinishedListener() {
public void onIabPurchaseFinished(IabResult result,
Purchase purchase)
{
if (result.isFailure()) {
// Handle error
return;
}
else if (purchase.getSku().equals(ITEM_SKU)) {
consumeItem();
//buyButton.setEnabled(false);
}
}
};
public void consumeItem() {
mHelper.queryInventoryAsync(mReceivedInventoryListener);
}
IabHelper.QueryInventoryFinishedListener mReceivedInventoryListener
= new IabHelper.QueryInventoryFinishedListener() {
public void onQueryInventoryFinished(IabResult result,
Inventory inventory) {
if (result.isFailure()) {
Toast.makeText(getApplicationContext(), "Failed to consume item",
Toast.LENGTH_SHORT).show();
} else {
mHelper.consumeAsync(inventory.getPurchase(ITEM_SKU),
mConsumeFinishedListener);
}
}
};
IabHelper.OnConsumeFinishedListener mConsumeFinishedListener =
new IabHelper.OnConsumeFinishedListener() {
public void onConsumeFinished(Purchase purchase,
IabResult result) {
if (result.isSuccess()) {
String getSeconds=purchase.getDeveloperPayload();
SharedPreferences saveTwoSeconds=getSharedPreferences(getSeconds, Context.MODE_PRIVATE);
//clickButton.setEnabled(true);
} else {
// handle error
Toast.makeText(getApplicationContext(), "Failed to consume item",
Toast.LENGTH_SHORT).show();
}
}
};
#Override
public void onDestroy() {
super.onDestroy();
if (mHelper != null) mHelper.dispose();
mHelper = null;
}
I think your Consume purchase is not being called due to your code in onActivityResult.
Try with below code
#Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data){
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == <Code you sent during launching purchase flow> || requestCode == <Code you sent during launching consume flow> )
mHelper.handleActivityResult(requestCode,resultCode, data))
}
}
First of all, you need to clear why you got this error (Error response: 7:Item Already Owned)
There are some common case:
① You just buy itemA and not consume it yet;
② Another person (use the same google account with you) just buy this itemA
In case 1: Just consume itemA and you will be able to buy it again.
In case 2: You need to reboot or delete google play service and play store cache. Then use the same google play account consume that itemA.
in case 2, receipt information of itemA is not stored in your device. You reboot or delete google play service and play store cache to make in-app-billing request to google store to get receipt information of purchase. So you cant consume and buy it again.