i am attempting to implement a built in controller that is part of the scoreloop library. the documentation states:
Basic Usage:
To invoke the TOS dialog if it was not accepted previously, the following code may be used:
final TermsOfServiceController controller = new TermsOfServiceController(new TermsOfServiceControllerObserver() {
#Override
public void termsOfServiceControllerDidFinish(final TermsOfServiceController controller, final Boolean accepted) {
if(accepted != null) {
// we have conclusive result.
if(accepted) {
// user did accept
}
else {
// user did reject
}
}
}
});
controller.query(activity);
but when i paste this into my code i get the following syntax errors:
am i using this incorrectly? how and where would this be used any ideas?
EDIT: after moving the statement to the method where i want to show the dialog i now get the following error:
You seem to be calling controller.query(activity) in a class body where a declaration is expected. Move the statement controller.query(activity) to a method where you would like to show the dialog.
Related
Last week I started learning Android as I needed to create an application for one of the projects at Uni.
The application is a simple barcode/QRcode scanner and it should scan the code, compare its result with the database (I'm using Firebase) and either return other data from database if the barcode is found or ask the user if he wants to add the barcode to the database if it's not found.
I thought the easiest way to do it would be to use AlertDialog, but the app crashes every single time I scan the code.
I debugged the app and checked the Logcat, what I get is:
You need to use a Theme.AppCompat theme (or descendant) with this activity.
This is exactly where I get the error and where I wanted to use AlertDialog - based on the value in the variable details.
private BarcodeCallback callback = new BarcodeCallback() {
#Override
public void barcodeResult(final BarcodeResult result) {
barcodeView.decodeSingle(callback);
dbRef.child("Items").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Iterator<DataSnapshot> item = dataSnapshot.getChildren().iterator();
Boolean isFound = false;
while (!isFound || item == null) {
DataSnapshot i = item.next();
String check = i.child("ID").getValue().toString();
if (result.getText().equals(check)) {
isFound = true;
details = "Consumption: " + i.child("Consumption").getValue().toString()
+ "\nCost: " + i.child("Cost").getValue().toString()
+ "\nName: " + i.child("Name").getValue().toString();
} else {
details = "Not found";
}
}
new AlertDialog.Builder(getApplicationContext())
.setMessage("This is just an example for the purpose of the question.")
.create()
.show();
}
I get the error exactly on the line with .show();.
In the previous posts I found that you can't display AlertDialog in this place, and you need to use runOnUiThread function or Handler, none of those options worked for me, and I was getting the error in the same place.
Do you guys have any advice or suggestions?
Also, I'm sorry for the way this post looks like or for any missing but required information. I know it's not an excuse, but this is my first post here.
Thanks in advance for any replies.
The problem is here:
new AlertDialog.Builder(getApplicationContext())
You can't build a Dialog using the application context. To reach this you need an Activity Context.
Read this question or this article for further understanding
Please assist in this. I can't seem to create a suitable test for this method:
protected void startInterfacing() {
mLiveAuthClient.login(mView.context(), Arrays.asList(SCOPES), new LiveAuthListener() {
#Override
public void onAuthComplete(final LiveStatus liveStatus, final LiveConnectSession liveConnectSession,
Object o) {
// Login successful and user consented, now retrieve user ID and connect with backend server
getUserIdAndConnectWithBackendServer(liveConnectSession, mLiveAuthClient);
}
#Override
public void onAuthError(LiveAuthException e, Object o) {
// We failed to authenticate with auth service... show error
if (e.getError().equals("access_denied") ||
e.getMessage().equals("The user cancelled the login operation.")) {
// When user cancels in either the login or consent page, we need to log the user out to enable
// the login screen again when trying to connect later on
logUserOut(mLiveAuthClient, false);
} else {
onErrorOccured();
}
}
});
}
I'll explain abit what goes on here:
I'm trying to authenticate my client and log into OneDrive.
The method starts with a call to the Live SDK's login method. That SDK object is given to me from outside this class. So I can basically mock it.
Here's what I'm struggling with:
I do not need to test the call to the login method because it is not mine. I do need to test the call to getUserIdAndConnectWithBackendServer() inside onAuthComplete. But this method requires a liveConnectSession object. How do I provide that? It is given to me on the onAuthComplete method.
How do I mock the calls to onAuthComplete and onAuthError? I read about ArgumentCaptor but when I use that, I need to provide the arguments to those methods when I call the actual method.
For instance, argument.getValue().onAuthComplete() requires me to add arguments to this call. What do I actually provide here?
Here is the next method which is roughly the same but has its own issues:
protected void getUserIdAndConnectWithBackendServer(final LiveConnectSession liveConnectSession, final LiveAuthClient
authClient) {
final LiveConnectClient connectClient = new LiveConnectClient(liveConnectSession);
connectClient.getAsync("me", new LiveOperationListener() {
#Override
public void onComplete(LiveOperation liveOperation) {
// We got a result. Check for errors...
JSONObject result = liveOperation.getResult();
if (result.has(ERROR)) {
JSONObject error = result.optJSONObject(ERROR);
String code = error.optString(CODE);
String message = error.optString(MESSAGE);
onErrorOccured();
} else {
connectWithBackend(result, liveConnectSession, authClient);
}
}
#Override
public void onError(LiveOperationException e, LiveOperation liveOperation) {
// We failed to retrieve user information.... show error
onErrorOccured();
logUserOut(authClient, false);
}
});
}
In here I would like to mock the JSONObject for instance. But how do I call the onComplete method, or the onError method. And what would I provide as the arguments the methods provide me with. LiveOperation for instance?
Thank you!!
The solution I eventually used was to use mockito's doAnswer() structure.
This enabled me to get the callback argument and call one of its methods.
Another solution was to use an ArgumentCator.
I would like to integrate Braintree API into my android application.I referred to Braintree page and I got an idea for how we can integrate it into application. But I have an issue when I want to display Drop-In UI below of my currently showing activity's layout. But in demo it will start new activity BraintreePaymentActivity.java.
I don't want to open new activity I just want to show the same operation in my activity. For that I refer Card-form demo and added my custom button for Purchase.And on Purchase button click I call below code. But here I don't understand from where I can get Nonce value?
Braintree.setup ( this, CLIENT_TOKEN_FROM_SERVER, new Braintree.BraintreeSetupFinishedListener () {
#Override
public void onBraintreeSetupFinished ( boolean setupSuccessful, Braintree braintree, String errorMessage, Exception exception ) {
if ( setupSuccessful ) {
// braintree is now setup and available for use
} else {
// Braintree could not be initialized, check errors and try again
// This is usually a result of a network connectivity error
}
}} );
If anyone has any idea about that then please suggest here.
I am stuck with Braintree API.
Thanks in advance.
You are correct that your use case does not fit for the Drop-in UI.
In order to add a PaymentMethodNonce listener, once Braintree is setup, simply call Braintree.addListener and supply a Braintree.PaymentMethodNonceListener implementation. I've included an example below. You can also refer to the client side integration section in the Credit Cards guide in Braintree's documentation.
Braintree.setup (this, CLIENT_TOKEN_FROM_SERVER, new Braintree.BraintreeSetupFinishedListener () {
#Override
public void onBraintreeSetupFinished ( boolean setupSuccessful, Braintree braintree, String errorMessage, Exception exception) {
if (setupSuccessful) {
// braintree is now setup and available for use
braintree.addListener(new Braintree.PaymentMethodNonceListener() {
public void onPaymentMethodNonce(String paymentMethodNonce) {
// Communicate the nonce to your server
}
});
} else {
// Braintree could not be initialized, check errors and try again
// This is usually a result of a network connectivity error
}
}
});
I need to implement quite popular template of app behaviour - give opportunity to user to retry failed requests. Right now I catch failed request with SpiceServiceListener, and shows dialog where user can press "Retry" button. Unfortunately, using the same CachedSpiceRequest object with SpiceManager.execute() don't give desired behaviour, because RS removing all request listeners from mapRequestToLaunchToRequestListener if request wasn't successful. So request can work fine, but it will not return any information to my Activity.
Is there easy way (without modifying code of library) to implement this?
Unfortunately looks like there are no abstract solution for situation like this, so I had to add code like this in every request.
getSpiceManager().execute(r, new RequestListener<CountProfiles>() {
#Override
public void onRequestFailure(SpiceException spiceException) {
if (act.getSupportFragmentManager().findFragmentByTag("network_problem") == null) {
NetworkProblemDialogFragm.newInstance(r, this).show(act.getSupportFragmentManager(), "network_problem");
} else {
((NetworkProblemDialogFragm) act.getSupportFragmentManager().findFragmentByTag("network_problem")).setSpiceRequest(r);
((NetworkProblemDialogFragm) act.getSupportFragmentManager().findFragmentByTag("network_problem")).setRequestListener(this);
}
}
#Override
public void onRequestSuccess(CountProfiles countProfiles) {
}
});
NetworkProblemDialogFragm is a DialogFragment with Retry button, on click on this button I re execute failed request, using given RequestListener.
Not very beautiful solution, but looks like there no better one.
Hi I am running ADMMessenger sample application provided with SDK.
in which I am not able to get Registration ID in register() method of MainActivity.
Method is like this.
private void register()
{
final ADM adm = new ADM(this);
if (adm.isSupported())
{
if(adm.getRegistrationId() == null)
{
adm.startRegister();
} else {
// final MyServerMsgHandler srv = new MyServerMsgHandler();
// srv.registerAppInstance(getApplicationContext(), adm.getRegistrationId());
}
Log.v("log_tag","Reg_id:: "+adm.getRegistrationId());
}
}
in Log cat I am always getting Reg_id:: null
and onRegistrationError() method of SampleADMMessageHandler is calling.
and error at there is ERROR_SERVICE_NOT_AVAILABLE
I can not understand what is problem, please help me.
For the service to work correctly you need to be using the Kindle image (not a generic Android one) and also make sure you have logged into your account on the device (pull down the status bar at the top and ensure you have selected an account)