Ionic/Cordova:使用config.xml添加intent-filter

Mar*_*zzo 12 cordova ionic

我正在使用Ionic Framework(基于Cordova)开发移动应用程序.

在Android中,我注册我的应用程序以打开*.txt文件.我在平台/ android/AndroidManifest.xml中添加了intent-filter,它可以工作.但是平台文件夹在.gitignore中:我想用config.xml来做.

我尝试在config.xml中添加:

 <platform name="android">
    <config-file target="AndroidManifest.xml" parent="/*/application/activity">
      <intent-filter><!-- ... --></intent-filter>
    </config-file>
    <!-- ... -->
 </platform>
Run Code Online (Sandbox Code Playgroud)

我还尝试添加:

 <platform name="android">
    <config-file target="AndroidManifest.xml" parent="/manifest/application">
      <activity android:name="CordovaApp"> 
        <intent-filter><!-- ... --></intent-filter>
      </activity>
    </config-file>
    <!-- ... -->
 </platform>
Run Code Online (Sandbox Code Playgroud)

然后我尝试更新AndroidManifest启动

ionic prepare
Run Code Online (Sandbox Code Playgroud)

或者:

ionic remove platform android && ionic add platform android
Run Code Online (Sandbox Code Playgroud)

但AndroidManifest.xml始终保持不变.我究竟做错了什么?

我正在使用Ionic 1.3.2和Cordova 4.2.0.

在这里编辑整个config.xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<widget id="com.ionicframework.myapp551932" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0" xmlns:android="http://schemas.android.com/apk/res/android">
  <name>MyApp</name>
  <description>
        myApp
    </description>
  <author email="xxx@yyy.it" href="http://www.example.com/">
      A Team
    </author>
  <content src="index.html"/>
  <access origin="*"/>
  <preference name="webviewbounce" value="false"/>
  <preference name="UIWebViewBounce" value="false"/>
  <preference name="DisallowOverscroll" value="true"/>
  <preference name="BackupWebStorage" value="none"/>
  <preference name="SplashScreen" value="screen"/>
  <preference name="SplashScreenDelay" value="3000"/>
  <feature name="StatusBar">
    <param name="ios-package" value="CDVStatusBar" onload="true"/>
  </feature>
  <platform name="android">
    <config-file target="AndroidManifest.xml" parent="/manifest/application/activity">
      <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="file" />
        <data android:mimeType="*/*" />
        <data android:pathPattern=".*\\.txt" />
        <data android:host="*" />
      </intent-filter>
      <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:scheme="content" />
        <data android:pathPattern=".*\\.txt" />
        <data android:mimeType="*/*" />
      </intent-filter>
    </config-file>
    <icon src="resources/android/icon/drawable-ldpi-icon.png" density="ldpi"/>
    <icon src="resources/android/icon/drawable-mdpi-icon.png" density="mdpi"/>
    <icon src="resources/android/icon/drawable-hdpi-icon.png" density="hdpi"/>
    <icon src="resources/android/icon/drawable-xhdpi-icon.png" density="xhdpi"/>
    <icon src="resources/android/icon/drawable-xxhdpi-icon.png" density="xxhdpi"/>
    <icon src="resources/android/icon/drawable-xxxhdpi-icon.png" density="xxxhdpi"/>
    <splash src="resources/android/splash/drawable-land-ldpi-screen.png" density="land-ldpi"/>
    <splash src="resources/android/splash/drawable-land-mdpi-screen.png" density="land-mdpi"/>
    <splash src="resources/android/splash/drawable-land-hdpi-screen.png" density="land-hdpi"/>
    <splash src="resources/android/splash/drawable-land-xhdpi-screen.png" density="land-xhdpi"/>
    <splash src="resources/android/splash/drawable-land-xxhdpi-screen.png" density="land-xxhdpi"/>
    <splash src="resources/android/splash/drawable-land-xxxhdpi-screen.png" density="land-xxxhdpi"/>
    <splash src="resources/android/splash/drawable-port-ldpi-screen.png" density="port-ldpi"/>
    <splash src="resources/android/splash/drawable-port-mdpi-screen.png" density="port-mdpi"/>
    <splash src="resources/android/splash/drawable-port-hdpi-screen.png" density="port-hdpi"/>
    <splash src="resources/android/splash/drawable-port-xhdpi-screen.png" density="port-xhdpi"/>
    <splash src="resources/android/splash/drawable-port-xxhdpi-screen.png" density="port-xxhdpi"/>
    <splash src="resources/android/splash/drawable-port-xxxhdpi-screen.png" density="port-xxxhdpi"/>
  </platform>
  <icon src="resources/android/icon/drawable-xhdpi-icon.png"/>
</widget>
Run Code Online (Sandbox Code Playgroud)

Mar*_*zzo 20

解决!

我无法使用Ionic或Cordova:它是PhoneGap功能(请参阅此Stackoverflow答案)

我可以通过其他两种方式来做到这一点:

  1. 使用自定义Cordova插件
  2. 使用钩子

我更喜欢第二种方式.我找到了一个有趣的钩子用于我的目的.注意:Rembember安装一些包:

npm install lodash elementtree plist --save-dev
Run Code Online (Sandbox Code Playgroud)

可悲的是,这个钩子合并了标签.所以我写了这个钩子的一个小改变版本:见这里.你可以将这个钩子放在/ hooks/after_platform_add中.

现在我在config.xml中有了intent-filter配置:

  <platform name="android">
    <config-file target="AndroidManifest.xml" parent="application/activity">
      <intent-filter><!-- ... --></intent-filter>
    </config-file>
    <!-- ... -->
  </platform>
Run Code Online (Sandbox Code Playgroud)

我可以更新AndroidManifest.xml重新生成的android平台:

ionic platform remove android && ionic platform add android
Run Code Online (Sandbox Code Playgroud)

  • 对于那些不是XML爱好者,如果你得到`xml unbound prefix`的cordova构建错误,那么你需要添加`xmlns:android ="http://schemas.android.com/apk/res/android"`到配置顶部的<widget>元素 (10认同)

Ric*_*ich 8

我有同样的问题,但安装(然后记住或记录依赖)一堆npm依赖关系,然后使用一个大的通用钩子的想法是太重量级我所需要的.

钩子可以是简单的shell脚本,这通常是一种更简单的修改文本文件的方式.在我的情况下,我只需要为活动添加一个intent-filter,MainActivity这是一项微不足道的工作sed; 我刚刚创建了hooks/after_prepare/020_add_moozvine_intents.sh包含内容的文件:

#!/usr/bin/env zsh

MANIFEST=${0:h}/../../platforms/android/AndroidManifest.xml
[[ -e $MANIFEST ]] || { print "Manifest not found at $MANIFEST." ; exit 1; }

grep -q HANDLE_MOOZVINE_NOTIFICATION $MANIFEST && { print "Manifest already modified. Nothing to do."; exit 0; }

AFTER_LINE='android:name="MainActivity"'
ADDITION='\
        <intent-filter>\
            <action android:name="HANDLE_MOOZVINE_NOTIFICATION" />\
            <category android:name="android.intent.category.DEFAULT" />\
        </intent-filter>
';

sed -i -e "/${AFTER_LINE}/a${ADDITION}" $MANIFEST
Run Code Online (Sandbox Code Playgroud)

任务完成.您可以使用类似的方法对生成的文件进行任何简单的文本修改.


Fro*_*y Z 8

Cordova 9现在直接支持 using<config-file><edit-config>部分,就像在plugin.xml文件中一样。

在不使用任何插件或钩子的情况下,您可以直接执行以下操作,例如在AndroidManifest.xml 中添加一个意图过滤器:

<?xml version='1.0' encoding='utf-8'?>
<widget id="yourdomain.app" version="1.7.8" xmlns="http://www.w3.org/ns/widgets" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:cdv="http://cordova.apache.org/ns/1.0">
    <!-- ... -->
    <platform name="android">
        <!-- ... -->
        <config-file parent="application" target="AndroidManifest.xml">
            <activity android:label="webIntentFilter" android:name="yourdomain.app">
                <intent-filter android:autoVerify="true">
                    <action android:name="android.intent.action.VIEW" />
                    <category android:name="android.intent.category.DEFAULT" />
                    <category android:name="android.intent.category.BROWSABLE" />
                    <data android:host="yourdomain.com" android:scheme="https" />
                </intent-filter>
            </activity>
        </config-file>
    </platform>
    <!-- ... -->
</widget>
Run Code Online (Sandbox Code Playgroud)

不要忘记xmlns:android="http://schemas.android.com/apk/res/android"<widget>标签中添加属性以避免unbound prefix构建时出错。

  • 如何使用它启动应用程序后获取 url 中的数据? (2认同)

Chr*_*rra 6

以下是Rich在JS中为未来Google员工编写的上述解决方案,因为我遇到了shell脚本问题.

module.exports = function (context) {
    const fs = require('fs');
    const _ = require('lodash');

    const scheme = 'flowkey';
    const insertIntent = `
    <intent-filter>
                <action android:name="android.intent.action.VIEW"></action>
                <category android:name="android.intent.category.DEFAULT"></category>
                <category android:name="android.intent.category.BROWSABLE"></category>
                <data android:scheme="${scheme}"></data>
    </intent-filter>
    `;
    const manifestPath = context.opts.projectRoot + '/platforms/android/AndroidManifest.xml';
    const androidManifest = fs.readFileSync(manifestPath).toString();
    if (!androidManifest.includes(`android:scheme="${scheme}"`)) {
        const manifestLines = androidManifest.split(/\r?\n/);
        const lineNo = _.findIndex(manifestLines, (line) => line.includes('@string/activity_name'));
        manifestLines.splice(lineNo + 1, 0, insertIntent);
        fs.writeFileSync(manifestPath, manifestLines.join('\n'));
    }
};
Run Code Online (Sandbox Code Playgroud)

使用此作为您的准备后挂钩.

注意:这是在ES6中,您可以在这里找到ES5版本:https://gist.github.com/smowden/f863331034bf300b960beef1ae25bf82