메뉴 건너뛰기

조회 수 8422 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

직접 만든 플러그인을 호출할 때는 cordova.exec 함수를 호출해서 사용한다. 

 

형태는 아래와 같다.

 

cordova.exec( 성공시콜백함수, 실패시콜백함수, 클래스명, 액션명, [전달인자] );

 

예를 들어서

 

cordova.exec( function(param) {}, function(error) {}, "MyPlugin", "MyAction", ["param1", "param2", "param3"]);

 

위와 같이 호출했다면 MyPlugin 클래스의 execute 함수가 호출이 되는데, execute의 파라미터로 액션명과 파라미터들이 같이 전달된다. 그리고, execute 함수에서 콜백함수를 호출해주면 자바스크립트의 성공콜백함수, 실패콜백함수가 호출되는 것이다.

 

>> 위 스토리를 실제 예로 정리를 해보면 다음과 같다.

 

1. CordovaPlugin 클래스로부터 상속을 받는 클래스를 만든다.

 

...

import org.apache.cordova.api.CordovaPlugin;

import org.apache.cordova.api.PluginResult;

import org.json.JSONArray;

import org.json.JSONException;

import org.json.JSONObject;

 

public class HelloPlugin extends CordovaPlugin {

 ...

}

 

2. CordovaPlugin 클래스의 execute 함수를 오버라이드한다.

 

public class HelloPlugin extends CordovaPlugin {

...

 

@Override

public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {

...

}

}

 

3. execute 함수를 다음과 같이 정의한다.

 

if ( action.equals("hello") ) {

String yourname = args.getString(0);

 

if ( yourname == null || yourname.length() == 0 ) {

callbackContext.error("input your name");

return true;

}

 

callbackContext.success("Hello " + yourname );

return true;

}

return false;

 

4. 해석을 해보자면 이름을 넘겨받으면 "Hello 이름" 으로 반환해주는 역할을 한다.

이름이 입력되지 않았다면 error 콜백함수를 호출해주며 정상적이라면 success 콜백함수를 호출해준다.

그리고, hello 액션이 아니라면 false를 반환하고 false가 반환되면 그냥 예외로 떨어진다.

 

5. index.html를 다음과 같이 수정한다.

 

<!DOCTYPE html>
 <html>
      <head>
           <title>Hello</title>
           <meta name="viewport" content="width=device-width, initial-scale=1"/>
          
           <link rel="stylesheet" href="jquery.mobile-1.3.1.min.css"/>
           <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
           <script src="jquery-1.10.2.min.js"></script>
           <script src="jquery.mobile-1.3.1.min.js"></script>
           <script type="text/javascript">
             $(function() {
              $("#btn_add").click(function() {
              
               cordova.exec(function(result) {
                alert(result);
               }, function(error) {
                alert(error);
               }, "HelloPlugin", "hello", [$("#yourname").val()]);
               
              });
             });
           </script>
      </head>

      <body>
   <section id="mainpage" data-role="page">
    <header data-role="header">Hello</header>
    <div class="content" data-role="content">
     <label for="yourname">yourname</label><input type="text" id="yourname"/>
     <input type="button" value="Hello" id="btn_add" />
    </div>
    <footer data-role="footer">Hanttasoft</footer>
   </section>
      </body>

 </html>

 

6. 이제 HelloPlugin 이란 플러그인을 config.xml 파일에다가 등록해준다.

 

/res/xml/config.xml 파일을 열어서 다음줄을 추가해준다.

 

<plugin name="HelloPlugin" value="kr.inclass.mysample.HelloPlugin" />

 

===> 물론, 클래스의 전체경로는 자신의 패키지명에 맞게 수정한다.

 

7. 위 까지 작업한후 Run을 시켜보면...

이름 입력란에 이름을 입력하고 "Hello" 버튼을 클릭하면 입력된 이름으로 인사를 하는 alert 창이 뜨게 된다.

이름을 입력하지 않고 버튼을 클릭하면 이름 입력하라는 경고창이 뜨게 된다.


  1. 화면 회전에 따른 애니메이션 효과 구현하기

    Date2015.07.16 Views8055
    Read More
  2. 화면 해상도에 관계없는 레이아웃(Layout) 만들기

    Date2015.07.16 Views8641
    Read More
  3. 화면 전환해도 데이터 유지 예제

    Date2015.07.26 Views9204
    Read More
  4. 하이브리드앱 기본 - WebView로 웹페이지 띄우기

    Date2020.12.14 Views1025
    Read More
  5. 하이브리드 앱에서의 세션관리(로그인 상태 유지)

    Date2018.12.27 Views5000
    Read More
  6. 푸시 서비스(GCM)에 대해 알아보자

    Date2015.07.01 Views7000
    Read More
  7. 푸쉬 알림 기능. GCM (Google Cloud Messaging) 사용하기 (3)

    Date2015.07.16 Views6267
    Read More
  8. 푸쉬 알림 기능. GCM (Google Cloud Messaging) 사용하기 (2)

    Date2015.07.16 Views7292
    Read More
  9. 푸쉬 알림 기능. GCM (Google Cloud Messaging) 사용하기 (1)

    Date2015.07.16 Views6726
    Read More
  10. 폰갭(PhoneGap) 플러그인 사용하기

    Date2015.06.29 Views7357
    Read More
  11. 폰갭(PhoneGap) 플러그인 만들기

    Date2015.06.29 Views8422
    Read More
  12. 폰갭(PhoneGap) 에서 페이지들간의 이동

    Date2015.06.29 Views8459
    Read More
  13. 폰갭(PhoneGap) & jQuery Mobile 로 안드로이드 어플 개발

    Date2015.06.29 Views7839
    Read More
  14. 폰갭 비콘 디텍팅 안될 때 (기본적인건 다 되있어야됨)

    Date2015.07.26 Views6529
    Read More
  15. 패키지명을 한꺼번에 변경하기 (Refactor)

    Date2020.12.14 Views295
    Read More
  16. 특정 폴더에서 오래된 파일 삭제하기

    Date2015.07.16 Views6767
    Read More
  17. 트리뷰(TreeView) 컨트롤

    Date2014.10.16 Views6722
    Read More
  18. 탭 뷰에 탭 추가하기, 아이콘 넣기

    Date2015.07.16 Views9360
    Read More
  19. 클래스나눠서 xml 파싱과 FTP를이용하여 안드로이드에서 활용하기

    Date2014.08.28 Views6180
    Read More
  20. 카카오톡 분석하기 (2) - 카카오톡 암호화 함수 찾기

    Date2016.05.26 Views9599
    Read More
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 13 Next
/ 13

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved