Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force Share Intent to open a specific app?

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

like image 539
NullPointerException Avatar asked Nov 29 '11 09:11

NullPointerException


People also ask

How do I open an app with intent?

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.

How do I send intent from one app to another?

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.

How do I open one app from another app?

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.

How do I redirect an app to another Android?

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);


2 Answers

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);         }     } 
like image 142
Xorsat Avatar answered Oct 22 '22 16:10

Xorsat


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?

like image 34
Derzu Avatar answered Oct 22 '22 17:10

Derzu