本文介绍了Phonegap webintent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须发送电子邮件使用webintent在phonegap为android.I得到错误在下面的代码。我不明白如何调用该函数在脚本中的index.html文件。我得到json的异常。any any帮助帮助我解决这个问题

I have to send email using webintent in phonegap for android.I am getting error in the following code.I dont understand how to call the function in script in the index.html file.I am getting json exception.Can anybdy help me to solve this??

WebIntent.java

WebIntent.java

  public class WebIntent extends Plugin {

        private String onNewIntentCallback = null;
        private static final String TAG = "Webintent";


        public PluginResult execute(String action, JSONArray args, String callbackId) {
            Log.d(TAG, "WebintentPlugin Called");
            try {

                  System.out.println("JSON11"+args);
                if (action.equals("startActivity")) {
                    if (args.length() != 1) {
                        return new PluginResult(PluginResult.Status.INVALID_ACTION);
                    }


                    // Parse the arguments

                    JSONObject obj = args.getJSONObject(0);

                    String type = obj.has("type") ? obj.getString("type") : null;
                    Uri uri = obj.has("url") ? Uri.parse(obj.getString("url")) : null;
                    JSONObject extras = obj.has("extras") ? obj.getJSONObject("extras") : null;
                    Map<String, String> extrasMap = new HashMap<String, String>();

                    // Populate the extras if any exist
                    if (extras != null) {
                        JSONArray extraNames = extras.names();
                        for (int i = 0; i < extraNames.length(); i++) {
                            String key = extraNames.getString(i);
                            String value = extras.getString(key);
                            extrasMap.put(key, value);
                        }
                    }

                    startActivity(obj.getString("action"), uri, type, extrasMap);
                    return new PluginResult(PluginResult.Status.OK);

                } else if (action.equals("hasExtra")) {
                    if (args.length() != 1) {
                        return new PluginResult(PluginResult.Status.INVALID_ACTION);
                    }
                    Intent i = ((DroidGap) this.ctx).getIntent();
                    String extraName = args.getString(0);
                    return new PluginResult(PluginResult.Status.OK, i.hasExtra(extraName));

                } else if (action.equals("getExtra")) {
                    if (args.length() != 1) {
                        return new PluginResult(PluginResult.Status.INVALID_ACTION);
                    }
                    Intent i = ((DroidGap) this.ctx).getIntent();
                    String extraName = args.getString(0);
                    if (i.hasExtra(extraName)) {
                        return new PluginResult(PluginResult.Status.OK, i.getStringExtra(extraName));
                    } else {
                        return new PluginResult(PluginResult.Status.ERROR);
                    }
                } else if (action.equals("getUri")) {
                    if (args.length() != 0) {
                        return new PluginResult(PluginResult.Status.INVALID_ACTION);
                    }

                    Intent i = ((DroidGap) this.ctx).getIntent();
                    String uri = i.getDataString();
                    return new PluginResult(PluginResult.Status.OK, uri);
                } else if (action.equals("onNewIntent")) {
                    if (args.length() != 0) {
                        return new PluginResult(PluginResult.Status.INVALID_ACTION);
                    }

                    this.onNewIntentCallback = callbackId;
                    PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT);
                    result.setKeepCallback(true);
                    return result;
                } else if (action.equals("sendBroadcast")) 
                {
                    if (args.length() != 1) {
                        return new PluginResult(PluginResult.Status.INVALID_ACTION);
                    }

                    // Parse the arguments
                    JSONObject obj = args.getJSONObject(0);
                    System.out.println("JSON"+obj);
                    JSONObject extras = obj.has("extras") ? obj.getJSONObject("extras") : null;
                    Map<String, String> extrasMap = new HashMap<String, String>();

                    // Populate the extras if any exist
                    if (extras != null) {
                        JSONArray extraNames = extras.names();
                        for (int i = 0; i < extraNames.length(); i++) {
                            String key = extraNames.getString(i);
                            String value = extras.getString(key);
                            extrasMap.put(key, value);
                        }
                    }

                    sendBroadcast(obj.getString("action"), extrasMap);
                    return new PluginResult(PluginResult.Status.OK);
                }
                return new PluginResult(PluginResult.Status.INVALID_ACTION);
            } catch (JSONException e) {
                e.printStackTrace();
                return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
            }
        }

        @Override
        public void onNewIntent(Intent intent) {
            if (this.onNewIntentCallback != null) {
                PluginResult result = new PluginResult(PluginResult.Status.OK, intent.getDataString());
                result.setKeepCallback(true);
                this.success(result, this.onNewIntentCallback);
            }
        }

        void startActivity(String action, Uri uri, String type, Map<String, String> extras) {
            Intent i = (uri != null ? new Intent(action, uri) : new Intent(action));

            if (type != null && uri != null) {
                i.setDataAndType(uri, type); //Fix the crash problem with android 2.3.6
            } else {
                if (type != null) {
                    i.setType(type);
                }
            }

            for (String key : extras.keySet()) {
                String value = extras.get(key);
                // If type is text html, the extra text must sent as HTML
                if (key.equals(Intent.EXTRA_TEXT) && type.equals("text/html")) {
                    i.putExtra(key, Html.fromHtml(value));
                } else if (key.equals(Intent.EXTRA_STREAM)) {
                    // allowes sharing of images as attachments.
                    // value in this case should be a URI of a file
                    i.putExtra(key, Uri.parse(value));
                } else if (key.equals(Intent.EXTRA_EMAIL)) {
                    // allows to add the email address of the receiver
                    i.putExtra(Intent.EXTRA_EMAIL, new String[] {"myemail.com"});
                } else {
                    i.putExtra(key, value);
                }
            }
            this.ctx.startActivity(i);
        }

        void sendBroadcast(String action, Map<String, String> extras) {
            Intent intent = new Intent();
            intent.setAction(action);
            for (String key : extras.keySet()) {
                String value = extras.get(key);
                intent.putExtra(key, value);
            }



            ((DroidGap) this.ctx).sendBroadcast(intent);
        }
    }
