質問編集履歴

2

コード内容を変更しました

2016/10/25 14:46

投稿

hide09090909
hide09090909

スコア68

test CHANGED
File without changes
test CHANGED
@@ -114,161 +114,65 @@
114
114
 
115
115
 
116
116
 
117
- プラグイン呼び出しのJS
118
-
119
- ```ここに言語を入力
120
-
121
- var app = {
122
-
123
- // コンストラクタ
124
-
125
- //initializeは初期化
126
-
127
- initialize: function() {
128
-
129
- //alert("tesuto2");
130
-
131
- this.bindEvents();
132
-
133
- // alert("tesuto3");
134
-
135
- },
136
-
137
-
138
-
139
- // 'deviceready'はCordovaを全て読み込んから
140
-
141
- //bindEventsは起動時に必要となる全てのイベントをバインド(関連付け)する
142
-
143
- //addEventListenerは第一引数にイベント名、第二引数にイベントリスナ(呼び出す関数)、第三引数に真偽値を入れつ。
144
-
145
- //第一引数にonはつけない
146
-
147
- bindEvents: function() {
148
-
149
- document.addEventListener('deviceready', this.onDeviceReady, false);
150
-
151
- //alert("tesuto4");
152
-
153
- },
154
-
155
- // deviceready Event Handler
156
-
157
- //
158
-
159
-
160
-
161
- onDeviceReady: function() {
162
-
163
-
164
-
165
- setTimeout(function (){cordova.exec(function() {
166
-
167
- console.log('プラグイン呼び出し成功しました');
168
-
169
- }, function() {
170
-
171
- console.log('プラグイン呼び出しに失敗しました');
172
-
173
- //'プラグイン名'、'実行したいメソッド名'[引数の配列]
174
-
175
- }, 'Hello', 'makeText', []);},2000);
176
-
177
- },
178
-
179
-
180
-
181
-
182
-
183
- //alert("tesuto5");
184
-
185
- //app.receivedEvent('deviceready');
186
-
187
- //alert("tesuto6");
188
-
189
-
190
-
191
- /* //カメラへのオプション
192
-
193
- var options = {
194
-
195
- quality: 50,
196
-
197
- destinationType: navigator.camera.DestinationType.FILE_URI
198
-
199
- };
200
-
201
- //第一引数に成功時の処理、第二引数に失敗時の処理、第三引数にオプション
202
-
203
- //functionを使用する意味
204
-
205
- setTimeout(function (){navigator.camera.getPicture(function(imageData) {
206
-
207
- alert(imageData);
208
-
209
- }, function() {
210
-
211
- alert('fail');
212
-
213
- }, options);},2000);
214
-
215
- //カメラを起動する。
216
-
217
- // navigator.camera.getPicture(function(imageData) {
218
-
219
- // alert(imageData);
220
-
221
- // }, function() {
222
-
223
- // alert('fail');
224
-
225
- // }, options);
226
-
227
- //setTimeoutは第一引数に呼び出したい関数、第二引数に待機時間を設定する。2000=2秒
228
-
229
- //setTimeoutのTは大文字でなければ実行されない。
230
-
231
- //setTimeout(navigator.camera.getPicture(), 2000);
232
-
233
- */
234
-
235
-
236
-
237
-
238
-
239
-
240
-
241
- // Update DOM on a Received Event
242
-
243
- receivedEvent: function(id) {
244
-
245
- //document.getElementById(id);は指定の引数を持つ要素を返す。
246
-
247
- var parentElement = document.getElementById(id);
248
-
249
- //セレクタにマッチする文章中の最初の要素を返す。
250
-
251
- //セレクタとはスタイルを適用する対象のこと、CSS
252
-
253
- var listeningElement = parentElement.querySelector('.listening');
254
-
255
- var receivedElement = parentElement.querySelector('.received');
256
-
257
- //指定の要素に新しい属性を追加もしくは指定の要素に存在する値を変更する。
258
-
259
- //第一引数に属性名、第二引数に変更したい値を入れる。
260
-
261
- listeningElement.setAttribute('style', 'display:none;');
262
-
263
- receivedElement.setAttribute('style', 'display:block;'); //console.logはコンソールにメッセージを出力させる関数。
264
-
265
- console.log('Received Event: ' + id);
117
+ index.js
118
+
119
+ ```ここに言語を入力
120
+
121
+ document.addEventListener("deviceready", function() {
122
+
123
+ var success = function(message) {
124
+
125
+ alert(message);
126
+
127
+ };
128
+
129
+
130
+
131
+ var failure = function() {
132
+
133
+ alert("Plugin error...");
134
+
135
+ };
136
+
137
+
138
+
139
+ // [js-moduleのname].[helloworld.js指定した名前]
140
+
141
+ hello.sayHello("SAMPLE NAME", success, failure);
142
+
143
+ });
144
+
145
+
146
+
147
+ ```
148
+
149
+
150
+
151
+ ↓helloworld.js
152
+
153
+ ```ここに言語を入力
154
+
155
+ module.exports = {
156
+
157
+ sayHello: function(name, successCallback, errorCallback) {
158
+
159
+ // 第1引数: 成功時に呼び出す関数
160
+
161
+ // 第2引数: エラー時に呼び出す関数
162
+
163
+ // 第3引数: プラグインの名前(plugin.xmlのfeatureのnameに設定したもの)
164
+
165
+ // 第4引数: HelloWorld.javaの第1引数に渡る名前
166
+
167
+ // 第5引数: HelloWorld.javaの第2引数渡る値
168
+
169
+ cordova.exec(successCallback, errorCallback, "helloworld", "sayHello", [name]);
266
170
 
267
171
  }
268
172
 
269
173
  };
