Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to export an activity so other apps can call it?

Well I searched a lot, but I didn't find a precise answer how to export an Activity, so an app can start it with startActivityforResult.

How do I achieve that? Do I have to change the Manifest in some ways?

like image 510
Force Avatar asked Nov 16 '11 10:11

Force


People also ask

How do I export Android activity?

The important part is android:exported="true" , this export tag determines "whether or not the activity can be launched by components of other applications". If your <activity> contains an <intent-filter> then this tag is set to true automatically, if it does not then it is set to false by default.

What is activity exported?

The exported attribute is used to define if an activity, service, or receiver in your app is accessible and can be launched from an external application. As a practical example, if you try to share a file you'll see a set of applications available.

Are Android activities exported by default?

Note that in Android before 4.2, the Content Provider is automatically exported unless it has been explicitly declared as NOT exported.


2 Answers

As an alternate to Dalmas' answer, you can actually export an Activity without creating an <intent-filter> (along with the hassle of coming up with a custom action).

In the Manifest edit your Activity tag like so:

<activity
    android:name=".SomeActivity"
    ....
    android:exported="true" />

The important part is android:exported="true", this export tag determines "whether or not the activity can be launched by components of other applications". If your <activity> contains an <intent-filter> then this tag is set to true automatically, if it does not then it is set to false by default.

Then to launch the Activity do this:

Intent i = new Intent();
i.setComponent(new ComponentName("package name", "fully-qualified name of activity"));
startActivity(i);

Of course with this method you will need to know the exact name of the Activity you are trying to launch.

like image 199
Tony Chan Avatar answered Oct 04 '22 05:10

Tony Chan


You need to declare an intent-filter in your Manifest (I took the following example from Barcode Scanner) :

<activity android:name="...">
    <intent-filter>
        <action android:name="com.google.zxing.client.android.SCAN" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

Then create an intent with the same action string :

Intent intent = new Intent("com.google.zxing.client.android.SCAN");
startActivityForResult(intent, code);

Android should start your activity (or it will show a drop-down box if there are multiple apps sharing the same action string).

like image 15
Dalmas Avatar answered Oct 03 '22 05:10

Dalmas