【问题标题】:Problems sharing text with the new Android Studio与新的 Android Studio 共享文本时出现问题
【发布时间】:2015-12-16 19:23:06
【问题描述】:

我有一个简单的应用程序,它会在某些时候生成一串文本,我想与安装在手机上的任何其他可以接收一串文本的应用程序(推特、脸书等)分享 这是我正在尝试做的一个示例:

Intent sendIntent = new Intent();
   sendIntent.setAction(Intent.ACTION_SEND);
   sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
   sendIntent.setType("text/plain");
   startActivity(sendIntent);

来自:https://developer.android.com/training/sharing/send.html#send-text-content

但是,Android 工作室说它“无法解决” startActivity 并建议我这样做:

import static android.support.v4.app.ActivityCompat.startActivity;

好的,我这样做了,现在 startActivity() 期待三个输入。由于更新,这似乎是一个新要求。我见过的所有代码示例最多只给它两个输入。期望是:

  1. 一个活动???
  2. 我们定义的意图
  3. 我将保留为空的捆绑/额外选项

如何满足第一个输入?分享功能完成后是否可以重定向回我的应用?

【问题讨论】:

  • 好的,对于任何试图这样做的人来说都是大收获。您必须在 Main.java 中实现它。扩展“Activity”的那个如果您尝试在子例程中调用它(即另一个 .java 类中的单击事件),它将不会合作。

标签: android text share


【解决方案1】:

试试这个

    Intent sharingIntent = new Intent(Intent.ACTION_SEND);
    sharingIntent.setType("text/plain");
    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "This is my text");
    sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "This is the title");
    startActivity(Intent.createChooser(sharingIntent, "Share using"));

设置标题不是强制性的。那么“共享使用”只是一个文本,您可以将其替换为您想要的任何内容。它只是显示所有建议的应用程序

【讨论】:

    猜你喜欢
    • 2018-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-13
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多