How to Call One Android Application from Another Android Application

How to Invoke or call one app from another app in Android?

I am going to assume that you really mean that you want to launch another app, not another Activity in your app.

Then there are two ways to do this. You can try using an implicit intent which according to the docs, an (Implicit) intent is "an abstract description of an operation to be performed" that "provides for performing late runtime binding between code in different applications." Sort of like trying to launch a method over the wire using an interface. You cannot be sure exactly what the class of the object that is launched only that it can handle the action and categories that you declare.

The second approach is an explicit intent, which is more like making a concrete call over the wire. If you know the package and class name this should work.

    Intent intent = new Intent(Intent.ACTION_MAIN);
//intent.putExtra("plain_text", "Testing");
intent.setClassName("packagename", "packagename.ClassName"); // Explicit Intent
try {
startActivity(intent);
}
catch (Exception e)
{
Log.d(TAG","onCreate",e);
}
}

You can add extra info using flags depending on your needs and where your are trying to launch from.

JAL

Android : Call activity of another application

Grant,

The issue here is clearly a misunderstanding of the Android Application Model. Commonsware is absolutely correct about how to solve this problem. However, without understanding Android fundamentals, I can see why you are having difficulty applying it. So, a quick explanation:

Every action in Android begins with an Intent. This is particularly true for Activities. Every Activity has an Intent. To make the interface easy for the developers, you may respond to an Intent from the OS, OR you may create an Intent from the Activities class to use. In general, it is best practice to do the first option.

Responding to an Intent

When picking an Intent to respond to, you may literally respond to any Intent. This is called an Action. If I created an Intent called "FOO", the Bar Activity could pick it up and respond. We have conventions, however, and the primary of those is to prepend your package name to any Intent you make. For example "com.company.package.FOO". Simply put, this is so that we avoid collisions with other apps.

Every Activity may respond to different events. This is defined in the AndroidManifest.xml.

<activity android:name="Activity3" ... >
<intent-filter>
<action android:name="com.company.package.FOO"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

Above, we also set the category to DEFAULT, so that unless the user changes it, we'll be the only app that responds to our custom Intent. The way that we then call the Intent is by using the SAME NAME that we created (i.e. "com.company.package.FOO")

startActivity(new Intent("com.company.package.FOO"));

That's how it works! You would simply change the above "com.company.package.FOO" to your package name (defined by your application) and something meaningful. An example is "com.testapp.ws.SWAT_FLIES".

Why your code doesn't work

Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.testapp.ws");

The above code looks for a specific KIND of Intent action. Remember when you made the AndroidManifest and the first Activity you put:

 <action android:name="android.intent.action.MAIN">
<category android:name="android.intent.category.LAUNCHER">

Well... getLaunchIntentForPackage() only gets the Intent for that first Activity. That's WHY we make a custom Intent... First, because we don't really want it to be our 3rd Activity to be our start up... And second, becuase the OS will tell us only the startup Activity. We have to tell it with our OWN action (i.e. "com.testapp.ws.SWAT_FLIES")

Hope this helps,

FuzzicalLogic

How to launch an Activity from another Application in Android 30

Android 11 added restrictions regarding the visibility of other apps. Apps that have targetSdk set to >= 30 can't interact with and open other apps without specifying this in the manifest.

To specify that your app interacts with another specific app, you need to add a <queries> element to your manifest file:

<manifest package="com.example.game">
<queries>
<package android:name="com.android.audioapp" />
</queries>
...
</manifest>

The link below contains other examples in case you need to specify a broader range of apps which you want to interact with.

Source: https://developer.android.com/training/basics/intents/package-visibility

Calling an app from another app

Use the PackageManager to get an Intent for the package:

PackageManager pm = getPackageManager();
Intent intent = pm.getLaunchIntentForPackage("com.example.package");
startActivity(intent);


Related Topics



Leave a reply



Submit