【问题标题】:How to make my app appear in share board of other apps - example whatsApp如何让我的应用出现在其他应用的共享板中 - 例如whatsApp
【发布时间】:2020-11-22 22:45:12
【问题描述】:

我正在尝试读取从 WhatsApp 导出到我的 Android 应用程序的聊天文本文件。 当我尝试将聊天文本文件导出到我的应用程序时,它没有出现在 WhatsApp 的共享板中,尽管我已将以下内容添加到 manifest.xml:

  <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.SEND" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="text/plain" />
        </intent-filter>
</activity>

另外,这里是 Java 代码:

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent intent = getIntent();
    String action = intent.getAction();
    String type = intent.getType();


    if (Intent.ACTION_SEND.equals(action) && type != null) {
        if ("text/plain".equals(type)) {
            handleSendText(intent); // Handle text being sent
        } else {
            // Handle other intents, such as being started from the home screen
        }
    }
}


void handleSendText(Intent intent) {
    String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
    if (sharedText != null) {

        Log.d(TAG, sharedText);
        // Update UI to reflect text being shared
    }
}}

我经历了很多解释,但我无法弄清楚问题所在。谢谢。

【问题讨论】:

  • 您能否更新问题,使其也显示意图过滤器的 父标签?
  • 好的。我更新它。

标签: android android-studio android-layout android-intent text


【解决方案1】:

您是否在 AndroidManifest.xml 中正确的活动下添加了意图过滤器?

这在理论上应该可行,但某些应用可能不会为您的应用发送正确的意图。 WhatsApp 可能使用的是“VIEW”而不是“SEND”,因此请尝试以下操作:

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

    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    
    <data android:mimeType="text/plain" />
</intent-filter>

让我知道它是否适合你。

【讨论】:

  • 您能否检查您尝试从 WhatsApp 接收的文件的 mimeType 是否真的是“text/plain”?
  • 是的...我 100% 确定
猜你喜欢
  • 2013-11-18
  • 2016-09-09
  • 1970-01-01
  • 2012-06-21
  • 2014-02-07
  • 1970-01-01
  • 1970-01-01
  • 2023-03-04
  • 1970-01-01
相关资源
最近更新 更多