In most Android devices, the RecognitionService will be supplied by Google's native 'Now/Assistant' application.
Up until Android Oreo, I was able to query the languages supported by the Google Recognizer with the following simple code:
final Intent vrIntent = new Intent(RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS);
// vrIntent.setPackage("com.google.android.googlequicksearchbox");
getContext().sendOrderedBroadcast(vrIntent, null, new BroadcastReceiver() {
#Override
public void onReceive(final Context context, final Intent intent) {
// final Bundle bundle = intent.getExtras();
final Bundle bundle = getResultExtras(true);
if (bundle != null) {
if (bundle.containsKey(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES)) {
Log.i("TAG", "onReceive: EXTRA_SUPPORTED_LANGUAGES present");
final ArrayList<String> vrStringLocales = bundle.getStringArrayList(
RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES);
Log.i("TAG", "onReceive: EXTRA_SUPPORTED_LANGUAGES size: " + vrStringLocales.size());
} else {
Log.w("TAG", "onReceive: missing EXTRA_SUPPORTED_LANGUAGES");
}
} else {
Log.w("TAG", "onReceive: Bundle null");
}
}, null, 1234, null, null);
However, since 8.0+ the extra RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES is no longer contained in the response.
Before I attempt to file this as a bug, I wanted to firstly see if others could replicate - but also check if there has been an Ordered Broadcast behavioural change in API 26 I've somehow overlooked, which could be the cause of this.
Thanks in advance.
So, I could't replicate, but further to the comments, if you don't set the package name
vrIntent.setPackage("com.google.android.googlequicksearchbox");
then it fails, otherwise all works fine to me.
That's the basic activity I've used to test it.
package it.versionestabile.stackover001;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.speech.RecognizerIntent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import java.util.ArrayList;
import static java.security.AccessController.getContext;
/**
* https://stackoverflow.com/questions/48500077/recognizerintent-action-get-language-details-in-oreo
*/
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Intent vrIntent = new Intent(RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS);
vrIntent.setPackage("com.google.android.googlequicksearchbox");
PackageManager packageManager = getPackageManager();
for (PackageInfo packageInfo: packageManager.getInstalledPackages(0)) {
if (packageInfo.packageName.contains("com.google.android.googlequicksearchbox"))
Log.d("AAA", packageInfo.packageName + ", " + packageInfo.versionName);
}
this.sendOrderedBroadcast(vrIntent, null, new BroadcastReceiver() {
#Override
public void onReceive(final Context context, final Intent intent) {
// final Bundle bundle = intent.getExtras();
final Bundle bundle = getResultExtras(true);
if (bundle != null) {
if (bundle.containsKey(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES)) {
Log.i("TAG", "onReceive: EXTRA_SUPPORTED_LANGUAGES present");
final ArrayList<String> vrStringLocales = bundle.getStringArrayList(
RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES);
Log.i("TAG", "onReceive: EXTRA_SUPPORTED_LANGUAGES size: " + vrStringLocales.size());
} else {
Log.w("TAG", "onReceive: missing EXTRA_SUPPORTED_LANGUAGES");
}
} else {
Log.w("TAG", "onReceive: Bundle null");
}
}
}, null, 1234, null, null);
}
}
I've tested it both on Android Studio 2.3 and 3.0.1 and on emulator with API 26 and 27.
All works fine with the above code.
But if you comment out this line:
vrIntent.setPackage("com.google.android.googlequicksearchbox");
on Oreo it doesn't work.
And I still suggest to check the presence of Google Now with Package Manager in a way like this:
PackageManager packageManager = getPackageManager();
for (PackageInfo packageInfo: packageManager.getInstalledPackages(0)) {
if (packageInfo.packageName.contains("com.google.android.googlequicksearchbox"))
Log.d("AAA", packageInfo.packageName + ", " + packageInfo.versionName);
// TODO - set a boolean value to discriminate the precence of google now
}
In order to decide if you have the right version of Google Now.
Hope it helps!
Related
I am trying to implement oauth2 to enable users to login with Reddit. I have created my app on reddit with the appropriate redirect uri.
What I did:
A MainActivity with a login button. Clicking the login button, starts the authorization flow. To create the authorization request, we need to pass a pending intent that the library uses to call the appropriate component that we want it to call after authorization is successful.
Problem:
When the pending intent is made using an implicit intent (setting only action string while creating intent), the library gets a cancelled exception while invoking the pending intent. I have mentioned the action string in the intent filter for the MainActivity in manifest file also.
What I have tried:
1. I tried creating pending intent using an explicit intent (defining the activity class I want to open while creating intent), my activity's onStart is getting called with the correct intent.
2. I tried by directly invoking the pending intent (with implicit intent) from the activity itself and it got called successfully.
Observation:
1. If I use an older version of the library (v0.2.0), the pending intent with implicit intent works fine.
Current version of OpenId AppAuth library - 0.7.1
Tested on Android 9 (Pie) - OnePlus 3T
Below is my MainActivity.java
package com.prateekgrover.redditline;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.prateekgrover.redditline.services.RedditAuthService;
import net.openid.appauth.AuthState;
import net.openid.appauth.AuthorizationException;
import net.openid.appauth.AuthorizationRequest;
import net.openid.appauth.AuthorizationResponse;
import net.openid.appauth.AuthorizationService;
import net.openid.appauth.AuthorizationServiceConfiguration;
import net.openid.appauth.TokenRequest;
import net.openid.appauth.TokenResponse;
import java.util.UUID;
public class MainActivity extends AppCompatActivity {
private String USED_INTENT = "1";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button loginButton = findViewById(R.id.reddit_login);
loginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Intent intent = new Intent(MainActivity.this, RedditAuthService.class);
// startService(intent);
performRedditAuthAction(MainActivity.this, "com.prateekgrover.redditline.HANDLE_AUTHORIZATION_RESPONSE");
}
});
}
public void performRedditAuthAction(Context context, String actionRedirect) {
String uuid = UUID.randomUUID().toString();
AuthorizationServiceConfiguration serviceConfiguration = new AuthorizationServiceConfiguration(
Uri.parse("https://www.reddit.com/api/v1/authorize") /* auth endpoint */,
Uri.parse("https://www.reddit.com/api/v1/access_token") /* token endpoint */
);
String clientId = "<my client id>";
Uri redirectUri = Uri.parse("com.prateekgrover.redditline://oauth2callback");
AuthorizationRequest.Builder builder = new AuthorizationRequest.Builder(
serviceConfiguration,
clientId,
"code",
redirectUri
);
builder.setState(uuid);
builder.setScopes("identity", "mysubreddits", "read", "save", "submit", "subscribe", "vote");
AuthorizationRequest request = builder.build();
AuthorizationService authorizationService = new AuthorizationService(context);
String action = actionRedirect;
Intent postAuthorizationIntent = new Intent("com.prateekgrover.redditline.HANDLE_AUTHORIZATION_RESPONSE");
PendingIntent pendingIntent = PendingIntent.getActivity(this, request.hashCode(), postAuthorizationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
authorizationService.performAuthorizationRequest(request, pendingIntent);
}
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (intent != null && intent.getAction() != null) {
String action = intent.getAction();
switch (action) {
case "com.prateekgrover.redditline.HANDLE_AUTHORIZATION_RESPONSE":
redirectIntent(intent);
break;
default:
}
}
}
private void redirectIntent(#Nullable Intent intent) {
if (!intent.hasExtra(USED_INTENT)) {
handleAuthorizationResponse(intent);
intent.putExtra(USED_INTENT, true);
}
}
private void handleAuthorizationResponse(Intent intent) {
AuthorizationResponse response = AuthorizationResponse.fromIntent(intent);
AuthorizationException error = AuthorizationException.fromIntent(intent);
final AuthState authState = new AuthState(response, error);
if (response != null) {
AuthorizationService service = new AuthorizationService(this);
service.performTokenRequest(response.createTokenExchangeRequest(), new AuthorizationService.TokenResponseCallback() {
#Override
public void onTokenRequestCompleted(#Nullable TokenResponse tokenResponse, #Nullable AuthorizationException exception) {
if (exception != null) {
} else {
if (tokenResponse != null) {
authState.update(tokenResponse, exception);
System.out.println(tokenResponse.accessToken + " refresh_token " + tokenResponse.refreshToken);
}
}
}
});
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
#Override
protected void onStart() {
super.onStart();
Intent intent = getIntent();
if (intent != null && intent.getAction() != null) {
String action = intent.getAction();
switch (action) {
case "com.prateekgrover.redditline.HANDLE_AUTHORIZATION_RESPONSE":
redirectIntent(intent);
break;
default:
}
}
}
}
Manifest File:
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="com.prateekgrover.redditline.HANDLE_AUTHORIZATION_RESPONSE"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
Relevant parts of the library - mCompleteIntent is the PendingIntent that I sending to the library
private void extractState(Bundle state) {
if (state == null) {
Logger.warn("No stored state - unable to handle response");
finish();
return;
}
mAuthIntent = state.getParcelable(KEY_AUTH_INTENT);
mAuthorizationStarted = state.getBoolean(KEY_AUTHORIZATION_STARTED, false);
try {
String authRequestJson = state.getString(KEY_AUTH_REQUEST, null);
mAuthRequest = authRequestJson != null
? AuthorizationRequest.jsonDeserialize(authRequestJson)
: null;
} catch (JSONException ex) {
throw new IllegalStateException("Unable to deserialize authorization request", ex);
}
mCompleteIntent = state.getParcelable(KEY_COMPLETE_INTENT);
mCancelIntent = state.getParcelable(KEY_CANCEL_INTENT);
}
private void handleAuthorizationComplete() {
Uri responseUri = getIntent().getData();
Intent responseData = extractResponseData(responseUri);
if (responseData == null) {
Logger.error("Failed to extract OAuth2 response from redirect");
return;
}
responseData.setData(responseUri);
if (mCompleteIntent != null) {
Logger.debug("Authorization complete - invoking completion intent");
try {
mCompleteIntent.send(this, 0, responseData);
} catch (CanceledException ex) {
Logger.error("Failed to send completion intent", ex);
}
} else {
setResult(RESULT_OK, responseData);
}
}
In case anybody else stumbles upon this issue.
Use the example app within app-auth android github project.
Don't use Google CodeLabs app-auth example! The code from the question above is from Google CodeLabs, it is very old and no longer works (state at July 2020).
I did the same mistake, app-auth links codelabs on their own page/readme, so I started using codelabs code and ended up with lots of problems and errors.
The new app-auth version 0.7.x uses a json configuration file and the example app shows how to handle errors around pending intents etc. .
im trying to open a Gallery with Images/Videos from a specific Folder. I´m using this solution but im getting the error code below and nothing happens. I guess its something abot the Uri but i cant find a solution. Has anyone an Idea how to solve this? I also included "my" code.
03-15 16:30:53.733 21902-22775/de.comidos.fotoapp D/onScanCompleted: Scan completed: content://media/external/images/media/1730
03-15 16:30:53.752 21902-22775/de.comidos.fotoapp D/Instrumentation: checkStartActivityResult() : Intent { act=android.intent.action.VIEW dat=content://media/external/images/media/1730 launchParam=MultiScreenLaunchParams { mDisplayId=0 mFlags=0 } }
03-15 16:30:53.773 21902-22775/de.comidos.fotoapp W/Binder: Binder call failed.
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=content://media/external/images/media/1730 launchParam=MultiScreenLaunchParams { mDisplayId=0 mFlags=0 } }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1839)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1531)
at android.app.Activity.startActivityForResult(Activity.java:4389)
at android.app.Activity.startActivityForResult(Activity.java:4348)
at android.app.Activity.startActivity(Activity.java:4672)
at android.app.Activity.startActivity(Activity.java:4640)
at de.comidos.fotoapp.GalleryViewActivity.onScanCompleted(GalleryViewActivity.java:59)
at android.media.MediaScannerConnection$1.scanCompleted(MediaScannerConnection.java:55)
at android.media.IMediaScannerListener$Stub.onTransact(IMediaScannerListener.java:60)
at android.os.Binder.execTransact(Binder.java:573)
package de.comidos.fotoapp;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import java.io.File;
public class GalleryViewActivity extends Activity implements MediaScannerConnection.MediaScannerConnectionClient {
public String[] allFiles;
private String SCAN_PATH ;
private static final String FILE_TYPE = "*/*";
private MediaScannerConnection conn;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery);
File folder = new File(Environment.getExternalStorageDirectory().toString()+"/comidos/sent/");
allFiles = folder.list();
// uriAllFiles= new Uri[allFiles.length];
for(int i=0;i<allFiles.length;i++)
{
Log.d("all file path"+i, allFiles[i]+allFiles.length);
}
// Uri uri= Uri.fromFile(new File(Environment.getExternalStorageDirectory().toString()+"/yourfoldername/"+allFiles[0]));
SCAN_PATH= Environment.getExternalStorageDirectory().toString()+"/comidos/sent/"+allFiles[0];
Log.d("SCAN PATH", "Scan Path " + SCAN_PATH);
}
private void startScan()
{
Log.d("Connected","success"+conn);
if(conn!=null)
{
conn.disconnect();
}
conn = new MediaScannerConnection(this,this);
conn.connect();
}
#Override
public void onMediaScannerConnected() {
Log.d("onMediaScannerConnected","success"+conn);
conn.scanFile(SCAN_PATH, FILE_TYPE);
}
#Override
public void onScanCompleted(String path, Uri uri) {
try {
Log.d("onScanCompleted","Scan completed: "+uri );
if (uri != null)
{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(uri);
startActivity(intent);
}
} finally
{
conn.disconnect();
conn = null;
}
}
#Override
public void onResume(){
super.onResume();
startScan();
}
}
There is no activity on your device that supports ACTION_VIEW of a content Uri for whatever MIME type that content is. There is no requirement for an Android device to have an ACTION_VIEW activity for every possible piece of content.
Currently making an Android app and decided to integrate Paypal payment in it, so that the user is redirected to Paypal payment inside the app.
I found the Paypal SampleApp for what I want to do:
https://github.com/paypal/PayPal-Android-SDK.
Firstly, I tested it and it worked without errors with CONFIG_ENVIRONMENT set to PayPalConfiguration.ENVIRONMENT_NO_NETWORK, and with the default sandbox sample acc (usr: sample#buy.com, pass:123123123).
Then I made an account on developer.paypal.com in order to make the payment to my Paypal account (with CONFIG_ENVIRONMENT set to PayPalConfiguration.ENVIRONMENT_PRODUCTION). I created an app and used the provided Client ID from the developer portal in the SampleApp. By doing that, the payment isn't completed, and gives the error:
request failure with http statusCode:400,exception:Bad Request
request failed with server response:{"name":"DUPLICATE_REQUEST_ID","debug_id":"9f83f9a8ce3e5","message":"The value of PayPal-Request-Id header has already been used","information_link":"https://developer.paypal.com/webapps/developer/docs/api/","details":[]}
DUPLICATE_REQUEST_ID
Note: I'm only using the "BUY A THING" option in the Sample App; not interested in the other options
The code of the class that handles the payment process:
import com.paypal.android.sdk.payments.PayPalAuthorization;
import com.paypal.android.sdk.payments.PayPalConfiguration;
import com.paypal.android.sdk.payments.PayPalFuturePaymentActivity;
import com.paypal.android.sdk.payments.PayPalItem;
import com.paypal.android.sdk.payments.PayPalOAuthScopes;
import com.paypal.android.sdk.payments.PayPalPayment;
import com.paypal.android.sdk.payments.PayPalPaymentDetails;
import com.paypal.android.sdk.payments.PayPalProfileSharingActivity;
import com.paypal.android.sdk.payments.PayPalService;
import com.paypal.android.sdk.payments.PaymentConfirmation;
import com.paypal.android.sdk.payments.ShippingAddress;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import org.json.JSONException;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class PaymentActivity extends Activity{
private static final String TAG = "PAYMENT";
private static final String CONFIG_ENVIRONMENT = PayPalConfiguration.ENVIRONMENT_PRODUCTION;
private static final String CONFIG_CLIENT_ID = "i changed this field with my client id";
private static final int REQUEST_CODE_PAYMENT = 1;
private static final int REQUEST_CODE_FUTURE_PAYMENT = 2;
private static final int REQUEST_CODE_PROFILE_SHARING = 3;
private static PayPalConfiguration config = new PayPalConfiguration()
.environment(CONFIG_ENVIRONMENT)
.clientId(CONFIG_CLIENT_ID)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_payment);
Intent intent = new Intent(this, PayPalService.class);
intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);
startService(intent);
}
public void onBuyPressed(View pressed) {
PayPalPayment thingToBuy = getThingToBuy(PayPalPayment.PAYMENT_INTENT_SALE);
Intent intent = new Intent(PaymentActivity.this, com.paypal.android.sdk.payments.PaymentActivity.class);
// send the same configuration for restart resiliency
intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);
intent.putExtra(com.paypal.android.sdk.payments.PaymentActivity.EXTRA_PAYMENT, thingToBuy);
startActivityForResult(intent, REQUEST_CODE_PAYMENT);
}
private PayPalPayment getThingToBuy(String paymentIntent) {
return new PayPalPayment(new BigDecimal("1.0"), "USD", "DRINK",
paymentIntent);
}
/*
* This method shows use of optional payment details and item list.
*/
public void onFuturePaymentPressed(View pressed) {
Intent intent = new Intent(PaymentActivity.this, PayPalFuturePaymentActivity.class);
// send the same configuration for restart resiliency
intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);
startActivityForResult(intent, REQUEST_CODE_FUTURE_PAYMENT);
}
public void onProfileSharingPressed(View pressed) {
Intent intent = new Intent(PaymentActivity.this, PayPalProfileSharingActivity.class);
// send the same configuration for restart resiliency
intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);
intent.putExtra(PayPalProfileSharingActivity.EXTRA_REQUESTED_SCOPES, getOauthScopes());
startActivityForResult(intent, REQUEST_CODE_PROFILE_SHARING);
}
private PayPalOAuthScopes getOauthScopes() {
Set<String> scopes = new HashSet<String>(
Arrays.asList(PayPalOAuthScopes.PAYPAL_SCOPE_EMAIL, PayPalOAuthScopes.PAYPAL_SCOPE_ADDRESS) );
return new PayPalOAuthScopes(scopes);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_PAYMENT) {
if (resultCode == Activity.RESULT_OK) {
PaymentConfirmation confirm =
data.getParcelableExtra(com.paypal.android.sdk.payments.PaymentActivity.EXTRA_RESULT_CONFIRMATION);
if (confirm != null) {
try {
Log.i(TAG, confirm.toJSONObject().toString(4));
Log.i(TAG, confirm.getPayment().toJSONObject().toString(4));
/**
* TODO: send 'confirm' (and possibly confirm.getPayment() to your server for verification
* or consent completion.
* See https://developer.paypal.com/webapps/developer/docs/integration/mobile/verify-mobile-payment/
* for more details.
*
* For sample mobile backend interactions, see
* https://github.com/paypal/rest-api-sdk-python/tree/master/samples/mobile_backend
*/
Toast.makeText(
getApplicationContext(),
"PaymentConfirmation info received from PayPal", Toast.LENGTH_LONG)
.show();
} catch (JSONException e) {
Log.e(TAG, "an extremely unlikely failure occurred: ", e);
}
}
} else if (resultCode == Activity.RESULT_CANCELED) {
Log.i(TAG, "The user canceled.");
} else if (resultCode == com.paypal.android.sdk.payments.PaymentActivity.RESULT_EXTRAS_INVALID) {
Log.i(
TAG,
"An invalid Payment or PayPalConfiguration was submitted. Please see the docs.");
}
} else if (requestCode == REQUEST_CODE_FUTURE_PAYMENT) {
if (resultCode == Activity.RESULT_OK) {
PayPalAuthorization auth =
data.getParcelableExtra(PayPalFuturePaymentActivity.EXTRA_RESULT_AUTHORIZATION);
if (auth != null) {
try {
Log.i("FuturePaymentExample", auth.toJSONObject().toString(4));
String authorization_code = auth.getAuthorizationCode();
Log.i("FuturePaymentExample", authorization_code);
sendAuthorizationToServer(auth);
Toast.makeText(
getApplicationContext(),
"Future Payment code received from PayPal", Toast.LENGTH_LONG)
.show();
} catch (JSONException e) {
Log.e("FuturePaymentExample", "an extremely unlikely failure occurred: ", e);
}
}
} else if (resultCode == Activity.RESULT_CANCELED) {
Log.i("FuturePaymentExample", "The user canceled.");
} else if (resultCode == PayPalFuturePaymentActivity.RESULT_EXTRAS_INVALID) {
Log.i(
"FuturePaymentExample",
"Probably the attempt to previously start the PayPalService had an invalid PayPalConfiguration. Please see the docs.");
}
} else if (requestCode == REQUEST_CODE_PROFILE_SHARING) {
if (resultCode == Activity.RESULT_OK) {
PayPalAuthorization auth =
data.getParcelableExtra(PayPalProfileSharingActivity.EXTRA_RESULT_AUTHORIZATION);
if (auth != null) {
try {
Log.i("ProfileSharingExample", auth.toJSONObject().toString(4));
String authorization_code = auth.getAuthorizationCode();
Log.i("ProfileSharingExample", authorization_code);
sendAuthorizationToServer(auth);
Toast.makeText(
getApplicationContext(),
"Profile Sharing code received from PayPal", Toast.LENGTH_LONG)
.show();
} catch (JSONException e) {
Log.e("ProfileSharingExample", "an extremely unlikely failure occurred: ", e);
}
}
} else if (resultCode == Activity.RESULT_CANCELED) {
Log.i("ProfileSharingExample", "The user canceled.");
} else if (resultCode == PayPalFuturePaymentActivity.RESULT_EXTRAS_INVALID) {
Log.i(
"ProfileSharingExample",
"Probably the attempt to previously start the PayPalService had an invalid PayPalConfiguration. Please see the docs.");
}
}
}
private void sendAuthorizationToServer(PayPalAuthorization authorization) {
/**
* TODO: Send the authorization response to your server, where it can
* exchange the authorization code for OAuth access and refresh tokens.
*
* Your server must then store these tokens, so that your server code
* can execute payments for this user in the future.
*
* A more complete example that includes the required app-server to
* PayPal-server integration is available from
* https://github.com/paypal/rest-api-sdk-python/tree/master/samples/mobile_backend
*/
}
#Override
public void onDestroy() {
// Stop service when done
stopService(new Intent(this, PayPalService.class));
super.onDestroy();
}
}
Thank you!
Turns out that the error occurs because the PayPal app is also installed on my phone. If I uninstall it, the payment is successfully done in the PRODUCTION environment.
PayPal will soon release a version of their app that patches this bug:
https://github.com/paypal/PayPal-Android-SDK/issues/272.
i want to build a android application that first received a message from any number and after
receive message i want hold the message string and number in the variables and then reply message adding some extra string in message to same no automatically without user interface.
i am new in android so please help me.
only i have receiving message code....plese give me solution for rest of code.
thank you
My brodcost reciver class
package com.example.broadcastreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;
public class IncomingSms extends BroadcastReceiver {
// Get the object of SmsManager
final SmsManager sms = SmsManager.getDefault();
public void onReceive(Context context, Intent intent) {
// Retrieves a map of extended data from the intent.
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String senderNum = phoneNumber;
String message = currentMessage.getDisplayMessageBody();
System.out.println(phoneNumber);
System.out.println(currentMessage);
Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + message);
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, "senderNum: "+ senderNum + ", message: " + message, duration);
toast.show();
} // end for loop
} // bundle is null
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" +e);
}
}
}
my main activity
package com.example.broadcastreceiver;
import com.androidexample.broadcastreceiver.R;
import android.os.Bundle;
import android.app.Activity;
public class BroadcastNewSms extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.androidexample_broadcast_newsms);
Ssystem.out.println("")
}
}
Well, you now should send your message from your broadcast, you should:
PendingIntent sentIntent; // can be null
PendingIntent deliveryIntent; // can be null
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentIntent, deliveryIntent);
http://developer.android.com/reference/android/telephony/SmsManager.html
you might consider putting it into IntentService, I am not sure how long execution of sendTextMessage takes, and broadcasts should not do any heavy computations/processing. Also putting this code into IntentService, will prevent android from killing your app, this is important because you want this to work in background and android might create your app just to make it receive broadcast, and after onReceive ends it might kill your app, Service will prolong your app life.
So basically im just new to Android, and im making a application which sends realtime notifications sended with php from server.
Now i want that the notification when clicked by the user opens a webpage in my webview.
Its a webbased app.
Im using 3 tabs to switch between 3 fragents.
Each fragment got a different webview.
Now this is mine GCMintentService.java:
package blah.blah;
import static blah.blah.CommonUtilities.SENDER_ID;
import static blah.blah.CommonUtilities.displayMessage;
import blah.blah.R;
import blah.blah.R.drawable;
import blah.blah.R.string;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import com.google.android.gcm.GCMBaseIntentService;
public class GCMIntentService extends GCMBaseIntentService {
private static final String TAG = "GCMIntentService";
public GCMIntentService() {
super(SENDER_ID);
}
/**
* Method called on device registered
**/
#Override
protected void onRegistered(Context context, String registrationId) {
Log.i(TAG, "Toestel geregistreerd: regId = " + registrationId);
displayMessage(context, "Je toestel is geregistreerd");
Log.d("NAME", NotificationMain.name);
ServerUtilities.register(context, NotificationMain.name, NotificationMain.klas, registrationId);
}
/**
* Method called on device un registred
* */
#Override
protected void onUnregistered(Context context, String registrationId) {
Log.i(TAG, "Toestel nog niet geregistreerd!");
displayMessage(context, getString(R.string.gcm_unregistered));
ServerUtilities.unregister(context, registrationId);
}
/**
* Method called on Receiving a new message
* */
#Override
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "Ontvangen bericht");
String message = intent.getExtras().getString("price");
displayMessage(context, message);
// notifies user
generateNotification(context, message);
}
/**
* Method called on receiving a deleted message
* */
#Override
protected void onDeletedMessages(Context context, int total) {
Log.i(TAG, "Received deleted messages notification");
String message = getString(R.string.gcm_deleted, total);
displayMessage(context, message);
// notifies user
generateNotification(context, message);
}
/**
* Method called on Error
* */
#Override
public void onError(Context context, String errorId) {
Log.i(TAG, "Received error: " + errorId);
displayMessage(context, getString(R.string.gcm_error, errorId));
}
#Override
protected boolean onRecoverableError(Context context, String errorId) {
// log message
Log.i(TAG, "Received recoverable error: " + errorId);
displayMessage(context, getString(R.string.gcm_recoverable_error,
errorId));
return super.onRecoverableError(context, errorId);
}
/**
* Issues a notification to inform the user that server has sent a message.
*/
private static void generateNotification(Context context, String message) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, MyFragment.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =
PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
//notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "your_sound_file_name.mp3");
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
}
A main activity holds the 3 fragments with all differnt webviews.
I get the urls from a array called in FragmentAdapter.java where it extends FragmentAdapter.
Works perfect... here
is the fragmentAdapter:
package blah.blah;
import blah.blah.MyFragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class MyFragmentPagerAdapter extends FragmentPagerAdapter{
String[] toVisit={
"www.google.com",
"www.stackoverlow.com",
"www.lorumipsum.com",
};
final int PAGE_COUNT = 3;
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// Here is where all the magic of the adapter happens
// As you can see, this is really simple.
return MyFragment.newInstance(toVisit[position]);
}
#Override
public int getCount() {
return PAGE_COUNT;
}
#Override
public CharSequence getPageTitle(int position) {
if(position == 0)
{
return "Klassen";
}
else if(position == 1)
{
return "Docenten";
}
else
{
return "Lokalen";
}
}
}
But how to load differnt urls depending on the retrieved info from the server? ,
The GCM push is sended with php with the usually code.
Should be something with intent.PushExtra("google.com, http://google.com") // for example
all doesn't work. I hope my question is clear.
All help is appriciated!
Just share all ur tips & thoughts:P.
Oh ye and my MyFragment.java if its usefull:
package blah.blah;
import blah.blah.R;
import blah.blah.R.id;
import blah.blah.R.layout;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
public class MyFragment extends Fragment{
WebView browser;
String url;
private Bundle webViewBundle;
Intent extras = getActivity().getIntent();
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(
R.layout.myfragment_layout,
container,
false);
final ProgressBar spinner = (ProgressBar)view.findViewById(R.id.progress);
browser=(WebView)view.findViewById(R.id.webView1);
browser.getSettings().setJavaScriptEnabled(true);
browser.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
browser.loadUrl("file:///android_asset/geeninternet.html");
}
#SuppressWarnings("unused")
public void onProgressChanged(final WebView view, final int progress)
{
if(progress == 100)
spinner.setVisibility(View.GONE);
}
});
browser.loadUrl(url);
// Just load whatever URL this fragment is
// created with.
return view;
}
// This is the method the pager adapter will use
// to create a new fragment
public static Fragment newInstance(String url)
{
MyFragment f=new MyFragment();
f.url=url;
return f;
}
// Met browser;
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_refresh:
browser.loadUrl(url);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/**
* Sla webview op
*/
#Override
public void onPause()
{
super.onPause();
webViewBundle = new Bundle();
browser.saveState(webViewBundle);
}
/**
* Herstel staat van webview
*/
#Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
if (webViewBundle != null)
{
browser.restoreState(webViewBundle);
}
}
}
#Override
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "Ontvangen bericht");
String message = intent.getExtras().getString("price");
displayMessage(context, message);
// notifies user
generateNotification(context, message);
}
In this code you take only one value from the notification payload - the value of the price key.
You didn't include the server code, so I don't know if you are sending a URL with the notification. If you do, you should get its value from the intent in the onMessage method and pass it to the code that open the web-view. If you don't, you should change your server code to include the URL in the notification payload.
EDIT :
In order to pass the URL to your fragment :
Get the URL from the extras of the intent in onMessage().
Pass it to generateNotification.
Add the URL as an extra to the intent that would start the activity when the notification is tapped : notificationIntent.putExtra ("url",url).
In the fragment, in the code where you initialize the web-view, get the URL from the extras of the intent that started the activity that contains the fragment (getActivity().getIntent().getExtras().getString("url").
EDIT2:
You should load the web-view in onActivityCreated(), not in onCreateView(). onCreateView() is called before the activity is created, so getActivity() will return null.
Don't use that code as is : String test = (getActivity().getIntent().getExtras().getString("url")); Make sure that getActivity() doesn't return null. I believe that when the getActivity() is not null, you can assume that getIntent() and getExtras() won't return null, but it's safer to check. getString("url") will return null if the activity wasn't opened from a notification. Therefore you should make sure that it has a value before loading the web-view.
There may be a problem with the notification flags :
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
It seems that the notification won't open a new activity if such an activity already exists (for example, if the app is already running). In that case, your fragment code for onCreateView() and onActivityCreated() won't be called and the URL won't be loaded. You should either change the flags so that a new activity will always be created, or you should put the logic that loads the web-view as a result of the notification somewhere else - probably in onResume().