J'essaie de développer un plugin Android à utiliser dans une application Ionic.
J'ai créé l'application avec un projet de démarrage déjà fourni par Ionic en utilisant onglets ionic start myApp
. p >
J'ai également créé, dans un dossier différent, mon plugin, qui contient les fichiers suivants.
plugin.xml
package android.package.locationmanager; import org.apache.cordova.CallbackContext; import org.apache.cordova.CordovaInterface; import org.apache.cordova.CordovaWebView; import org.apache.cordova.CordovaPlugin; import org.json.JSONArray; import org.json.JSONObject; import org.json.JSONException; import android.annotation.SuppressLint; import android.content.Context; import android.location.Location; import android.location.LocationManager; public class GSLocationManager extends CordovaPlugin { private JSONObject data = new JSONObject(); // at the initialize function, we can configure the tools we want to use later, like the sensors @Override public void initialize(CordovaInterface cordova, CordovaWebView webView) { super.initialize(cordova, webView); } // safety unregistering from the events if the application stops somehow @Override public void onDestroy() { } // this is the main part of the plugin, we have to handle all of the actions sent from the js @Override public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { if ("getCurrentLocation".equals(action)) { Location location = getCurrentLocation(); JSONObject json = new JSONObject(); json.put("latitude", location.getLatitude()); json.put("longitude", location.getLongitude()); callbackContext.success(json); return true; } return false; // Returning false results in a "MethodNotFound" error. } @SuppressLint("MissingPermission") private Location getCurrentLocation() { LocationManager locationManager = (LocationManager) cordova.getActivity().getApplicationContext().getSystemService(Context.LOCATION_SERVICE); Location lastKnownGPSLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); return lastKnownGPSLocation; } }
locationManager.js
var GSLocationManager = { getCurrentLocation: function(success, failure) { exec(success, failure, 'GSLocationManager', 'getCurrentLocation', []); } } module.exports = GSLocationManager;
GSLocationManager.java
<?xml version="1.0" encoding="UTF-8"?> <plugin xmlns="http://apache.org/cordova/ns/plugins/1.0" id="location-plugin" version="0.0.1"> <name>GSLocationManager</name> <description>Location Plugin</description> <license>MIT</license> <keywords>cordova,device,sensors,location</keywords> <js-module name="LocationManager" src="www/LocationManager.js"> <clobbers target="LocationManager" /> </js-module> <engines> <engine name="cordova" version=">=3.6.0"></engine> </engines> <platform name="android"> <preference name="GOOGLE_PLAY_SERVICES_VERSION" default="11+"/> <preference name="ANDROID_SUPPORT_LIBRARY_VERSION" default="26+"/> <preference name="ICON" default="@mipmap/icon" /> <preference name="SMALL_ICON" default="@mipmap/icon" /> <preference name="ACCOUNT_NAME" default="@string/app_name" /> <preference name="ACCOUNT_LABEL" default="@string/app_name" /> <preference name="ACCOUNT_TYPE" default="$PACKAGE_NAME.account" /> <preference name="CONTENT_AUTHORITY" default="$PACKAGE_NAME" /> <framework src="com.google.android.gms:play-services-location:$GOOGLE_PLAY_SERVICES_VERSION" /> <framework src="com.android.support:support-v4:$ANDROID_SUPPORT_LIBRARY_VERSION" /> <framework src="com.android.support:appcompat-v7:$ANDROID_SUPPORT_LIBRARY_VERSION" /> <!-- <framework src="android/dependencies.gradle" custom="true" type="gradleReference"/> --> <source-file src="path/to/locationmanager/GSLocationManager.java" target-dir="path/to/locationmanager"></source-file> <config-file target="AndroidManifest.xml" parent="/manifest"> <uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" /> <uses-permission android:name="android.permission.READ_SYNC_SETTINGS" /> <uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.hardware.location" /> </config-file> <config-file target="res/xml/config.xml" parent="/*"> <feature name="GSLocationManager"> <param name="android-package" value="android.package.locationmanager.GSLocationManager" /> </feature> </config-file> </platform>
Ensuite, j'ai créé un package.json pour mon plugin en utilisant plugman
Et après cela, j'ai ajouté mon plugin Android à mon projet Ionic en faisant
ionic cordova plugin add path / to / my / plugin
Mais il semble que je n'arrive pas à importer mon plugin dans app.module.ts code> en utilisant
importer {GSLocationManager} depuis 'globespinning-location-plugin'
J'essaye de construire mais la CLI dit ERROR dans src / app / app.module.ts (12,31): erreur TS2307: Impossible de trouver le module 'globespinning-location-plugin'.
Une idée d'où se trouve l'erreur?
Comment importer un plugin que j'ai créé et référencé à partir d'un dossier local?
Je souhaite pouvoir appeler GSLocationManager.getCurrentLocation ()
dans mon application ionique.
4 Réponses :
Si l'importation échoue, cela pourrait être très probablement lié à votre tsconfig.json ou à votre module angulaire (app.module.ts). Vérifiez si le fichier est inclus et non.
Vous n'avez publié que vos fichiers Java. Êtes-vous sûr que cela fonctionne sur un navigateur?
Je n'ai posté qu'un seul fichier java. J'ai posté les fichiers de mon plugin. Je ne suis pas sûr que le fichier js soit correct.
Vous voulez continuer et exécuter ionic cordova plugin ajouter "chemin du dossier de votre plugin personnalisé"
après cela dans le fichier .ts
que vous voulez l'utiliser en vous voulez déclarer ce qui suit, declare var myPlugin: any;
Une fois que vous avez déclaré le plugin, vous pouvez continuer et l'utiliser comme suit:
declare var GSLocationManager: any; .... GSLocationManager.getCurrentLocation().then((data) => {});
Dans votre cas, vous utiliseriez donc ce qui suit:
myPlugin.myFuntion((data) => { console.log(data); },(err) => { console.log(err); });
Outre la réponse @tachyon , voici quelques éléments à noter.
import * from *
. ce n'est pas obligatoire dans ce cas.
Par conséquent, voici comment mettre en œuvre un tel dans votre cas;
Dans le fichier .TS vous souhaitez exécuter la fonction
// at the top of the file. after all the import statements import { Component } from '@angular/core'; // i'm using this as an example. import { Platform } from '@ionic/angular'; // you need the platform declare var LocationManager; ... constructor( private platform: Platform ){ this.platform.ready().then(() => { // we will make use of the plugin here console.log('LocationManager: ', LocationManager); // we can check if it exist by logging it out to the console if (LocationManager) { LocationManager.GSLocationManager.getCurrentLocation( success => { console.log('LocationManager: Got Location: ', success); }, error => { console.log('LocationManager: Err Getting Location: ', error); } ); } }); }
vous pouvez en savoir plus ici: Comment utiliser un plugin sans utiliser ionic-native? a >, https://cordova.apache.org/docs/en/latest/ plugin_ref / spec.html
La réponse de @Tachyon et @SeunBincom a été utile, mais le problème venait de mon fichier plugin.xml
. Avec l'aide d'Android Studio, j'ai pu le déboguer et finalement le faire fonctionner.
Le xml s'est terminé comme suit:
<?xml version="1.0" encoding="UTF-8"?> <plugin xmlns="http://apache.org/cordova/ns/plugins/1.0" id="globespinning-location-plugin" version="0.0.1"> <name>LocationManager</name> <description>Globespinning Location Plugin</description> <license>MIT</license> <keywords>cordova,device,sensors,location</keywords> <js-module name="LocationManager" src="www/locationManager.js"> <clobbers target="LocationManager" /> </js-module> <platform name="android"> <preference name="GOOGLE_PLAY_SERVICES_VERSION" default="11+"/> <preference name="ANDROID_SUPPORT_LIBRARY_VERSION" default="26+"/> <preference name="ICON" default="@mipmap/icon" /> <preference name="SMALL_ICON" default="@mipmap/icon" /> <preference name="ACCOUNT_NAME" default="@string/app_name" /> <preference name="ACCOUNT_LABEL" default="@string/app_name" /> <preference name="ACCOUNT_TYPE" default="$PACKAGE_NAME.account" /> <preference name="CONTENT_AUTHORITY" default="$PACKAGE_NAME" /> <framework src="com.google.android.gms:play-services-location:$GOOGLE_PLAY_SERVICES_VERSION" /> <framework src="com.android.support:support-v4:$ANDROID_SUPPORT_LIBRARY_VERSION" /> <framework src="com.android.support:appcompat-v7:$ANDROID_SUPPORT_LIBRARY_VERSION" /> <framework src="src/android/dependencies.gradle" custom="true" type="gradleReference"/> <source-file src="src/android/com/globespinning/ionic/locationmanager/GSLocationManager.java" target-dir="src/com/globespinning/ionic/locationmanager"></source-file> <config-file target="AndroidManifest.xml" parent="/manifest"> <uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" /> <uses-permission android:name="android.permission.READ_SYNC_SETTINGS" /> <uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.hardware.location" /> </config-file> <config-file target="res/xml/config.xml" parent="/*"> <feature name="LocationManager"> <param name="android-package" value="com.globespinning.ionic.locationmanager.GSLocationManager" /> </feature> </config-file> </platform> </plugin>