通过webintents从另一个应用程序发送url到离子安卓应用

通过webintents从另一个应用程序发送url到离子安卓应用

本文介绍了通过webintents从另一个应用程序发送url到离子安卓应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

寻找更新的解决方案,运行使用Cordova 5.x的最新离子1.1.0版本。试图能够浏览Chrome中的网站并使用网络意图将该网址发送到我的离子Android应用程序。我的应用程序编译并运行,但是当我尝试使用Chrome(或任何其他应用程序)的共享功能并选择我的应用程序共享时,我的应用程序崩溃。

Looking for an updated solution, running the latest ionic 1.1.0 release which uses Cordova 5.x. Trying to be able to browse a website in chrome and send that url to my ionic android app using web intent. My app compiles and runs, however when i attempt to use the share to feature from chrome(or any other app) and choose my app to share to, my app crashes.

我首先尝试使用插件:

离子插件添加

然后删除了插件,我还尝试了最近更新的fork:

and then removed the plugin and i also tried a more recently updated fork:

离子插件添加

在我的app.js文件中,我输入以下代码:

In my app.js file I putting the following code:

.run(function($ionicPlatform, $rootScope, $ionicHistory, $state) {
  $ionicPlatform.ready(function() {
    window.plugins.webintent.getExtra(window.plugins.webintent.EXTRA_TEXT,
    function(url) {
      incomingURL = url;
      //alert(incomingURL);
      console.log(incomingURL);
    }, function() {
      incomingURL = false;
      //alert("no url");
      console.log("no url");
    });
  });
})

我也试过:

.run(function($ionicPlatform, $rootScope, $ionicHistory, $state) {
  $ionicPlatform.ready(function() {
    window.plugins.webintent.getUri(function(url) {
      if(url !== "") {
          alert(url);//url is the url the intent was launched with
      }
    });
  });
})

In文件config.xml我会把:

In the file config.xml I would put:

<plugin name="WebIntent" value="com.borismus.webintent.WebIntent"/>

在AndroidManifest.xml中我会手动输入:

In the AndroidManifest.xml I would manually put in:

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

该应用运行,但当我转到chrome并点击分享按钮,然后选择我的应用,应用程序关闭以下android消息出现:

The app runs, but when I go to chrome and click the share button, and then choose my app, the app shuts down the following android message appears:

任何人都可以建议一个解决方案,让股票意图与我的应用程序一起工作......或者我忘记了某些事情并做错了什么。

Can anybody suggest a solution to getting the share to intent to work with my app...or am i forgetting something and doing something wrong.

谢谢!

推荐答案

这个问题有点老了。但我有一个或多或少类似的挑战。我没有使用WebIntent插件,因此我开发了自己的插件来处理Android上的Intents。

This question is a bit aged. But i had a more or less similar challenge. I had no luck with the WebIntent plugin and therefore i developed my own plugin for dealing with Intents on Android.

你可以检查一下:

虽然我仍然在为自己的项目开发这个插件,但它几乎可用,而且文档应该足够好以便进入大门。

While i'm still working on this plugin for my own projects, it's yet almost usable and documentation should be good enough to get a foot into the door.

一些背景知识:

要允许第三方应用向您的应用发送内容,您必须在AndroidManifest.xml中为MainActivity添加一个intent-filter。

To allow third party apps to send content to your app, you must add an intent-filter to your MainActivity in the AndroidManifest.xml.

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

虽然你可以手动执行此操作,但这有一些缺点 - 所以你可能想要一个钩子或相似的东西。如果你去上面的存储库,你会发现并为此做出示例。

While you can do this manually, this has some disadvantages - so you probably want to have a hook or something similar. If you go to the repository above, you'll find and example for this.

除了intent-filter之外,你当前需要一个插件,允许你a)到访问cordova intent(这对于在应用程序启动时访问已发送内容很重要)并在触发 onNewIntent 事件时接收通知,如果在您的应用程序正在运行。

Additionally to the intent-filter, you currently need an plugin that allows you a) to access the cordova intent (that's important to get access to sent content on application startup) and to receive notifications if the onNewIntent event was triggered, which happens, if content is sent while your application is running.

这是我的插件所做的事情。它允许您访问cordova intent并允许您处理 onNewIntent 事件。

That's exaclty what my plugin does. It gives you access to the cordova intent and allows you to handle the onNewIntent event.

示例:

window.plugins.intent.getCordovaIntent(function (Intent) {
    console.log(Intent);
}, function () {
    console.log('Error');
});

window.plugins.intent.setNewIntentHandler(function (Intent) {
    console.log(Intent);
});

成功后,您将收到有限的意向对象。这可以由您的应用处理。

You will receive a limited intent object as result on success. This can be processed by your app.

示例:

{
    "action": "android.intent.action.SEND_MULTIPLE",
    "clipItems": [
        {
            "uri": "file:///storage/emulated/0/Download/example-document.pdf"
        },
        {
            "uri": "file:///storage/emulated/0/Download/example-archive.zip"
        }
    ],
    "flags": 390070273,
    "type": "*/*",
    "component": "ComponentInfo{com.example.droid/com.example.droid.MainActivity}",
    "extras": "Bundle[mParcelledData.dataSize=596]"
}

虽然这个例子实际上显示了JSON,但你会收到一个现成的对象。

While this example actually shows JSON, you will receive a ready-to-use object.

请注意我目前只为我自己的需要开发插件。但它也可能对你有用。目前README中没有添加任何角度示例,但我认为这不应该是一个大问题。

Please note that i develop the plugin currently only for my own needs. However it may work also for you. Currently no angular examples were added to the README, but i think that should not be a big problem.

这篇关于通过webintents从另一个应用程序发送url到离子安卓应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 12:47