I like share intent, it is perfect to open sharing apps with image and text parameters.
But now i'm researching the way to force share intent to open a specific app from the list, with the paramters given to the share intent.
This is my actual code, it shows the list of sharing apps installed on the phone. Please, can somedone tell me what i should add to the code to force for example official twitter app? and official faccebok app?
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
Uri screenshotUri = Uri.parse("file:///sdcard/test.jpg");
sharingIntent.setType("image/*");
sharingIntent.putExtra(Intent.EXTRA_TEXT, "body text");
sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
startActivity(Intent.createChooser(sharingIntent, "Share image using"));
Thanks
It is possible to start an app's activity by using Intent. setClassName according to the docs. To open it outside the current app, add this flag before starting the intent.
When you construct an intent, you must specify the action you want the intent to perform. Android uses the action ACTION_SEND to send data from one activity to another, even across process boundaries. You need to specify the data and its type.
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken button view to open YouTube application.
You need to raise a intent, try like this: Uri mUri = Uri. parse("market://details?id=" + packageName); Intent mIntent = new Intent(Intent. ACTION_VIEW, mUri); startActivity(marketIntent);
For Facebook
public void shareFacebook() { String fullUrl = "https://m.facebook.com/sharer.php?u=.."; try { Intent sharingIntent = new Intent(Intent.ACTION_SEND); sharingIntent.setClassName("com.facebook.katana", "com.facebook.katana.ShareLinkActivity"); sharingIntent.putExtra(Intent.EXTRA_TEXT, "your title text"); startActivity(sharingIntent); } catch (Exception e) { Intent i = new Intent(Intent.ACTION_VIEW); i.setData(Uri.parse(fullUrl)); startActivity(i); } }
For Twitter.
public void shareTwitter() { String message = "Your message to post"; try { Intent sharingIntent = new Intent(Intent.ACTION_SEND); sharingIntent.setClassName("com.twitter.android","com.twitter.android.PostActivity"); sharingIntent.putExtra(Intent.EXTRA_TEXT, message); startActivity(sharingIntent); } catch (Exception e) { Log.e("In Exception", "Comes here"); Intent i = new Intent(); i.putExtra(Intent.EXTRA_TEXT, message); i.setAction(Intent.ACTION_VIEW); i.setData(Uri.parse("https://mobile.twitter.com/compose/tweet")); startActivity(i); } }
There is a way more generic to do that, and is not necessary to know the full package name of the application intent.
See this post: How to customize share intent in Android?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With