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

时间:2012-08-17 17:08:35

标签: 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>

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

  

错误:无法在未调用Looper.prepare()的线程内创建处理程序

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

我遇到了一些在某些设备上出现的问题。成功地,一个聪明的男孩,托比,帮助了我。所以,解决方案是下一个:
- 在调用任何StarIOPort的方法之前,你必须检查looper是否存在:

if (Looper.myLooper() == null) {
    Looper.prepare();
}

在你的情况下它会是这样的:

 try 
 {
    if (Looper.myLooper() == null) {
        Looper.prepare();
    }
    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";
 }

还有一个建议:
代替

port = StarIOPort.getPort("BT:", "mini", 10000, null);

仅使用

port = StarIOPort.getPort("BT:", "mini", 10000);

在插件中你不会使用Context

祝你好运。