ZOFTINO.COM android and web dev tutorials

How to Control Other Android Apps Access to Your App Components

One of the best features of Android platform is allowing components to interact, meaning a component can start another component to perform some action. Interacting components can exist within the same app or in separate apps.

Reusability of components is achieved with this type of interaction. You don’t need to write a component to capture video, camera app has the component which can be invoked by your app to capture video and send it to your app.

This communication between components is achieved using Intents. Intents are message carriers with such required information to carry out the action as type of action and data on which the action need to be performed etc.

Intents are two types, implicit and explicit intents. With explicit intents, source component mentions the target component by passing target action class in intent. Android system finds the action and starts it directly. Whereas, implicit intents are resolved using intent filters defined in all installed apps on the device. Implicit intents are defined by specifying type and category of action to be performed. Android system finds the target action to be performed using information passed in the intent and definition of intent filters defined in installed apps.

There are ways you can control who can start your app component. By default, components can be started within the app only if no intent filters are defined for the components.

By defining intent filter for your component, you can tell android system that only specific type and category of actions can be handled by your component. This means, Android system starts your component for intents which can be matched to intent filter defined for your component. But any app can start your component by passing components class with explicit intent.


<activity android:name="BestActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity> 

So if you don’t want other apps to start your component, you need to either not define intent filter or set exported as false for your activity in manifest xml file.


<activity android:name="BestActivity" android:exported="false">
</activity> 

You can also restrict other apps from accessing your components by setting permission for the activity you want to restrict access to. Other apps can start your component only when it gets required permissions from user, as set in manifest file for target component in your app.


<activity android:name="BestActivity" android:permission="BestActivityPermission">
</activity>