通过来自另一个应用程序的 webintents 将 url 发送到 ionic android 应用程序 [英] Sending url to ionic android app via webintents from another app

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

问题描述

正在寻找更新的解决方案,运行使用 Cordova 5.x 的最新 ionic 1.1.0 版本.试图能够在 chrome 中浏览网站并使用 web 意图将该 url 发送到我的 ionic 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.

我第一次尝试使用插件:

I first attempted to use the plugin:

ionic 插件添加 https://github.com/Initsogar/cordova-webintent

ionic plugin add https://github.com/Initsogar/cordova-webintent

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

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

ionic 插件添加 https://github.com/fluentstream/cordova-webintent

ionic plugin add https://github.com/fluentstream/cordova-webintent

在我的 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
      }
    }); 
  });
})

在文件 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:

不幸的是,MyAppName 已停止.

Unfortunately, MyAppName has stopped.

任何人都可以提出一个解决方案来让共享意图与我的应用一起工作......或者我是否忘记了某些事情并做错了什么.

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 上的 Intent.

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.

您可以检查一下:https://github.com/napolitano/cordova-plugin-intent

虽然我仍在为我自己的项目开发这个插件,但它几乎可以使用,而且文档应该足以让我涉足.

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 添加一个意图过滤器.

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.

除了意图过滤器之外,您目前需要一个插件,允许您 a) 访问cordova意图(这对于在应用程序启动时访问发送的内容很重要)并在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 意图并允许您处理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.

请注意,我目前仅根据自己的需要开发插件.但是,它也可能对您有用.目前没有在自述文件中添加角度示例,但我认为这应该不是一个大问题.

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 发送到 ionic android 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