【问题标题】:Sending url to ionic android app via webintents from another app通过 webintents 从另一个应用程序将 url 发送到 ionic android 应用程序
【发布时间】:2015-12-27 15:30:42
【问题描述】:

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

我第一次尝试使用插件:

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

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

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

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

.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 文件中放入:

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

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

<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 消息:

很遗憾,MyAppName 已停止。

任何人都可以提出一个解决方案来让共享意图与我的应用程序一起使用......还是我忘记了什么并且做错了什么。

谢谢!

【问题讨论】:

    标签: android cordova ionic-framework ionic


    【解决方案1】:

    我想添加到@axel-napolitano 的答案,以明确说明应在 Ionic 3 应用程序中添加代码的位置。免责声明:我几乎不会编写 Ionic 应用程序。

    在撰写本文时,我目前使用的是 Ionic 3.9。经过大量的试验和错误并拼凑出关于 SO 的答案,我能够得到这个工作(即让另一个应用程序共享到我的 Ionic 应用程序。)

    此时,您应该已经编译了适用于 Android 的应用,并将您的 Android 手机连接到您的计算机,以便应用在其上运行。查找有关如何在物理 Android 设备上运行 ionic 应用程序的文档。

    命令如下:ionic cordova run android -l -c

    您还应该将&lt;intent-filter&gt; 添加到AndroidManifest.xml 中。

    将代码放入 /pages/home/home.ts - 确保您的构造函数注入了“平台”。

    export class HomePage {
    
      constructor(private platform:Platform){
         ...
       }
    ...
    }
    

    创建此方法(在 home.ts 中)以接收应用未处于加载状态时的意图:

    ionViewDidLoad(){
      this.platform.ready().then(() => {
    
        (<any>window).plugins.intent.getCordovaIntent(
          function (Intent) {
            //you should filter on the intents you actually want to receive based on Intent.action
            console.log('intent received on app launch' + JSON.stringify(Intent));
        },
        function () {
            console.log('Error getting cordova intent');
        }
        );
      });
    }
    

    在应用已加载时接收 Intent:

    ionViewDidEnter() {
        this.platform.ready().then(() => {
          (<any>window).plugins.intent.setNewIntentHandler(
            function (Intent) {
                console.log('new intent' + JSON.stringify(Intent) );
            }
        );
    
        });
      }
    

    有趣的事实:“官方”网络意图插件 (darryncambell's) - 我无法开始工作 - 归功于 Napolitano 的插件。 Source

    我最终让 darryncambell 的插件使用相同的方法工作。

    【讨论】:

    • 我无法使 darryncambell 的插件工作。使用您的解决方案,它会立即生效。谢谢
    【解决方案2】:

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

    你可以检查这个: https://github.com/napolitano/cordova-plugin-intent

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

    一些背景:

    要允许第三方应用向您的应用发送内容,您必须在 AndroidManifest.xml 中的 MainActivity 中添加一个意图过滤器。

    <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>
    

    虽然您可以手动执行此操作,但这有一些缺点 - 因此您可能需要一个钩子或类似的东西。如果你去上面的存储库,你会找到这个的例子。

    除了意图过滤器之外,您目前还需要一个插件来允许您 a) 访问 cordova 意图(这对于在应用程序启动时访问发送的内容很重要)并在 onNewIntent 事件被触发时接收通知,如果在您的应用程序运行时发送内容,就会发生这种情况。

    这正是我的插件所做的。它使您可以访问 cordova 意图并允许您处理 onNewIntent 事件。

    例子:

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

    您将收到一个有限的意图对象作为成功的结果。这可以由您的应用处理。

    例子:

    {
        "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,但您将收到一个现成的对象。

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

    【讨论】:

    • 虽然我正在使用 github.com/Initsogar/cordova-webintent 插件并且它对我来说似乎工作正常,但您对 config.xml 中的 AndroidLaunchMode 设置的看法对我非常有帮助。谢谢!
    • @Axel Napolitano,我尝试使用您的插件,但我有意图,但缺少 clipItems。我究竟做错了什么?我的设置与插件页面上的设置完全相同。而且我收到错误消息 getRealPathFromContentUrl() 不是函数。
    • 嘿,过去几个月没有花太多时间在这上面,因为它对我有用。我会看看它,但由于我目前的工作量,我不能保证立即解决。很抱歉:-/
    【解决方案3】:

    发现问题出在我设置 AndroidManifest.xml 文件的方式上。

    我使用了一个额外的活动标签,而我应该在 1 活动中包含意图。

    例如我有:

    <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/activity_name" android:launchMode="singleTop" android:name="MainActivity" android:theme="@android:style/Theme.Black.NoTitleBar" android:windowSoftInputMode="adjustResize">
        <intent-filter android:label="@string/launcher_name">
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <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>
    

    我应该有的时候:

    <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/activity_name" android:launchMode="singleTop" android:name="MainActivity" android:theme="@android:style/Theme.Black.NoTitleBar" android:windowSoftInputMode="adjustResize">
         <intent-filter android:label="@string/launcher_name">
             <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>
    

    【讨论】:

      猜你喜欢
      • 2014-11-01
      • 2016-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-20
      相关资源
      最近更新 更多