How to Force Share Intent to Open a Specific App

How to force Share Intent to open a specific app?

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

Open another application from your own (intent)

Firstly, the concept of "application" in Android is slightly an extended one.

An application - technically a process - can have multiple activities, services, content providers and/or broadcast listeners. If at least one of them is running, the application is up and running (the process).

So, what you have to identify is how do you want to "start the application".

Ok... here's what you can try out:

  1. Create an intent with action=MAIN and category=LAUNCHER
  2. Get the PackageManager from the current context using context.getPackageManager
  3. packageManager.queryIntentActivity(<intent>, 0) where intent has category=LAUNCHER, action=MAIN or packageManager.resolveActivity(<intent>, 0) to get the first activity with main/launcher
  4. Get theActivityInfo you're interested in
  5. From the ActivityInfo, get the packageName and name
  6. Finally, create another intent with with category=LAUNCHER, action=MAIN, componentName = new ComponentName(packageName, name) and setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
  7. Finally, context.startActivity(newIntent)

Force Android app to use a particular intent, rather than showing intent menu

If you are sure that application which should get your intent is exist in a system, you can use Intent.setClass method

Navigate back to app after closing share dialog

Use startActivityForResult() instead of startActivity(). This will return to the starting Activity after the Intent action is completed. See the Getting a Result from an Activity post for an example.

And wait for response by overriding onActivityResult() method :

    @Override   public void onActivityResult(int requestCode, int resultCode, Intent data) {        
// TODO Auto-generated method stub if(requestCode == 0) {
// You will get callback here when email activity is exited
// perform you task here
}


Related Topics



Leave a reply



Submit