Android:Call Activity of Another Application

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 start activity in another application?

If you guys are facing "Permission Denial: starting Intent..." error or if the app is getting crash without any reason during launching the app - Then use this single line code in Manifest

android:exported="true"

Please be careful with finish(); , if you missed out it the app getting frozen. if its mentioned the app would be a smooth launcher.

finish();

The other solution only works for two activities that are in the same application. In my case, application B doesn't know class com.example.MyExampleActivity.class in the code, so compile will fail.

I searched on the web and found something like this below, and it works well.

Intent intent = new Intent();
intent.setComponent(new ComponentName("com.example", "com.example.MyExampleActivity"));
startActivity(intent);

You can also use the setClassName method:

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName("com.hotfoot.rapid.adani.wheeler.android", "com.hotfoot.rapid.adani.wheeler.android.view.activities.MainActivity");
startActivity(intent);
finish();

You can also pass the values from one app to another app :

Intent launchIntent = getApplicationContext().getPackageManager().getLaunchIntentForPackage("com.hotfoot.rapid.adani.wheeler.android.LoginActivity");
if (launchIntent != null) {
launchIntent.putExtra("AppID", "MY-CHILD-APP1");
launchIntent.putExtra("UserID", "MY-APP");
launchIntent.putExtra("Password", "MY-PASSWORD");
startActivity(launchIntent);
finish();
} else {
Toast.makeText(getApplicationContext(), " launch Intent not available", Toast.LENGTH_SHORT).show();
}

Call a specific activity of another application. Android

Intent launchIntent = new Intent(Intent.ACTION_MAIN);
launchIntent.addCategory(Intent.CATEGORY_LAUNCHER);
ComponentName cp = new ComponentName("Your App Packge Name that Contains Register Activity", "Register Activity Name");
launchIntent.setComponent(cp);

startActivity(launchIntent);

I hope this will help you out.

Launch Activity from another Application Android

What you need to use are intent-filters. Assume the activity to be launched is in the package launch.me. Inside this applications manifest all the activities (main or otherwise) will be decalred by the <activity> tag.

Assuming the activity you want to launch is inside the class file Launchme. Then a portion of your manifest will be something like:

<activity android:name="launch.me.Launchme"
android:label="@string/app_name">
<intent-filter>
<action android:name="launch.me.action.LAUNCH_IT"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>

Now in the activity from where you want to launch the above activity use:(Note: This activity can be in any package anywhere. You have to make sure that both, the calling and the called packages are available on the device)

Intent i=new Intent();
i.setAction("launch.me.action.LAUNCH_IT");
startActivityForResult(i,0);

You can use other methods for starting the intent other than startActivityForResult, thats upto you.

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



Related Topics



Leave a reply



Submit