【问题标题】:Open and Send data to another android application打开并将数据发送到另一个 android 应用程序
【发布时间】:2014-09-12 09:01:53
【问题描述】:

有人知道如何打开数据并将数据发送到另一个应用程序吗?

我有一个要发送的字符串,我使用了 ACTION.MAIN 和 putExtra 来发送字符串:

 String smth = "Test";

 Intent intents = new Intent(Intent.ACTION_MAIN);
 intents.setComponent(new ComponentName("com.package.address","com.package.address.HelloGlassActivity"));
 intents.putExtra("STRING", smth);
 mContext.startActivity(intents);

我在 Android manifest 中声明了另一个项目(Application)的意图:

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

在 Android Manifest 中声明意图后,我在我的一项活动中调用该意图:

  Intent intent = getIntent();
  String tester = intent.getStringExtra("STRING");

不幸的是,当我运行第一个应用程序时,我无法打开第二个应用程序。有人知道我错过了什么吗?

【问题讨论】:

    标签: java android eclipse android-intent


    【解决方案1】:

    只需使用startActivity(Intent intent) 方法将该意图发送到应用程序。您还可以向意图添加特定类型,以便定义哪些活动可以捕获它。

    【讨论】:

      【解决方案2】:

      我想你已经在 manifestfile 中声明了你的第二个活动。

      【讨论】:

        【解决方案3】:

        intent分为两类,一类是显式intent,就像你做的那样,你设置具体的组件,但它只在同一个应用程序中的组件时有效。二是隐式intent,不要命名具体的组件,而是而是声明要执行的一般操作,它允许来自另一个应用程序的组件来处理它。这意味着只需构建 带有动作的意图,活动将根据您在意图过滤器中声明的动作来捕获它。您混合了两种类型的意图。

        【讨论】:

        • 嗨@ben:所以我不必指定特定的组件?那我怎样才能得到意图呢?
        • 通过在intent-filter.likenew Intent("my action string")...&lt;intent-filter&gt; &lt;action android:name="my action string" /&gt; &lt;category android:name="android.intent.category.DEFAULT" /&gt; &lt;data android:mimeType="text/plain" /&gt; &lt;/intent-filter&gt;中定义action字符串
        【解决方案4】:

        使用Content Provider。然后要从应用程序B 访问应用程序A 中的文件,您可以在应用程序A 中声明ContentProvider 并覆盖public ParcelFileDescriptor openFile(Uri uri, String mode) 方法。然后应用程序B 可以使用getContentResolver().openInputStream(...) 方法通过适当的uri 访问位于应用程序A 的数据目录中的文件。

        欲了解更多信息,请参阅:

        http://www.grokkingandroid.com/handling-binary-data-with-contentproviders/

        https://stackoverflow.com/a/4336013/148272

        【讨论】:

          猜你喜欢
          • 2014-11-01
          • 1970-01-01
          • 2020-06-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多