Reputation: 1423
I know how to update my own programs, and I know how to open programs using the a predefined Uri (for sms or email for example)
I need to know how I can create an Intent to open MyTracks or any other application that I don't know what intents they listen to.
I got this info from DDMS, but I havn't been succesful in turning this to an Intent I can use. This is taken from when opening MyTracks manually.
Thanks for your help
05-06 11:22:24.945: INFO/ActivityManager(76): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.google.android.maps.mytracks/com.google.android.apps.mytracks.MyTracks bnds=[243,338][317,417] }
05-06 11:22:25.005: INFO/ActivityManager(76): Start proc com.google.android.maps.mytracks for activity com.google.android.maps.mytracks/com.google.android.apps.mytracks.MyTracks: pid=1176 uid=10063 gids={3003, 1015}
05-06 11:22:26.995: INFO/ActivityManager(76): Displayed activity com.google.android.maps.mytracks/com.google.android.apps.mytracks.MyTracks: 1996 ms (total 1996 ms)
Upvotes: 142
Views: 260877
Reputation: 25
I have work it like this,
/** 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 ActivityNotFoundException();
}
i.addCategory(Intent.CATEGORY_LAUNCHER);
context.startActivity(i);
return true;
} catch (ActivityNotFoundException e) {
return false;
}
}
Example usage:
openApp(this, "com.google.android.maps.mytracks");
Upvotes: 245
Reputation: 5947
If you want to open another application and it is not installed you can send it to the Google App Store to download
First create the openOtherApp method for example
public static boolean openOtherApp(Context context, String packageName) {
PackageManager manager = context.getPackageManager();
try {
Intent intent = manager.getLaunchIntentForPackage(packageName);
if (intent == null) {
//the app is not installed
try {
intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(Uri.parse("market://details?id=" + packageName));
context.startActivity(intent);
} catch (ActivityNotFoundException e) {
//throw new ActivityNotFoundException();
return false;
}
}
intent.addCategory(Intent.CATEGORY_LAUNCHER);
context.startActivity(intent);
return true;
} catch (ActivityNotFoundException e) {
return false;
}
}
2.- Usage
openOtherApp(getApplicationContext(), "com.packageappname");
Upvotes: 3
Reputation: 3500
Open application if it is exist, or open Play Store application for install it:
private void open() {
openApplication(getActivity(), "com.app.package.here");
}
public void openApplication(Context context, String packageN) {
Intent i = context.getPackageManager().getLaunchIntentForPackage(packageN);
if (i != null) {
i.addCategory(Intent.CATEGORY_LAUNCHER);
context.startActivity(i);
} else {
try {
context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + packageN)));
}
catch (android.content.ActivityNotFoundException anfe) {
context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + packageN)));
}
}
}
Upvotes: 6
Reputation: 192
You can use this command to find the package names installed on a device:
adb shell pm list packages -3 -f
Reference: http://www.aftvnews.com/how-to-determine-the-package-name-of-an-android-app/
Upvotes: 0
Reputation: 3561
Try this code, Hope this will help you. If this package is available then this will open the app or else open the play store for downloads
String packageN = "aman4india.com.pincodedirectory";
Intent i = getPackageManager().getLaunchIntentForPackage(packageN);
if (i != null) {
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
} else {
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + packageN)));
}
catch (android.content.ActivityNotFoundException anfe) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + packageN)));
}
}
Upvotes: 0
Reputation: 617
Intent launchIntent = getActivity.getPackageManager().getLaunchIntentForPackage("com.ionicframework.uengage");
startActivity(launchIntent);
Upvotes: 2
Reputation: 2350
Use following:
String packagename = "com.example.app";
startActivity(getPackageManager().getLaunchIntentForPackage(packagename));
Upvotes: 2
Reputation: 462
To Start another application activity from my application Activity. It is working fine for me.
Below code will work if the another application already installed in your phone otherwise it is not possible to redirect form one app to another app.So make sure your app launched or not
Intent intent = new Intent();
intent.setClassName("com.xyz.myapplication", "com.xyz.myapplication.SplashScreenActivity");
startActivity(intent);
Upvotes: 4
Reputation: 1647
Alternatively you can also open the intent from your app in the other app with:
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
where uri
is the deeplink to the other app
Upvotes: 2
Reputation: 8541
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(ComponentName.unflattenFromString("com.google.android.maps.mytracks/com.google.android.apps.mytracks.MyTracks"));
intent.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(intent);
EDIT :
as suggested in comments, add one line before startActivity(intent);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Upvotes: 97
Reputation: 49
// This works on Android Lollipop 5.0.2
public static boolean launchApp(Context context, String packageName) {
final PackageManager manager = context.getPackageManager();
final Intent appLauncherIntent = new Intent(Intent.ACTION_MAIN);
appLauncherIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> resolveInfos = manager.queryIntentActivities(appLauncherIntent, 0);
if ((null != resolveInfos) && (!resolveInfos.isEmpty())) {
for (ResolveInfo rInfo : resolveInfos) {
String className = rInfo.activityInfo.name.trim();
String targetPackageName = rInfo.activityInfo.packageName.trim();
Log.d("AppsLauncher", "Class Name = " + className + " Target Package Name = " + targetPackageName + " Package Name = " + packageName);
if (packageName.trim().equals(targetPackageName)) {
Intent intent = new Intent();
intent.setClassName(targetPackageName, className);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
Log.d("AppsLauncher", "Launching Package '" + packageName + "' with Activity '" + className + "'");
return true;
}
}
}
return false;
}
Upvotes: 3
Reputation: 9404
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:
action=MAIN
and category=LAUNCHER
PackageManager
from the current context using context.getPackageManager
packageManager.queryIntentActivity(<intent>, 0)
where intent has category=LAUNCHER
, action=MAIN
or packageManager.resolveActivity(<intent>, 0)
to get the first activity with main/launcherActivityInfo
you're interested inActivityInfo
, get the packageName
and name
category=LAUNCHER
, action=MAIN
, componentName = new ComponentName(packageName, name)
and setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(newIntent)
Upvotes: 142
Reputation: 18922
If you're attempting to start a SERVICE rather than activity, this worked for me:
Intent intent = new Intent();
intent.setClassName("com.example.otherapplication", "com.example.otherapplication.ServiceName");
context.startService(intent);
If you use the intent.setComponent(...) method as mentioned in other answers, you may get an "Implicit intents with startService are not safe" warning.
Upvotes: 2
Reputation: 746
Use this :
PackageManager pm = getPackageManager();
Intent intent = pm.getLaunchIntentForPackage("com.package.name");
startActivity(intent);
Upvotes: 6
Reputation: 1647
For API level 3+, nothing more then one line of code:
Intent intent = context.getPackageManager().getLaunchIntentForPackage("name.of.package");
Return a CATEGORY_INFO launch Intent (apps with no launcher activity, wallpapers for example, often use this to provide some information about app) and, if no find it, returns the CATEGORY_LAUNCH of package, if exists.
Upvotes: 2
Reputation: 1021
Since applications aren't allowed to change many of the phone settings, you can open a settings activity just like another application.
Look at you LogCat output after you actually modified the setting manually:
INFO/ActivityManager(1306): Starting activity: Intent { act=android.intent.action.MAIN cmp=com.android.settings/.DevelopmentSettings } from pid 1924
Then use this to display the settings page from your app:
String SettingsPage = "com.android.settings/.DevelopmentSettings";
try
{
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(ComponentName.unflattenFromString(SettingsPage));
intent.addCategory(Intent.CATEGORY_LAUNCHER );
startActivity(intent);
}
catch (ActivityNotFoundException e)
{
log it
}
Upvotes: 2
Reputation: 15
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName("package_name","package_name.class_name"));
intent.putExtra("grace", "Hi");
startActivity(intent);
Upvotes: -3
Reputation: 12045
Using the solution from inversus, I expanded the snippet with a function, that will be called when the desired application is not installed at the moment. So it works like: Run application by package name. If not found, open Android market - Google play for this package.
public void startApplication(String packageName)
{
try
{
Intent intent = new Intent("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
List<ResolveInfo> resolveInfoList = getPackageManager().queryIntentActivities(intent, 0);
for(ResolveInfo info : resolveInfoList)
if(info.activityInfo.packageName.equalsIgnoreCase(packageName))
{
launchComponent(info.activityInfo.packageName, info.activityInfo.name);
return;
}
// No match, so application is not installed
showInMarket(packageName);
}
catch (Exception e)
{
showInMarket(packageName);
}
}
private void launchComponent(String packageName, String name)
{
Intent intent = new Intent("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
intent.setComponent(new ComponentName(packageName, name));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
private void showInMarket(String packageName)
{
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + packageName));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
And it is used like this:
startApplication("org.teepee.bazant");
Upvotes: 10
Reputation: 8090
If you already have the package name you wish to activate, you can use the following code which is a bit more generic:
PackageManager pm = context.getPackageManager();
Intent appStartIntent = pm.getLaunchIntentForPackage(appPackageName);
if (null != appStartIntent)
{
context.startActivity(appStartIntent);
}
I found that it works better for cases where the main activity was not found by the regular method of start the MAIN activity.
Upvotes: 40
Reputation: 1317
This is the code of my solution base on MasterGaurav solution:
private void launchComponent(String packageName, String name){
Intent launch_intent = new Intent("android.intent.action.MAIN");
launch_intent.addCategory("android.intent.category.LAUNCHER");
launch_intent.setComponent(new ComponentName(packageName, name));
launch_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activity.startActivity(launch_intent);
}
public void startApplication(String application_name){
try{
Intent intent = new Intent("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
List<ResolveInfo> resolveinfo_list = activity.getPackageManager().queryIntentActivities(intent, 0);
for(ResolveInfo info:resolveinfo_list){
if(info.activityInfo.packageName.equalsIgnoreCase(application_name)){
launchComponent(info.activityInfo.packageName, info.activityInfo.name);
break;
}
}
}
catch (ActivityNotFoundException e) {
Toast.makeText(activity.getApplicationContext(), "There was a problem loading the application: "+application_name,Toast.LENGTH_SHORT).show();
}
}
Upvotes: 13