beerBear
beerBear

Reputation: 979

Starting an Activity Using Intent's Action and Limiting it to Within an App

I have 2 Apps which have the same package name but different package IDs(set in gradle config).

An activity is started by setting an intent's action string in both the apps which are same in both Apps.

This means that if I have both the Apps installed on the same device then starting an Activity shows me a Complete action using.. dialog which asks the user to choose an activity to complete the action with.

My question is as to how I can separate them without going through changing the string itself in the manifest files (a lot of them due to multiple modules) and the setting of action string before using startActivity() itself in the codebase so that the actions are limited to the current App only?

Update #1: Do we have anything closer to intent.setPackage() as to use Application Id instead?

Upvotes: 1

Views: 1507

Answers (2)

beerBear
beerBear

Reputation: 979

Here's the shortest possible way to tackle it in it's current state:
android:exported = "false" for every activity declared in android manifest setting it's intent filter with an action.
Check here for reference

I overlooked the fact that's something we also do in case of GCM's broadcast receiver and other background services generally.

This has to be done in both the Apps for tighter integration.

Upvotes: 1

Kingfisher Phuoc
Kingfisher Phuoc

Reputation: 8190

If I do not miss-understand your question, you can archive it via below code:

// start exactly component
Intent intent = new Intent();
intent.setComponent(new ComponentName("yourPackageId", "yourPackageName.MainActivity"));
startActivity(intent);

//or you want to start launcher intent
Intent intent = getActivity().getPackageManager().getLaunchIntentForPackage("yourPackageId");

You shoud make sure that your MainActivity has android:exported = true as describe here android:exported:

Whether or not the activity can be launched by components of other applications — "true" if it can be, and "false" if not. If "false", the activity can be launched only by components of the same application or applications with the same user ID.

Upvotes: 1

Related Questions