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:
- Create an intent with
action=MAIN
andcategory=LAUNCHER
- Get the
PackageManager
from the current context usingcontext.getPackageManager
packageManager.queryIntentActivity(<intent>, 0)
where intent hascategory=LAUNCHER
,action=MAIN
orpackageManager.resolveActivity(<intent>, 0)
to get the first activity with main/launcher- Get the
ActivityInfo
you're interested in - From the
ActivityInfo
, get thepackageName
andname
- Finally, create another intent with with
category=LAUNCHER
,action=MAIN
,componentName = new ComponentName(packageName, name)
andsetFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
- 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
Android: How to Create a Dialog Without a Title
How to Change the Default Icon on the Searchview, to Be Use in the Action Bar on Android
How to Programmatically Add Buttons into Layout One by One in Several Lines
Navigation Drawer (Google+ VS. Youtube)
Fragment Onresume() & Onpause() Is Not Called on Backstack
Setuservisiblehint Called Before Oncreateview in Fragment
Connect an Android Device to a Web Service on Local Host
Edittext Loses Content on Scroll in Listview
Android What Does the Cliptopadding Attribute Do
Add Ripple Effect to My Button with Button Background Color
Android How to Display 2 Listviews in One Activity One After the Other
How to Globally Force Screen Orientation in Android
How to Rotate the Android Emulator Display
Install_Failed_Duplicate_Permission... C2D_Message