如何让我的Android应用程序出现在另一个特定应用程序的共享列表中

And*_*nja 23 android share filter android-intent

<action android:name="android.intent.action.SEND" />     
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
Run Code Online (Sandbox Code Playgroud)

这是在我的清单文件中

这将使我的应用程序出现在所有应用程序的共享列表中,但我希望我的应用程序出现在另一个特定应用程序的共享列表中,而我不拥有其他应用程序

Bha*_*ani 13

在此处输入图片说明- 将以下代码添加到特定活动中的项目 AndroidManifest.xml 文件中。

     <activity
                android:name=".MainActivity"
                android:theme="@style/Theme.AppCompat.Light.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="text/plain" />
            </intent-filter>
     </activity>
Run Code Online (Sandbox Code Playgroud)

- 将以下代码行添加到您的项目特定活动中。

    Intent intent = getIntent();
    String action = intent.getAction();
    String type = intent.getType();
    if ("android.intent.action.SEND".equals(action) && type != null && "text/plain".equals(type)) {
                Log.println(Log.ASSERT,"shareablTextExtra",intent.getStringExtra("android.intent.extra.TEXT"));
            }
Run Code Online (Sandbox Code Playgroud)


Dan*_*anO 10

为此,您需要知道应用程序正在创建的Intent并创建一个IntentFilter,它将您的应用程序添加到特定列表中.

意图和过滤器接收隐含意图(Android开发人员)

应用程序可能使用您可以挂钩的特定操作名称.

<intent-filter . . . >
    <action android:name="com.example.project.SHOW_CURRENT" />
    <action android:name="com.example.project.SHOW_RECENT" />
    <action android:name="com.example.project.SHOW_PENDING" />
    . . .
</intent-filter>
Run Code Online (Sandbox Code Playgroud)

或者它可能正在寻找接受某种类型文件的应用程序.

<intent-filter . . . >
    <data android:mimeType="video/mpeg" android:scheme="http" . . . /> 
    <data android:mimeType="audio/mpeg" android:scheme="http" . . . />
    . . .
</intent-filter>
Run Code Online (Sandbox Code Playgroud)

应用程序的名称及其共享内容将帮助我提供更具体的响应.

  • 我希望我的应用只出现在"Google Play App"的共享列表中.当你尝试分享应用程序时.它共享文本,但很多其他应用程序使用相同的东西!我可以过滤文本内容吗?? !! (2认同)

Sor*_*omo 8

从应用程序外部共享内容时,将此代码添加到您要首先打开的活动中,然后在onCreate()中调用此方法

private void onSharedIntent() {
    Intent receiverdIntent = getIntent();
    String receivedAction = receiverdIntent.getAction();
    String receivedType = receiverdIntent.getType();

    if (receivedAction.equals(Intent.ACTION_SEND)) {

        // check mime type 
        if (receivedType.startsWith("text/")) {

            String receivedText = receiverdIntent
                    .getStringExtra(Intent.EXTRA_TEXT);
            if (receivedText != null) {
                //do your stuff
            }
        }

        else if (receivedType.startsWith("image/")) {

            Uri receiveUri = (Uri) receiverdIntent
                    .getParcelableExtra(Intent.EXTRA_STREAM);

            if (receiveUri != null) {
                //do your stuff
                fileUri = receiveUri;// save to your own Uri object

                Log.e(TAG,receiveUri.toString());
            }
        }

    } else if (receivedAction.equals(Intent.ACTION_MAIN)) {

        Log.e(TAG, "onSharedIntent: nothing shared" );
    }
}
Run Code Online (Sandbox Code Playgroud)

在清单中添加它,

 <activity
            android:name="your-package-name.YourActivity">
            <intent-filter>
                <action android:name="android.intent.action.SEND" /> 

                <category android:name="android.intent.category.DEFAULT" />      
                <data android:mimeType="image/*" />
                <data android:mimeType="text/*" />
            </intent-filter>
        </activity>
Run Code Online (Sandbox Code Playgroud)


Abu*_*uod 7

将此添加到您的 mainefist 文件中

<activity android:name=".ShareActivity">
<intent-filter
    android:label="Share with my app">
    <action android:name="android.intent.action.SEND" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>
Run Code Online (Sandbox Code Playgroud)

这个链接可以帮助你


med*_*116 7

这对我来说非常适合获取所有网页,我的应用程序扫描网页上的mp3文件,并设置它们的警报.当您共享网页时,它会打开我的新网址活动:

以下是此代码的结果: 在此输入图像描述

  <activity
        android:name=".NewUrl"
        android:label="@string/title_activity_new_url"
        android:windowSoftInputMode="stateUnchanged">
        <intent-filter>
            <action android:name="android.intent.action.SEND"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <data android:mimeType="text/*"/>
        </intent-filter>
    </activity>
Run Code Online (Sandbox Code Playgroud)

然后,为了接收应用程序中的链接,我从本教程获得了很多信息:http: //code.tutsplus.com/tutorials/android-sdk-receiving-data-from-the-send-intent--mobile-14878


Bik*_*ram 6

我想在上面的答案中添加一些内容。请记住,如果您在活动中使用多个类别,您应该放置另一个意图过滤器以防止覆盖。

例如,以下内容将阻止您的应用程序显示在设备的应用程序列表中,因为它不会将其检测为启动器活动。

不要这样做

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
      
                <!-- DO NOT DO THIS-->

                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="*/*" />
            </intent-filter>

        </activity>
    </application>
Run Code Online (Sandbox Code Playgroud)

相反,请执行以下操作

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">

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

            <!-- USE SEPERATE INTENT FILTER -->

            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="*/*" />
            </intent-filter>

        </activity>
    </application>
Run Code Online (Sandbox Code Playgroud)

注意:根据您的用途更改 mimeType。

结论:如果您在意图过滤器中使用多个类别,请单独使用它们。