webintent.js

    var WebIntent = function() { 

    };

    WebIntent.ACTION_SEND = "android.intent.action.SEND";
    WebIntent.ACTION_VIEW= "android.intent.action.VIEW";
    WebIntent.EXTRA_TEXT = "android.intent.extra.TEXT";
    WebIntent.EXTRA_SUBJECT = "android.intent.extra.SUBJECT";
    WebIntent.EXTRA_STREAM = "android.intent.extra.STREAM";
    WebIntent.EXTRA_EMAIL = "android.intent.extra.EMAIL";

    WebIntent.prototype.startActivity = function(params, success, fail) {
        return cordova.exec(function(args) {
            success(args);
        }, function(args) {
            fail(args);
        }, 'WebIntent', 'startActivity', [params]);
    };

    WebIntent.prototype.hasExtra = function(params, success, fail) {
        return cordova.exec(function(args) {
            success(args);
        }, function(args) {
            fail(args);
        }, 'WebIntent', 'hasExtra', [params]);
    };

    WebIntent.prototype.getUri = function(success, fail) {
        return cordova.exec(function(args) {
            success(args);
        }, function(args) {
            fail(args);
        }, 'WebIntent', 'getUri', []);
    };

    WebIntent.prototype.getExtra = function(params, success, fail) {
        return cordova.exec(function(args) {
            success(args);
        }, function(args) {
            fail(args);
        }, 'WebIntent', 'getExtra', [params]);
    };


    WebIntent.prototype.onNewIntent = function(callback) {
        return cordova.exec(function(args) {
            callback(args);
        }, function(args) {
        }, 'WebIntent', 'onNewIntent', []);
    };

    WebIntent.prototype.sendBroadcast = function(params, success, fail) {
        return cordova.exec(function(args) {
            success(args);
        }, function(args) {
            fail(args);
        }, 'WebIntent', 'sendBroadcast', [params]);
    };



    cordova.addConstructor(function() {
        cordova.addPlugin('WebIntent', new WebIntent());
    });

index.html

<html>
  <head>
    <meta name="viewport" content="target-densitydpi=low-dpi; user-scalable=no" />
    <title>PhoneGap Events Example</title>
    <script type="text/javascript" charset="utf-8" src="cordova-1.8.0.js"></script> 
     <script type="text/javascript" charset="utf-8" src="webintent.js"></script>

      <script type="text/javascript" charset="utf-8">


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

   function onDeviceReady() {    
       window.plugins.WebIntent.startActivity('WebIntent', success, fail);

        }
        function success(e) {


        console.log("Success");
        }

        function fail(f) {
             console.log("Failure");
        }





    </script>
  </head>
  <body onload="onBodyLoad();">
    <h2>IMEI v1.5</h2>
    IMEI: <span id="imei"></span><br/>

  </body>
</html>


My error is-

07-10 10:23:12.180: W/System.err(536): org.json.JSONException: Value WebIntent at 0 of type java.lang.String cannot be converted to JSONObject
07-10 10:23:12.200: W/System.err(536):  at org.json.JSON.typeMismatch(JSON.java:100)
07-10 10:23:12.200: W/System.err(536):  at org.json.JSONArray.getJSONObject(JSONArray.java:484)
07-10 10:23:12.230: W/System.err(536):  at com.gsr.imei.WebIntent.execute(WebIntent.java:48)
07-10 10:23:12.240: W/System.err(536):  at org.apache.cordova.api.PluginManager$1.run(PluginManager.java:186)
07-10 10:23:12.240: W/System.err(536):  at java.lang.Thread.run(Thread.java:856)


can anyone help me??Thanks for any help..


推荐答案

window.plugins.WebIntent.startActivity('WebIntent', success, fail);

应该是这样的地图对象:

Should be a map object like this:

window.plugins.WebIntent.startActivity({url: 'url here', type: 'type', extras: {}}, success, fail);



编辑



使用此功能呼叫意图

Edit

Using this function to call the intent

function sendEmail(to, subject, body) { 
          var extras = {};
          extras[WebIntent.EXTRA_SUBJECT] = subject;
          extras[WebIntent.EXTRA_TEXT] = body;
          window.plugins.webintent.startActivity({
              url: to,
              action: WebIntent.ACTION_SEND,
              type: 'text/plain', 
              extras: extras 
            }, 
            function() {
                alert("mail sent");
            }, 
            function() {
              alert('Failed to send email via Android Intent');
            }
          ); 
    }

它会打开包含所有已发送参数的邮件发送对话框。

It opens up the mail send dialog with all the sent parameter.

这篇关于Phonegap webintent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 16:42