How to launch a mobile app from another app? - android

Is there any way to launch for one mobile app to launch another mobile app, e.g. via a button click?
Example: the org.apache.cordova.camera plugin allows direct access to the camera on a button click. In the same way, how can one app launch another app?

You can use this java code:
Intent LaunchIntent = this.cordova.getActivity().getPackageManager().getLaunchIntentForPackage("appPackage");
this.cordova.getActivity().startActivity(LaunchIntent);
or try any of these 2 plugins for launching apps:
https://github.com/lampaa/com.lampa.startapp
https://github.com/dmedvinsky/cordova-startapp

Try this
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("package name here");
startActivity( LaunchIntent );
If you don't know the package name of application that you want to launch then try this
PackageManager pm;
pm = getPackageManager();
// get a list of all installed apps then launch by pakagename.
packages = pm.getInstalledApplications(0);
String packagename = packages.get(position).packageName.toString()
Refer this android-package-manager

You need to find out full name of application, and then start it as activity via intent like this:
Intent myIntent = new Intent(getApplicationContext(), "full name of activity you are starting");
startActivity(myIntent);
You can even receive result from that activity check this. Hope this helps!

Related

How to leave the current app and launch an activity of another app?

I have a simple Android app that needs to launch another app under certain condition, and I will need to check the condition upon the app launch. That is, either continue to launch my app or just launch another app:
if (A == true) {
launch another activity of another app
leave the current app without creating the main activity
} else {
launch the main activity of the current app
}
Could anyone please let me know how to deal with A == true case? I am able to launch another app's activity but I have trouble leaving the current app without even opening the main activity.
Any help will be greatly appreciated!
You can launch other application using following intent
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.example.abc");//pass the packagename of app you want to open
startActivity( LaunchIntent );
If you don't know the package name of application that you wanted to launch then try your hand on
PackageManager pm;
pm = getPackageManager();
// get a list of installed apps.
packages = pm.getInstalledApplications(0);
Pass the packagename of app you want to open
You can use this if A == true
else You can launch the MainActivity as
startActivity(new Intent(CurrentActivity.this,MainActivity.class));
If you want to start another activity of another app without the normal IntentFilter then the easiest solutions is:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName("com.anotherapp.package","com.anotherapp.package.MainActivity"));
startActivity(intent);
Of course, you would need to know the package name and the Activity name you want to start
As for finishing your application call
finishAndRemoveTask();

How to finish my custom launcher Programmatically in android

I am developing an application my requirement is to finish my custom launcher and switch to default launcher. How can I achieve that?
follow the following steps:
By using Package Manager get the intents which is having launcher and category home. By using queryIntents method.
Use the 0th indexed intent and get the class name and package name. store some where else like sharedPreferences.
whenever you want to launch the default launcher just create intent with class name and packager name.
Then start that activity when it is required.
Following code may help you:
PackageManager packageManager = getPackageManager();
Intent i = new Intent();
i.addCategory(Intent.CATEGORY_HOME);
i.setAction(Intent.ACTION_MAIN);
List<ResolveInfo> queryIntentActivities = packageManager
.queryIntentActivities(i, 0);
ResolveInfo resolveInfo = queryIntentActivities.get(0);
String packageName = resolveInfo.resolvePackageName;
String className = resolveInfo.activityInfo.targetActivity;

Android Auto - launch third party app from within my app?

Is it possible to launch any third party application from my application on Android Auto
I couldn't find anything mentioned on this anywhere.
Note: Please note "Android Auto" (Car) words here. I am not asking for android mobile application.
Idea of android auto is completely different from what you are trying to do.
Android auto provides a platform where it has done the basic things with a good user interface making sure not to distract user much.
All that you need to do is provide services which this platform can use.
As of now you can provide Music and Messaging services which are compatible with android auto.
You can launch applications code something like
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName("com.example.youpackage", "com.example.LauchActivity");
startActivity(intent);
And if you want get all possible application list for launch.code :
Declare you intent and add value you want pass
PackageManager packageManager = getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
boolean isIntentSafe = activities.size() > 0;
if (isIntentSafe) {
startActivity(mapIntent);
}
And another way to start you specific application
PackageManager pm = getPackageManager();
Intent intent = pm.getLaunchIntentForPackage("com.example.package");
startActivity(intent);

Close the app programmatically (know package name)

I use the following code snippet to launch an app on device:
Context mContext = getContext();
String packageName = getPackageName(); //the app to launch
Intent mIntent = getPackageManager().getLaunchIntentForPackage(packageName);
if(mIntent!=null){
mContext.startActivity(mIntent);
}
It works, the app get launched, however, I don't figure out a way to close the launched app by using the packageName.
How to close the launched app if I only know the package name?
You cannot close another app. Only the system can do that.
But if you are also the author of that other app, you could create a receiver in that app's activities that accepts an intent that tells it to finish() the activities.
You cannot close other application from your app.your problem has a workaround,after starting app based on package name you may send home intent which will give same experience to user.
Intent mIntent = getPackageManager().getLaunchIntentForPackage(packageName);
if(mIntent!=null){
mContext.startActivity(mIntent);
}
//sleep for 250ms or whatever time using handler
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);

Launch another App within an App for android

I am new to Android. Say, I open an app and I would like to open another App after clicking a button. How can I accomplish this task? Would appreciate if you can provide me some tutorial on this.
Intent intent = new Intent();
intent.setClassName("**package.name**", "**package.name.LauncherActivityName**");
startActivityForResult(intent,REQUEST_CODE);
You need to know the package and class names of the activity to call
Intent i = new Intent(Intent.ACTION_MAIN);
PackageManager manager = getPackageManager();
i = manager.getLaunchIntentForPackage("app package name");
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
Use this code:
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.example.package");
startActivity(launchIntent);
The app you want to launch must be on the device.
Intent appIntent = getPackageManager().getLaunchIntentForPackage("your app package name ");
startActivity(appIntent );
If the other application is a pre-packaged application mean, this tutorial may help you.
If the other application is going to be your application, then you need to learn Implicit Intent tutorials.
Also include the activity of the other application which you are planning to call in the Manifest file of the calling package also.

Categories

Resources