2015-01-28 10 views
8

Pracuję nad aplikacją mobilną, korzystając z Ionic Framework (opartego na Cordova).Ionic/Cordova: Dodaj filtr intencyjny za pomocą pliku config.xml

W systemie Android rejestruję aplikację, aby otworzyć pliki * .txt. Robię to dodając filtr intencji na platformach/android/AndroidManifest.xml i to działa. Ale folder platformy znajduje się w .gitignore: Chcę to zrobić za pomocą pliku config.xml.

Próbowałem dodając config.xml:

<platform name="android"> 
    <config-file target="AndroidManifest.xml" parent="/*/application/activity"> 
     <intent-filter><!-- ... --></intent-filter> 
    </config-file> 
    <!-- ... --> 
</platform> 

Próbowałem również dodając:

<platform name="android"> 
    <config-file target="AndroidManifest.xml" parent="/manifest/application"> 
     <activity android:name="CordovaApp"> 
     <intent-filter><!-- ... --></intent-filter> 
     </activity> 
    </config-file> 
    <!-- ... --> 
</platform> 

Potem próbowałem zaktualizować AndroidManifest uruchomienie

ionic prepare 

czy także:

ionic remove platform android && ionic add platform android 

Ale AndroidManifest.xml jest zawsze niezmieniony. Co robię źle?

Używam Ionic 1.3.2 i Cordova 4.2.0.

Edit tu cały 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="[email protected]" 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> 
+0

można pokazać całą swoją 'config.xml' lub przynajmniej nagłówek pliku xml? –

+0

Tutaj jest (wszystko dodane przez Ionic za wyjątkiem znacznika pliku konfiguracyjnego). –

Odpowiedz

16

rozstrzygnięte!

nie mogę zrobić za pomocą jonowych lub Cordova: jest to cecha PhoneGap (zobacz Stackoverflow answer)

mogę to zrobić w dwa inne sposoby:

  1. Używanie niestandardowej Cordova wtyczki
  2. Korzystanie z haka

Ja preferowałem drugi sposób. Znalazłem interesting hook dla moich celów. Uwaga: Rembember zainstalować niektórych pakietów:

npm install lodash elementtree plist --save-dev 

Niestety ten hak łączy tagi. Więc napisałem trochę zmienioną wersję tego haka: see here. Możesz umieścić ten hak w/hooks/after_platform_add.

Teraz mam zamiar konfigurację filtra w config.xml:

<platform name="android"> 
    <config-file target="AndroidManifest.xml" parent="application/activity"> 
     <intent-filter><!-- ... --></intent-filter> 
    </config-file> 
    <!-- ... --> 
    </platform> 

I mogę zaktualizować AndroidManifest.xml regenerujący platformie Android:

ionic platform remove android && ionic platform add android 
+0

Kod haka jest niedostępny. Dlaczego nie skorzystać z porad GitHub? – neoascetic

+1

Ok. Przeniesiony do dna GitHub :) –

+0

Dzięki za to – pgorsira

5

miałem ten sam problem, ale pomysł instalacji (a następnie zapamiętywania lub dokumentowania zależności) na kilka zależności npm i używanie dużego haka ogólnego przeznaczenia był zdecydowanie za ciężki do tego, czego potrzebowałem.

Haczyki mogą być prostymi skryptami powłoki, co jest często znacznie prostszym sposobem modyfikacji plików tekstowych.W moim przypadku wystarczy dodać filtr zamiaru do działania MainActivity, co jest banalną pracą dla sed; Właśnie utworzyłem plik hooks/after_prepare/020_add_moozvine_intents.sh z treścią:

#!/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 

Zadanie zostało wykonane. Podobne podejście można zastosować do wszelkich prostych modyfikacji tekstowych wygenerowanych plików.

+0

Szukałem tej odpowiedzi przez wiele godzin. Dziękujemy za udostępnienie tutaj. Nie mogłem o tym myśleć z powodu braku wiedzy na temat skryptów powłoki, ale ... działa dobrze, dziękuję bardzo! –

5

Oto powyższe rozwiązanie Richa napisane w JS dla przyszłych pracowników Google, ponieważ miałem problemy ze skryptem powłoki.

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')); 
    } 
}; 

Użyj tego jako haczyka po przygotowaniu.

Uwaga: to jest w ES6 można znaleźć wersję ES5 tutaj: https://gist.github.com/smowden/f863331034bf300b960beef1ae25bf82

+0

To rozwiązanie sprawdziło się bardzo dobrze, gdy umieściłem skrypt w scripts/cordova/hooks/android i dodałem odpowiedni hook after_prepare w config.xml –

Powiązane problemy