phonegap-pluginsを使っためも
phonegap-pluginsを使っためも。
PhoneGap(Android版)のJavascript上からIntentを呼び出す必要があったので。。。
参考サイト
PhoneGapプラグインの作り方
http://wiki.phonegap.com/w/page/36753494/How-to-Create-a-PhoneGap-Plugin-for-Android
phonegap-plugins作者のブログ
http://borismus.com/android-phonegap-plugins/
phonegap-plugins github
https://github.com/borismus/phonegap-plugins
phonegapの動作環境を準備
http://www.phonegap.com/start#androidを参考に環境を構築。
phonegap-pluginを適用
phonegap-plugin ダウンロード
phonegap-pluginをダウンロードします。
https://github.com/borismus/phonegap-plugins
今回はIntentの機能を追加するため、ダウンロードしたファイルのうち、「phonegap-plugins / Android / WebIntent」を利用します。
pluginの追加
com.borismus.webintentというパッケージ名で、ダウンロードしたファイル「phonegap-plugins / Android / WebIntent/WebIntent.java」を追加します。
このWebIntent.javaがPhoneGapのプラグインの実装ファイルになります。
また、PhoneGapからプラグインを利用するとき、javascriptからクラス名を指定して読み込みを行うため、パッケージ名が異なるとエラーとなるのでご注意ください。
javascriptファイルの追加
「asset/www」あたりにダウンロードしたファイル「phonegap-plugins / Android / WebIntent/webintent.js」を追加します。
このwebintent.jsの中で、PhoneGapにプラグインの追加を行っています。
実行例
index.htmlで実行してみます。
- asset/www/index.html
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title> PhoneGap </title> <script type="text/javascript" charset="utf-8" src="phonegap.js"></script> <script type="text/javascript" charset="utf-8" src="webintent.js"></script> <script type="text/javascript" chareset="utf-8"> // Wait for PhoneGap to load // function onLoad() { document.addEventListener("deviceready", onDeviceReady, false); } function onDeviceReady() { open_maps(); //open_mail(); } function open_maps() { var address = 'hogehoge'; window.plugins.webintent.startActivity({ action: WebIntent.ACTION_VIEW, url: 'geo:0,0?q=' + address}, function() {}, function() {alert('Failed to open URL via Android Intent')} ); } function open_mail() { var extras = {}; extras[WebIntent.EXTRA_SUBJECT] = "サブジェクト"; extras[WebIntent.EXTRA_TEXT] = "テキスト"; window.plugins.webintent.startActivity({ action: WebIntent.ACTION_SEND, type: 'text/plain', extras: extras }, function() {}, function() {alert('Failed to send email via Android Intent');}); } </script> </head> <body onload="onLoad()"> <h2> plugin test </h2> </body> </html>
アプリの起動後に、google mapsやメーラを起動できると思います。
注意点として、devicereadyイベントの後でないと、プラグインが有効にならないという点があります。
また、メーラ起動時に添付ファイルを指定する方法がないか試してみましたが、無理っぽい気がします。
プラグインを改造するのが手っ取り早いかも・・・。