允许其他应用启动我的活动

时间:2017-03-21 10:25:31

标签: android android-manifest

我有used this example,以便其他应用程序可以调用我的活动来接收来自它们的数据。

具体来说,我想从浏览器上传图像时,我的应用程序可以提供该图像。

在下图中可以看到这种情况。用户单击上载文件时提供图像的应用程序列表:

enter image description here

我使用的代码是从我添加到开头的链接中复制的。

    <activity
        android:name=".LoginActivity"
        android:label="@string/title_activity_login">
        <!-- filter for sending text or images; accepts SEND action and text or image data -->
        <intent-filter>
            <action android:name="android.intent.action.SEND" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="image/*" />
            <data android:mimeType="text/plain" />
        </intent-filter>
    </activity>

我无法让我的应用出现在照片中显示的应用列表中。

我也测试了这个,正如@Avi建议的那样:

 <intent-filter>
            <action android:name="android.intent.action.SEND" />

            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.APP_BROWSER" />
            <category android:name="android.intent.category.BROWSABLE" />

            <data android:mimeType="image/*" />
            <data android:mimeType="text/plain" />
 </intent-filter>

获得相同的结果

注意:我在Android中使用Chrome浏览器

3 个答案:

答案 0 :(得分:0)

更改以下行

<action android:name="android.intent.action.SEND" />

<action android:name="android.intent.action.VIEW" />

答案 1 :(得分:0)

使用以下代码上传图像时,我的应用程序显示在浏览器中。

        <intent-filter>
            <action android:name="android.media.action.IMAGE_CAPTURE" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.media.action.STILL_IMAGE_CAMERA" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>

答案 2 :(得分:0)

试试这个-

清单文件:

<activity
    android:name=".activities.HomeActivity"
    android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.SEND" />
        <category android:name="android.intent.category.DEFAULT" />

        <data android:mimeType="text/plain" />
        <data android:mimeType="image/*" />
    </intent-filter>
</activity>

您的MainActivity文件:

val data: Uri? = intent?.data

// Figure out what to do based on the intent type
if (intent?.type?.startsWith("image/") == true) {
    // This handles the intents with image data or of any other type based on your specified condition.
} else if (intent?.type == "text/plain") {
    // This handle the intents with text or based on your conditions
    //Use any popup like toast, snackbar, dialog, etc of your preference.
}

也请检查您是否以VIEW或SEND提及了您的操作。 如果显示为VIEW,则不会反映在您的结果中,但是如果显示为SEND,则它将起作用。 检查是否在您提到此文件的任何文件中

val sendIntent = Intent(Intent.ACTION_SEND)   //Replace to SEND if you have used ACTION_VIEW

sendIntent.type = "image/*"
val title = context?.resources?.getString(R.string.chooser_text)
if (context?.packageManager != null) {
    context?.startActivity(Intent.createChooser(sendIntent, title))
}

如果您想获取操作结果,也可以插入...

Intent("com.example.RESULT_ACTION", Uri.parse("content://result_uri")).also { result ->
    setResult(Activity.RESULT_OK, result)
}
finish()

要更好地理解此流程,请参阅-> https://developer.android.com/training/basics/intents/filters