PhoneGap Android复制到剪贴板插件实现

时间:2013-08-02 18:00:17

标签: java javascript android cordova phonegap-plugins

我正在尝试在我的应用程序中使用this plugin。我从来没有写过插件,这是我第一次尝试在我的Phonegap程序中使用一个插件。我不得不尝试改变java文件中的一大堆东西来摆脱错误。当我单击屏幕上的按钮时,出现错误“Uncaught ReferenceError:cordova未定义在file:///android_asset/www/js/clipboardmanager.js:2”。不知道这意味着什么或如何改变它。我会尝试粘贴所有内容,以便更容易找到我的错误。

结构:

src
-->com.example.basket
  -->MainActivity.java
-->com.saaticiouglu.phonegap.ClipboardManagerPlugin
  -->ClipboardManagerPlugin.java

ClipboardManagerPlugin.java:

package com.saatcioglu.phonegap.ClipboardManagerPlugin;

import org.json.JSONArray;
import org.json.JSONException;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.ClipboardManager;


import org.apache.cordova.api.CordovaPlugin;
import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.PluginResult;


@SuppressWarnings("deprecation")
public class ClipboardManagerPlugin extends CordovaPlugin {
private static final String actionCopy = "copy";
private static final String actionPaste = "paste";
private static final String errorParse = "Couldn't get the text to copy";
private static final String errorUnknown = "Unknown Error";

private ClipboardManager mClipboardManager;


@SuppressLint("NewApi") @Override
  public boolean execute(String action, JSONArray args, CallbackContext callbackContext)
  {

    // If we do not have the clipboard
    if(mClipboardManager == null) {
        mClipboardManager = (ClipboardManager) cordova.getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
    }


    // Copy
    if (action.equals(actionCopy)) {
        String arg = "";
        try {
            arg = (String) args.get(0);
            mClipboardManager.setText(arg);
        } catch (JSONException e) {
              callbackContext.error( errorParse);
        } catch (Exception e) {
              callbackContext.error( errorUnknown);
        }
        callbackContext.success();
    // Paste
    } else if (action.equals(actionPaste)) {
        String arg = (String) mClipboardManager.getText();
        if (arg == null) {
            arg = "";
        }
        PluginResult copy_ret = new PluginResult(PluginResult.Status.OK, arg);
        callbackContext.sendPluginResult(copy_ret);
        callbackContext.success();

    } else {
          callbackContext.error("invalid action");
          return false;
    }

    return true;
 }
}

我不得不使用Surpress警告,因为setText和getText给了我错误......它们仍然在我的文件中被删除。但现在没有错误...

clipboardmanager.js:

window.clipboardManagerCopy = function(str, success, fail) {
cordova.exec(success, fail, "ClipboardManagerPlugin", "copy", [str]);
};

window.clipboardManagerPaste = function(success, fail) {
cordova.exec(success, fail, "ClipboardManagerPlugin", "copy", []);
};

我在index.html中引用了JS文件:

$('button').on('click', function(){
            window.clipboardManagerCopy(
                "the text to copy",
                function(r){alert("copy is successful")},
                function(e){alert(e)}
            );
        });

我只想获得一个简单的警报,看它是否有效。感谢您抽出宝贵时间提供帮助。请帮我解决这个问题!!!

0 个答案:

没有答案