Android Phonegap 插件在虚拟和真实设备上的不同结果(Looper.prepare() ERROR)

标签 android plugins cordova

我在 phonegap(html5、JQuery、JS)中开发了一个移动应用程序,我想开发一个插件来打印到 BT 打印机。

我下载了打印机制造商的 SDK,并将相应的 .jar 文件导入到我的项目中。

我创建了以下插件

js

var HelloPlugin = {

    callNativeFunction: function (success, fail, resultType) {
        return cordova.exec(success, fail, "com.tricedesigns.HelloPlugin", "nativeAction", [resultType]);
    }
};

Java

package com.tricedesigns;

import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
import org.json.JSONArray;
import com.starmicronics.stario.StarIOPort;
import com.starmicronics.stario.StarIOPortException;
import com.starmicronics.stario.StarPrinterStatus;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.util.Log;

public class HelloPlugin extends Plugin {

    public static final String NATIVE_ACTION_STRING="nativeAction";
    public static final String SUCCESS_PARAMETER="success";

    @Override
    public PluginResult execute(String action, JSONArray data, String callbackId) {

         Log.d("HelloPlugin", "Hello, this is a native function called from PhoneGap/Cordova!");

         //only perform the action if it is the one that should be invoked
         if (NATIVE_ACTION_STRING.equals(action)) {
             String resultType = null;
             StarIOPort port = null;
             byte[] texttoprint = new byte[]{0x1b,0x74,0x0D,(byte) 0x91,(byte) 0x92,(byte) 0x93,(byte) 0x94,(byte) 0x95,(byte) 0x96,(byte) 0x97,(byte) 0x98,(byte) 0x99,0x0A,0x0A,0x0A,0x0A,0x0A};
             try 
             {
                port = StarIOPort.getPort("BT:", "mini", 10000, null);
                    try
                    {
                        Thread.sleep(500);
                    }
                    catch(InterruptedException e) {}

                port.writePort(texttoprint, 0, texttoprint.length);
                    try
                    {
                        Thread.sleep(3000);
                    }
                    catch(InterruptedException e) {}

                resultType = "success";
             }
             catch (StarIOPortException e)
             {
                 resultType = "error";
             }


             if (resultType.equals(SUCCESS_PARAMETER)) {
                 return new PluginResult(PluginResult.Status.OK, "Yay, Success!!!");
             }
             else {
                 return new PluginResult(PluginResult.Status.ERROR, "Oops, Error :(");
             }

         }

         return null;
    }

}

html

<!DOCTYPE html>
<html>
  <head>
  <title></title>

    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" />
    <meta charset="utf-8">

    <!-- If your application is targeting iOS BEFORE 4.0 you MUST put json2.js from http://www.JSON.org/json2.js into your www directory and include it here -->
    <script type="text/javascript" charset="utf-8" src="cordova-1.6.1.js"></script>

    <script type="text/javascript" charset="utf-8" src="HelloPlugin.js"></script>
    <script type="text/javascript">


    function onBodyLoad()
    {       
        document.addEventListener("deviceready", onDeviceReady, false);
    }

    /* When this function is called, Cordova has been initialized and is ready to roll */
    /* If you are supporting your own protocol, the var invokeString will contain any arguments to the app launch.
    see http://iosdevelopertips.com/cocoa/launching-your-own-application-via-a-custom-url-scheme.html
    for more details -jm */
    function onDeviceReady()
    {
        // do your thing!
        navigator.notification.alert("Cordova is working")
    }

    function callNativePlugin( returnSuccess ) {
        HelloPlugin.callNativeFunction( nativePluginResultHandler, nativePluginErrorHandler, returnSuccess );
    }

    function nativePluginResultHandler (result) {
       alert("SUCCESS: \r\n"+result );
    }

    function nativePluginErrorHandler (error) {
       alert("ERROR: \r\n"+error );
    }



    </script>
    </head>
    <body onload="onBodyLoad()">
        <h1>Hey, it's Cordova!</h1>

        <button onclick="callNativePlugin('success');">Click to invoke the Native Plugin with an SUCCESS!</button>
        <button onclick="callNativePlugin('error');">Click to invoke the Native Plugin with an ERROR!</button>
    </body>
</html>

在虚拟设备上运行时总是出现错误,因为没有连接到打印机,但在真实设备上运行时出现以下错误:

ERROR: Can't create handler inside thread that has not called Looper.prepare()

最佳答案

我只需要在下面的代码中包含我的执行

        this.ctx.runOnUiThread(new Runnable()
        {
            public void run()
            {
            // my code
            }
        });

关于Android Phonegap 插件在虚拟和真实设备上的不同结果(Looper.prepare() ERROR),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12010115/

相关文章:

ruby-on-rails - Typhoeus::Request.new(...) 不起作用,但 Typhoeus::Request.get(...) 可以!

javascript - 我想知道 Google 的网站翻译器可以在我们的网站上免费使用吗?在我们的网站上使用它还有其他问题吗?

android - 违反用户数据政策和开发者分发协议(protocol)第 4.8 节

android - ShapeableImageView 的笔画被边框切割

android - 是否可以通过套接字连接在Cordova和Native Android之间进行通信?

javascript - 如何让 Acensor jQuery 插件为每个部分使用自定义 ID?

javascript - 使用javascript在phonegap中动态页面

javascript - Android native 通过 cordova 进行 JavaScript 通信

安卓 : Phonegap SQLite issue

java - Android向FragmentPagerAdapter添加和删除 fragment