12

I have been trying to get two activities in two separate applications to communicate using a custom action and an implicit intent.

The first application (server), has the following manifest:

<application android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" android:theme="@style/AppTheme">
    <activity android:name="edu.example.sharing.manager.SecureFileShare"
        android:label="@string/title_activity_secure_file_share" android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="edu.example.sharing.action.STORE" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="*/*" />
        </intent-filter>
    </activity>
</application>

The second application creates an intent as follows:

File f = new File(s);
Uri fileUri = Uri.fromFile(f);
Intent intent = new Intent();
intent.setData(fileUri);
intent.setAction("edu.example.sharing.action.STORE");               
startActivityForResult(intent, STORE_REQUEST);

Its manifest is normal. When I try to send the intent in the client application, however, I get an activity not found exception:

FATAL EXCEPTION: main
android.content.ActivityNotFoundException: No Activity found to handle Intent {act=edu.example.sharing.action.STORE dat=file:///storage/sdcard0/Download/Alarcon12-Rigoberto.pdf }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1545)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1416)
at android.app.Activity.startActivityForResult(Activity.java:3351)
at android.app.Activity.startActivityForResult(Activity.java:3312)

What's causing Android to not recognize the declared activity in the second application? Thanks.

skoke
  • 323
  • 4
  • 12
  • try putting an exported attribute to the `FileStore` activity in the manifest. – Varun Sep 06 '12 at 10:12
  • also for me sometimes it dint work when i was setting the action using `setAction()`, but then i switched to using the `Intent` constructor to set the action, ex: `new Intent("edu.example,manager.action.STORE");` – Varun Sep 06 '12 at 10:15
  • Try changing your action to match your package id, so edu.example.manager.action.STORE becomes edu.example.manager.FileStore.action.STORE – Dazzy_G Sep 06 '12 at 10:32
  • I modified the code as @Varun and Dazzy_G had suggested, but it didn't solve the problem. I have reposted the modified versions. – skoke Sep 06 '12 at 11:08

2 Answers2

10

After much looking, here's what I found:

When you use a built-in action type and attach a data field or when you use a custom action type with no data field, an intent-filter without a data element is ok.

However, when you define a custom action and include a data field, you must manually set the mime-type for the URI attached. The Android documentation claims that

Normally the type is inferred from the data itself. By setting this attribute, you disable that evaluation and force an explicit type.

But that's not the case. When I put in a file:// URI which ended in .txt, Android assigned it a null mime-type, so it wouldn't match any intent-filter, even one with a data and */* mime-type. I needed to manually set the intent's type using setDataAndType().

In short: You must manually define an intent's mime-type when using a custom action with data.

skoke
  • 323
  • 4
  • 12
1

Firstly You can specify only which application to go to; you cant specify which avtivity to go to; I've already answered how to navigate to another app here; after that your control goes to the other app; you have to handle it there

Community
  • 1
  • 1
Manoj Kumar
  • 1,510
  • 3
  • 20
  • 40
  • 1
    That's not really true. An implicit intent can map to an Activity, BroadcastReciever or Service. – Dazzy_G Sep 06 '12 at 10:14
  • 1
    I'm not trying to choose an activity to go to - I'm trying to send out an _implicit_ intent which should cause Android to choose which application(s) can handle the it. The answer you link to refers to opening an application by name. – skoke Sep 06 '12 at 10:43