270
174
 
271
- app.initialize();
175
+
272
176
 
273
177
  ```
274
178
 
@@ -278,60 +182,52 @@
278
182
 
279
183
  ```ここに言語を入力
280
184
 
185
+ // featureのparamのvalueで指定したやつ
186
+
281
- package plugin.hello;
187
+ package plugin.helloworld;
282
-
283
-
284
-
285
- package com.mse.plugin;
188
+
286
-
287
-
288
-
289
- import android.os.Bundle;
189
+
290
190
 
291
191
  import org.apache.cordova.*;
292
192
 
293
- import org.apache.cordova.CallbackContext;
294
-
295
- import org.apache.cordova.CordovaPlugin;
296
-
297
193
  import org.json.JSONArray;
298
194
 
299
195
  import org.json.JSONException;
300
196
 
301
- import android.app.AlertDialog;
197
+
302
-
303
-
304
-
198
+
305
- public class Hello extends CordovaPlugin{
199
+ public class HelloWorld extends CordovaPlugin {
306
-
307
- {
200
+
201
+
308
202
 
309
203
  @Override
310
204
 
311
- //callbackContext引数はJavaScript側に値を返したりプラグインの結果やエラーを通知したりするためのメソッドを持つオブジェクト
312
-
313
- //executeはjavascriptから呼ばれるメソッド
314
-
315
- public boolean execute(String action, JSONArray args,
316
-
317
- CallbackContext callbackContext) throws JSONException{
205
+ public boolean execute(String action, JSONArray data, CallbackContext callbackContext) throws JSONException {
206
+
207
+
208
+
318
-
209
+ if (action.equals("sayHello")) {
210
+
319
-
211
+ String name = data.getString(0);
212
+
320
-
213
+ String message = "Hello, World !!! " + "Hello, " + name;
214
+
215
+ callbackContext.success(message);
216
+
321
- return true;
217
+ return true;
322
-
323
-
324
-
218
+
325
- //表示時間を短くしたい場合はLONG部分をSHORTに変更
219
+ } else {
326
-
220
+
327
- Toast.makeText(this."Hello", Toast.LENGTH_LONG).show();
221
+ return false;
328
-
222
+
329
- }
223
+ }
330
224
 
331
225
  }
332
226
 
333
227
  }
334
228
 
229
+
230
+
335
231
  ```
336
232
 
337
233
 
@@ -342,88 +238,84 @@
342
238
 
343
239
  <?xml version="1.0" encoding="UTF-8"?>
344
240
 
345
-
346
-
347
- <!--
348
-
349
- Licensed to the Apache Software Foundation (ASF) under one
350
-
351
- or more contributor license agreements. See the NOTICE file
352
-
353
- distributed with this work for additional information
354
-
355
- regarding copyright ownership. The ASF licenses this file
356
-
357
- to you under the Apache License, Version 2.0 (the
358
-
359
- "License"); you may not use this file except in compliance
360
-
361
- with the License. You may obtain a copy of the License at
362
-
363
-
364
-
365
- http://www.apache.org/licenses/LICENSE-2.0
366
-
367
-
368
-
369
- Unless required by applicable law or agreed to in writing,
370
-
371
- software distributed under the License is distributed on an
372
-
373
- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
374
-
375
- KIND, either express or implied. See the License for the
376
-
377
- specific language governing permissions and limitations
378
-
379
- under the License.
380
-
381
- -->
382
-
383
-
384
-
385
- <plugin xmlns="http://cordova.apache.org/ns/plugins/1.0" id="cordova-plugin-Hello" version="1.0.0">
386
-
387
- <!-->プラグイン名<-->
388
-
389
- <name>cordova-plugin-Hello</name>
390
-
391
- <!-->プラグインの説明<-->
392
-
393
- <description>Toast plugin</description>
394
-
395
- <!-->キーワード<-->
396
-
397
- <keywords>Hello</keywords>
398
-
399
- <license></license>
400
-
401
-
402
-
403
- <!-- android用設定 -->
404
-
405
- <platform name="android">
406
-
407
- <!-->config.xmlに以下の情報が追加される<-->
408
-
409
- <config-file target="res/xml/config.xml" parent="/*">
410
-
411
- <feature name="Hello">
412
-
413
- <param name="android-package" value="plugin.hello.Hello"/>
414
-
415
- </feature>
416
-
417
- </config-file>
418
-
419
- <!-->javaソースファイル<-->
420
-
421
- <source-file src="src/android/Hello.java" target-dir="src/plugin/Hello"/>
422
-
423
- </platform>
424
-
425
-
426
-
427
- </plugin>
428
-
429
- ```
241
+ <!-- id: プラグインの識別子、version: プラグインのバージョン -->
242
+
243
+ <plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
244
+
245
+ xmlns:android="http://schemas.android.com/apk/res/android"
246
+
247
+ id="cordova-plugin-helloworld"
248
+
249
+ version="1.0.0">
250
+
251
+
252
+
253
+ <!-- プラグイン名 -->
254
+
255
+ <name>HelloWorld</name>
256
+
257
+ <!-- プラグインの説明 -->
258
+
259
+ <description>This is a first plugin !!</description>
260
+
261
+ <!-- 作者 -->
262
+
263
+ <author>cigalecigales</author>
264
+
265
+ <!-- キーワード -->
266
+
267
+ <keywords>hello, world</keywords>
268
+
269
+ <!-- ライセンス -->
270
+
271
+ <license>Sample License</license>
272
+
273
+ <!-- プラグインがサポートするCordovaのバージョン -->
274
+
275
+ <engines>
276
+
277
+ <engine name="cordova-android" version=">5.0.0"/>
278
+
279
+ </engines>
280
+
281
+ <!-- JSの場所指定。name: モジュール名 -->
282
+
283
+ <js-module src="www/helloworld.js" name="hello">
284
+
285
+ <clobbers target="hello"/>
286
+
287
+ </js-module>
288
+
289
+
290
+
291
+ <!-- Android用の設定 -->
292
+
293
+ <platform name="android">
294
+
295
+ <!-- Androidのconfig.xmlはここ→project/platform/android/res/xml/config.xmlにあるのでそこに反映するように -->
296
+
297
+ <config-file target="res/xml/config.xml" parent="/*">
298
+
299
+ <!-- Cordovaはfeatureタグをみて、どプラグインが有効か見る。以下の情報が上記のファイルに追加される。 -->
300
+
301
+ <feature name="helloworld">
302
+
303
+ <param name="android-package" value="plugin.helloworld.HelloWorld"/>
304
+
305
+ </feature>
306
+
307
+ </config-file>
308
+
309
+ <!-- Javaのソースファイル。 target-dir: ファイルがコンパイルされるべき場所 -->
310
+
311
+ <!-- 以下だとproject/platform/android/src/plugin/helloworld/以下になる -->
312
+
313
+ <source-file src="src/android/HelloWorld.java" target-dir="src/plugin/helloworld/"/>
314
+
315
+ </platform>
316
+
317
+ </plugin>
318
+
319
+
320
+
321
+ ```

1

コードの記載 

2016/10/25 14:46

投稿

hide09090909
hide09090909

スコア68

test CHANGED
File without changes
test CHANGED
@@ -9,3 +9,421 @@
9
9
  自分が気付いた問題点として、自作したプラグインの中にPlugin.xmlがないということです。
10
10
 
11
11
  ネットではエクリプスを使ったやり方ばかりで、どなたかテキストエディタで自作のプラグインの作成、使い方がわかる方教えてください。
12
+
13
+
14
+
15
+ よろしくお願いいたします
16
+
17
+ ↓アイコン表示
18
+
19
+ 編集------
20
+
21
+ アプリの流れはアイコンが表示され、タップするとロゴ画像が表示される、2秒経過すると自動で自作のToastが表示されるプラグインが起動するというものです。
22
+
23
+ ```ここに言語を入力
24
+
25
+ <!DOCTYPE html>
26
+
27
+ <html>
28
+
29
+
30
+
31
+ <head>
32
+
33
+ <meta charset="utf-8">
34
+
35
+ <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
36
+
37
+ <title> LIVE </title>
38
+
39
+
40
+
41
+ <script>
42
+
43
+ function imgClick() {
44
+
45
+ //指定したURLやファイルに遷移させる。
46
+
47
+ window.location.href="logo.html";
48
+
49
+
50
+
51
+ }
52
+
53
+ </script>
54
+
55
+
56
+
57
+ <!--クリックすると、imaClick関数の呼び出し-->
58
+
59
+ <a href="javascript:void(0)" onclick="imgClick();">
60
+
61
+ <img src="img/Icon.png" width="100" height="100" /></a>
62
+
63
+
64
+
65
+ <!--cordova.jsの読み込み-->
66
+
67
+ <!--divは囲んだ範囲を1つのかたまりとして、align属性で位置を指定したりできる。-->
68
+
69
+ </head>
70
+
71
+ </html>
72
+
73
+ コード
74
+
75
+ ```
76
+
77
+
78
+
79
+ ↓ロゴ画面表示
80
+
81
+ ```ここに言語を入力
82
+
83
+ <!DOCTYPE html>
84
+
85
+ <html>
86
+
87
+
88
+
89
+ <head>
90
+
91
+ <meta charset="utf-8">
92
+
93
+ <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
94
+
95
+ <title> LIVE </title>
96
+
97
+
98
+
99
+ <body>
100
+
101
+ <img src="img/Icon.png" width="350" height="600" ; />
102
+
103
+ <script type="text/javascript" src="cordova.js"></script>
104
+
105
+ <script type="text/javascript" src="js/index.js"></script>
106
+
107
+ </body>
108
+
109
+ </head>
110
+
111
+ </html>
112
+
113
+ ```
114
+
115
+
116
+
117
+ ↓プラグイン呼び出しのJS
118
+
119
+ ```ここに言語を入力
120
+
121
+ var app = {
122
+
123
+ // コンストラクタ
124
+
125
+ //initializeは初期化
126
+
127
+ initialize: function() {
128
+
129
+ //alert("tesuto2");
130
+
131
+ this.bindEvents();
132
+
133
+ // alert("tesuto3");
134
+
135
+ },
136
+
137
+
138
+
139
+ // 'deviceready'はCordovaを全て読み込んでから
140
+
141
+ //bindEventsは起動時に必要となる全てのイベントをバインド(関連付け)する
142
+
143
+ //addEventListenerは第一引数にイベント名、第二引数にイベントリスナ(呼び出す関数)、第三引数に真偽値を入れつ。
144
+
145
+ //第一引数にonはつけない
146
+
147
+ bindEvents: function() {
148
+
149
+ document.addEventListener('deviceready', this.onDeviceReady, false);
150
+
151
+ //alert("tesuto4");
152
+
153
+ },
154
+
155
+ // deviceready Event Handler
156
+
157
+ //
158
+
159
+
160
+
161
+ onDeviceReady: function() {
162
+
163
+
164
+
165
+ setTimeout(function (){cordova.exec(function() {
166
+
167
+ console.log('プラグイン呼び出しに成功しました');
168
+
169
+ }, function() {
170
+
171
+ console.log('プラグイン呼び出しに失敗しました');
172
+
173
+ //'プラグイン名'、'実行したいメソッド名'[引数の配列]
174
+
175
+ }, 'Hello', 'makeText', []);},2000);
176
+
177
+ },
178
+
179
+
180
+
181
+
182
+
183
+ //alert("tesuto5");
184
+
185
+ //app.receivedEvent('deviceready');
186
+
187
+ //alert("tesuto6");
188
+
189
+
190
+
191
+ /* //カメラへのオプション
192
+
193
+ var options = {
194
+
195
+ quality: 50,
196
+
197
+ destinationType: navigator.camera.DestinationType.FILE_URI
198
+
199
+ };
200
+
201
+ //第一引数に成功時の処理、第二引数に失敗時の処理、第三引数にオプション
202
+
203
+ //functionを使用する意味
204
+
205
+ setTimeout(function (){navigator.camera.getPicture(function(imageData) {
206
+
207
+ alert(imageData);
208
+
209
+ }, function() {
210
+
211
+ alert('fail');
212
+
213
+ }, options);},2000);
214
+
215
+ //カメラを起動する。
216
+
217
+ // navigator.camera.getPicture(function(imageData) {
218
+
219
+ // alert(imageData);
220
+
221
+ // }, function() {
222
+
223
+ // alert('fail');
224
+
225
+ // }, options);
226
+
227
+ //setTimeoutは第一引数に呼び出したい関数、第二引数に待機時間を設定する。2000=2秒
228
+
229
+ //setTimeoutのTは大文字でなければ実行されない。
230
+
231
+ //setTimeout(navigator.camera.getPicture(), 2000);
232
+
233
+ */
234
+
235
+
236
+
237
+
238
+
239
+
240
+
241
+ // Update DOM on a Received Event
242
+
243
+ receivedEvent: function(id) {
244
+
245
+ //document.getElementById(id);は指定の引数を持つ要素を返す。
246
+
247
+ var parentElement = document.getElementById(id);
248
+
249
+ //セレクタにマッチする文章中の最初の要素を返す。
250
+
251
+ //セレクタとはスタイルを適用する対象のこと、CSS
252
+
253
+ var listeningElement = parentElement.querySelector('.listening');
254
+
255
+ var receivedElement = parentElement.querySelector('.received');
256
+
257
+ //指定の要素に新しい属性を追加もしくは指定の要素に存在する値を変更する。
258
+
259
+ //第一引数に属性名、第二引数に変更したい値を入れる。
260
+
261
+ listeningElement.setAttribute('style', 'display:none;');
262
+
263
+ receivedElement.setAttribute('style', 'display:block;'); //console.logはコンソールにメッセージを出力させる関数。
264
+
265
+ console.log('Received Event: ' + id);
266
+
267
+ }
268
+
269
+ };
270
+
271
+ app.initialize();
272
+
273
+ ```
274
+
275
+
276
+
277
+ ↓プラグイン
278
+
279
+ ```ここに言語を入力
280
+
281
+ package plugin.hello;
282
+
283
+
284
+
285
+ package com.mse.plugin;
286
+
287
+
288
+
289
+ import android.os.Bundle;
290
+
291
+ import org.apache.cordova.*;
292
+
293
+ import org.apache.cordova.CallbackContext;
294
+
295
+ import org.apache.cordova.CordovaPlugin;
296
+
297
+ import org.json.JSONArray;
298
+
299
+ import org.json.JSONException;
300
+
301
+ import android.app.AlertDialog;
302
+
303
+
304
+
305
+ public class Hello extends CordovaPlugin{
306
+
307
+ {
308
+
309
+ @Override
310
+
311
+ //callbackContext引数はJavaScript側に値を返したりプラグインの結果やエラーを通知したりするためのメソッドを持つオブジェクト
312
+
313
+ //executeはjavascriptから呼ばれるメソッド
314
+
315
+ public boolean execute(String action, JSONArray args,
316
+
317
+ CallbackContext callbackContext) throws JSONException{
318
+
319
+
320
+
321
+ return true;
322
+
323
+
324
+
325
+ //表示時間を短くしたい場合はLONG部分をSHORTに変更
326
+
327
+ Toast.makeText(this."Hello", Toast.LENGTH_LONG).show();
328
+
329
+ }
330
+
331
+ }
332
+
333
+ }
334
+
335
+ ```
336
+
337
+
338
+
339
+ ↓plugin.xml
340
+
341
+ ```ここに言語を入力
342
+
343
+ <?xml version="1.0" encoding="UTF-8"?>
344
+
345
+
346
+
347
+ <!--
348
+
349
+ Licensed to the Apache Software Foundation (ASF) under one
350
+
351
+ or more contributor license agreements. See the NOTICE file
352
+
353
+ distributed with this work for additional information
354
+
355
+ regarding copyright ownership. The ASF licenses this file
356
+
357
+ to you under the Apache License, Version 2.0 (the
358
+
359
+ "License"); you may not use this file except in compliance
360
+
361
+ with the License. You may obtain a copy of the License at
362
+
363
+
364
+
365
+ http://www.apache.org/licenses/LICENSE-2.0
366
+
367
+
368
+
369
+ Unless required by applicable law or agreed to in writing,
370
+
371
+ software distributed under the License is distributed on an
372
+
373
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
374
+
375
+ KIND, either express or implied. See the License for the
376
+
377
+ specific language governing permissions and limitations
378
+
379
+ under the License.
380
+
381
+ -->
382
+
383
+
384
+
385
+ <plugin xmlns="http://cordova.apache.org/ns/plugins/1.0" id="cordova-plugin-Hello" version="1.0.0">
386
+
387
+ <!-->プラグイン名<-->
388
+
389
+ <name>cordova-plugin-Hello</name>
390
+
391
+ <!-->プラグインの説明<-->
392
+
393
+ <description>Toast plugin</description>
394
+
395
+ <!-->キーワード<-->
396
+
397
+ <keywords>Hello</keywords>
398
+
399
+ <license></license>
400
+
401
+
402
+
403
+ <!-- android用の設定 -->
404
+
405
+ <platform name="android">
406
+
407
+ <!-->config.xmlに以下の情報が追加される<-->
408
+
409
+ <config-file target="res/xml/config.xml" parent="/*">
410
+
411
+ <feature name="Hello">
412
+
413
+ <param name="android-package" value="plugin.hello.Hello"/>
414
+
415
+ </feature>
416
+
417
+ </config-file>
418
+
419
+ <!-->javaソースファイル<-->
420
+
421
+ <source-file src="src/android/Hello.java" target-dir="src/plugin/Hello"/>
422
+
423
+ </platform>
424
+
425
+
426
+
427
+ </plugin>
428
+
429
+ ```