I tried to send id of an image from an activity to another activity. But unfortunately stopped. But i couldn't find any solution. This is my main activity. I kept all image id in a class Utils.
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Intent intent=new Intent(MainActivity.this,SendActivity.class);
intent.putExtra("id",Utils.THUMBNAIL_IDS[position]);
startActivity(intent);
}
});
And this is my sendActivity from where i tried to share this image to messenger:
Intent intent = getIntent();
position=getIntent().getExtras().getInt("id");
Now the code to share to messenger:
private void onMessengerButtonClicked() {
// The URI can reference a file://, content://, or android.resource. Here we use
// android.resource for sample purposes.
Uri uri = Uri.parse("android.resource://com.example.amit.bengalistickerfun/drawable/" +
MainActivity.mAdapter.getItem(position));
// Create the parameters for what we want to send to Messenger.
ShareToMessengerParams shareToMessengerParams =
ShareToMessengerParams.newBuilder(uri, "image/jpeg")
.setMetaData("{ \"image\" : \"tree\" }")
.build();
// Sharing from an Activity
MessengerUtils.shareToMessenger(this, 0, shareToMessengerParams);
if (mPicking) {
// If we were launched from Messenger, we call MessengerUtils.finishShareToMessenger to return
// the content to Messenger.
MessengerUtils.finishShareToMessenger(this, shareToMessengerParams);
} else {
// Otherwise, we were launched directly (for example, user clicked the launcher icon). We
// initiate the broadcast flow in Messenger. If Messenger is not installed or Messenger needs
// to be upgraded, this will direct the user to the play store.
MessengerUtils.shareToMessenger(
this,
REQUEST_CODE_SHARE_TO_MESSENGER,
shareToMessengerParams);
}
Can anybody help me?
I would suggest to put try catch block here , so that you can catch the exception. if any
try to observe log-cat and view the errors
try using android inbuilt logging class to see the logs
Post your error logs here ,so that we can see the issue and provide our inputs accordingly.
Related
My app is using a NotificationListener to read out messages from various 3rd party apps, for example WhatsApp.
So far I was able to send a reply if only one chat is unread, the code is below.
However, in the case with WhatsApp, getNotification().actions returns a null object when more than two chats are unread, as the messages are bundled together. As you can see in the pictures below, if the notifications are extended there is an option to send a direct reply as well, therefore I am certain that it is possible to utilize this, also I think apps like PushBullet are using this method.
How could I access the RemoteInput of that notification?
public static ReplyIntentSender sendReply(StatusBarNotification statusBarNotification, String name) {
Notification.Action actions[] = statusBarNotification.getNotification().actions;
for (Notification.Action act : actions) {
if (act != null && act.getRemoteInputs() != null) {
if (act.title.toString().contains(name)) {
if (act.getRemoteInputs() != null)
return new ReplyIntentSender(act);
}
}
}
return null;
}
public static class ReplyIntentSender {
[...]
public final Notification.Action action;
public ReplyIntentSender(Notification.Action extractedAction) {
action = extractedAction;
[...]
}
private boolean sendNativeIntent(Context context, String message) {
for (android.app.RemoteInput rem : action.getRemoteInputs()) {
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putCharSequence(rem.getResultKey(), message);
android.app.RemoteInput.addResultsToIntent(action.getRemoteInputs(), intent, bundle);
try {
action.actionIntent.send(context, 0, intent);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
return false;
}
}
Some explanation how the above code works: Once a notification is received the app tries to get the actions and checks if the name is in the title of a remoteInput (normally it is in the format of "Reply to $NAME"), if that is found the Action is saved into a ReplyIntentSender class, which, when triggered by sendNativeIntent, cycles through all RemoteInputs of that Action and adds the message to the intent. If more than one chat is unread, getNotification().actions returns null.
Below are two screenshots, the first one where it is working without any problems and the second one where it doesn't.
You can consider this as my suggestion. I have done bit research on this and come up with following conclusions.(Also it looks like you have done plenty of research on this so it might be possible that you aware about what I wrote below)
Numerous apps send Wear specific notifications, and many of those contain actions accessible from an Android Wear device. We can grab those Wear notifications on the device, extracting the actions, finding the reply action (if one exists), populating it with our own response and then executing the PendingIntent which sends our response back the original app for it to send on to the recipient.
To do so you can refer this link (A nice workaround by Rob J). You can also refer this link in this context (Great research work done by MichaĆ Tajchert).(You might need to work around with NotificationCompat.isGroupSummary)
This is what I feel(Might be I am totally wrong)
.actions method returns Array of all Notification.Action
structures attached to current notification by addAction(int,
CharSequence, PendingIntent), Here addAction method is deprecated
one so it might not working as intended.
I am not able to test this at my end otherwise I will love to provide a working solution with code.
Hope this will help you. Happy Coding!!!
How to open an album or photo in facebook app using intent from your own Android App?
I been searching for the specific answer for this one. Most question are about how to open a facebook page via intent.
I saw this one (regards to Sunny a Sr. Software engineer at iAppStreet) but it doesn't work.
public Intent getOpenFacebookIntent(String pId) {
try {
activity.getPackageManager().getPackageInfo("com.facebook.katana", 0);
return new Intent(Intent.ACTION_VIEW, Uri.parse("facebook:/photos?album=0&photo=" + pId+"&user="+ownerId));
} catch (Exception e) {
return new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/"));
}
}
startActivity(getOpenFacebookIntent(pid));
thanks.
Actually i made this question to help those who have the same problem like me. As of today (28 March 2016) i just found out how to open an album using intent but i cannot open a photo using intent in the facebook app. I am a beginner in android programming so please bear with me.
I used a button to implement this. Here is the code:
Button buttonOpenALbum = (Button) findViewById(R.id.button);
buttonOpenALbum.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//It is a album from a facebook page # www.facebook.com/thinkingarmy
String albumID = "575146485903630";
String userID = "575145312570414";
String url = "facebook:/photos?album="+albumID+"&user="+userID;
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
If you want to get the albumID and userID of a facebook album try searching on google how or try this one if it helps, https://www.youtube.com/watch?v=sMEmlpmCHLc
Modifying the code to open a specific photo has not produce the expected result. Even though I included the photoID, you still end up in the album. Here is the code:
Button buttonOpenPhoto = (Button) findViewById(R.id.button);
buttonOpenPhoto.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//It is a album from a facebook page # www.facebook.com/thinkingarmy
String albumID = "575146485903630";
String userID = "575145312570414";
String photoID = "803154029769540";
String url = "facebook:/photos?album="+albumID+"&photo="+photoID+"&user="+userID;
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
I can't be sure why. I tried looking but some say the urls of facebook is undocumented. If I may, I think you cannot open a specific photo because upon observation, facebook open a photo in full screen, meaning it is a different Activity than the Main Activity which handles the incoming Intent. The Main Activity of the facebook app only handles the intent up to opening a specific album.
I hope I can help other "new android coders" with my first post.
NB: I just learn coding using google and stackoverflow. It's my way of giving back. Thanks.
Launch a view intent with the following URL
https://www.facebook.com/photo.php?fbid={photo_id}
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
ctx.startActivity(browserIntent);
If the app is installed, it will prompt the user to open in app, otherwise it will use the browser
Some useful information at Open Facebook Page in Facebook App (if installed) on Android
In particular, new versions of facebook app work with
Uri.parse("fb://facewebmodal/f?href=" + FACEBOOKURL
I found this the simplest approach to acheive what was needed.
Note also that to get the URL you can go to the facebook album (on facebook desktop), click the three dots next to "Edit" and "Tag" and select "Get Link". I found this more reliable than simply copying the URL from the browser.
I have a new app and I'm trying to implement it to "Call to Action" facebook page button, but I have no idea what I shall write in the "App link" text box.
They put an example link of: example://location/123456
But I have no idea what it's refering to or what's the App Link of my app.
Tried to find anything about it on the Internet but couldn't find any helpful solution since I have no app links in my app (no products etc)
My goal is that when the user clicks on Facebook "Use App", it will redirect him to the app or the google play store app page if it's not installed..
I'm not talking about open app in my app, im talking about open my app in my facebook page
You can use the next code from the wiki:
/** Open another app.
* #param context current Context, like Activity, App, or Service
* #param packageName the full package name of the app to open
* #return true if likely successful, false if unsuccessful
*/
public static boolean openApp(Context context, String packageName) {
PackageManager manager = context.getPackageManager();
try {
Intent i = manager.getLaunchIntentForPackage(packageName);
if (i == null) {
return false;
//throw new PackageManager.NameNotFoundException();
}
i.addCategory(Intent.CATEGORY_LAUNCHER);
context.startActivity(i);
return true;
} catch (PackageManager.NameNotFoundException e) {
return false;
}
}
Example usage:
openApp(this, "com.google.android.maps.mytracks");
You can read it from here
To post a message in FB, see the next post, maybe it will be useful How to open the Facebook Write Post with some pre defined text and image
You should use Try/Catch. if user have Facebook app, fb://page/12345 link will open in app. Else, Catch code will run.
#Override
public void onClick(View v) {
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri
.parse("fb://page/12345"))); //12345 is Facebook page number
} catch (Exception e) {
//open play link in browser
startActivity(new Intent(Intent.ACTION_VIEW, Uri
.parse("http://play.google.com/etc")));
}
}
I need to automate my UI testing for following operation with espresso test project.
Operation:
Click a button that opens my phone camera. Capture Image, and save the image in sdcard storage. Also update a small image view on the screen when done.
App works fine but with all other operations and similar type of above operation it becomes a time consuming process to test it manually again and again.
I was working with similar problem
and found best available solution at below link
Camera UI test
// CameraActivityInstrumentationTest.java
public class CameraActivityInstrumentationTest {
// IntentsTestRule is an extension of ActivityTestRule. IntentsTestRule sets up Espresso-Intents
// before each Test is executed to allow stubbing and validation of intents.
#Rule
public IntentsTestRule<CameraActivity> intentsRule = new IntentsTestRule<>(CameraActivity.class);
#Test
public void validateCameraScenario() {
// Create a bitmap we can use for our simulated camera image
Bitmap icon = BitmapFactory.decodeResource(
InstrumentationRegistry.getTargetContext().getResources(),
R.mipmap.ic_launcher);
// Build a result to return from the Camera app
Intent resultData = new Intent();
resultData.putExtra("data", icon);
Instrumentation.ActivityResult result = new Instrumentation.ActivityResult(Activity.RESULT_OK, resultData);
// Stub out the Camera. When an intent is sent to the Camera, this tells Espresso to respond
// with the ActivityResult we just created
intending(toPackage("com.android.camera2")).respondWith(result);
// Now that we have the stub in place, click on the button in our app that launches into the Camera
onView(withId(R.id.btnTakePicture)).perform(click());
// We can also validate that an intent resolving to the "camera" activity has been sent out by our app
intended(toPackage("com.android.camera2"));
// ... additional test steps and validation ...
}
}
If you still have the need, you can use the new Espresso-Intent that mocks the activity result that you can use to test this flow. See the sample from Android Testing
Google has provided an example on this camera problem, which they show how to stub an camera intent, launch camera, and test if image is get from stub intent, and display on imageView.
#Rule
public IntentsTestRule<ImageViewerActivity> mIntentsRule = new IntentsTestRule<>(
ImageViewerActivity.class);
#Before
public void stubCameraIntent() {
ActivityResult result = createImageCaptureActivityResultStub();
// Stub the Intent.
intending(hasAction(MediaStore.ACTION_IMAGE_CAPTURE)).respondWith(result);
}
#Test
public void takePhoto_drawableIsApplied() {
// Check that the ImageView doesn't have a drawable applied.
onView(withId(R.id.imageView)).check(matches(not(hasDrawable())));
// Click on the button that will trigger the stubbed intent.
onView(withId(R.id.button_take_photo)).perform(click());
// With no user interaction, the ImageView will have a drawable.
onView(withId(R.id.imageView)).check(matches(hasDrawable()));
}
private ActivityResult createImageCaptureActivityResultStub() {
// Put the drawable in a bundle.
Bundle bundle = new Bundle();
bundle.putParcelable(ImageViewerActivity.KEY_IMAGE_DATA, BitmapFactory.decodeResource(
mIntentsRule.getActivity().getResources(), R.drawable.ic_launcher));
// Create the Intent that will include the bundle.
Intent resultData = new Intent();
resultData.putExtras(bundle);
// Create the ActivityResult with the Intent.
return new ActivityResult(Activity.RESULT_OK, resultData);
}
Check this link for more detail IntentsAdvancedsample
For me none of the solutions worked out of the box but the following did. It's a combination of this answer and this comment.
Without calling Intents.init() my test would fail with the following exception:
java.lang.NullPointerException: Attempt to invoke virtual method 'androidx.test.espresso.intent.OngoingStubbing androidx.test.espresso.intent.Intents.internalIntending(org.hamcrest.Matcher)' on a null object reference
Complete solution:
#Before
fun setUp() {
Intents.init()
}
#After
fun tearDown() {
Intents.release()
}
#Test
fun testCameraIntent() {
// Build an ActivityResult that will return from the camera app and set your extras (if any).
val resultData = Intent()
resultData.putExtra(MediaStore.EXTRA_OUTPUT, "test.file.url")
val result = Instrumentation.ActivityResult(Activity.RESULT_OK, resultData)
// Mock the camera response. Now whenever an intent is sent to the camera, Espresso will respond with the result we pass here.
intending(hasAction(MediaStore.ACTION_IMAGE_CAPTURE)).respondWith(result)
// Now click on the button in your app that launches the camera.
onView(withId(R.id.button_camera)).perform(click());
// Optionally, we can also verify that the intent to the camera has actually been sent from out.
intended(hasAction(MediaStore.ACTION_IMAGE_CAPTURE))
// At this point the onActivityResult() has been called so verify that whatever view is supposed to be displayed is indeed displayed.
onView(withId(R.id.some_view)).check(matches(isDisplayed()))
}
In my Android app, I have a button with the background image of an Amazon.com product (let's say a shirt or something), and when clicked I would like it to open in the Amazon app (com.amazon.mShop.android) if is already installed rather than in the browser, and in the browser if the app is not installed.
I have been able to find how to add a deep link to a specific Amazon client app, but not how to link to a specific item that would open with open with the Amazon app.
Currently, my click listener opens in a browser by doing the following:
b3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse(urlOfItemOnAmazonSite));
startActivity(intent);
}
})
The Amazon developer's home page is probably the best place for this answer. This may be a good start: https://developer.amazon.com/public/apis/earn/in-app-purchasing/docs/deeplink#Link%20Configuration. Here they explain how to construct the Uri you'll need to use to set the Intent data.
Of course, you may want to be careful and wrap startActivity in a try/catch in case Amazon isn't installed and it throws an ActivityNotFoundException
You can simply do the following:
b3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Uri uri=Uri.parse(productUrl);
Intent intent=new Intent(Intent.ACTION_VIEW,uri);
startActivity(intent);
}
})
//Note extract url of product store in var productUrl(or any other var) and //parse it.