簡體   English   中英

如何在Phonegap中安裝插件

[英]How to Install plug-in in Phonegap

我是電話空白的新手,並且致力於一個可以在Android和IOS上運行的應用程序。 現在,我的要求是我想發送帶有附件的電子郵件,因此我通過Internet搜索並找到了該插件EmailComposer-phonegap-plugin 他們在這里寫下4點,如下所示。

  1. 將EmailComposer.h EmailComposer.m文件添加到您的Plugins文件夾中。
  2. 將EmailComposer.js文件放置在www文件夾中的某個位置,並從HTML中包含它。
  3. 在插件下添加到config.xml:或者,如果使用新格式,

     <feature name="EmailComposer"> <param name="ios-package" value="EmailComposer" /> </feature> 
  4. 在android上,請確保在value屬性中還包括包名稱,例如

     value="org.apache.cordova.EmailComposer" 

現在我的查詢是如何使用此插件? 我完成了第二點,但是不知道我將文件作為第一點放置在哪里? 因為我不知道我的“插件”文件夾在哪里。 以及如何處理第3點和第4點?

我也在互聯網上找到此代碼。 但是“附加文件”警報不起作用。

var smallImage = document.getElementById('smallImage').value
alert("Send Mail");
window.plugins.emailComposer.showEmailComposer(
        "Send Images",
        smallImage,
        ["test@email.com",],
        [],
        [],
        true,
        ["image.jpeg", "file.zip"],
        alert("Attach file");
    );
    alert("Send Mail Success");

那么,成功發送附件郵件我該怎么做?

試試這個對我來說工作...

index.html

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8" />
            <meta name="format-detection" content="telephone=no" />
            <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />

            <title>Hello World</title>
        </head>
        <body>

            <script type="text/javascript" src="phonegap.js"></script>
            <script type="text/javascript" src="js/EmailComposer.js"></script>  

             <script type="text/javascript">            
             document.addEventListener("deviceready",onDeviceReady,false);
                function onDeviceReady() {


                            cordova.require('cordova/plugin/emailcomposer').showEmailComposer(
                                function() { console.log( 'successfully called email composer' ); },
                                function() { console.log( 'failed to call email composer' ); },
                                "Look at this photo",
                                "Take a look at this:",
                                 ["amitd.prajapati@test.com"],
                                 [],
                                 [],
                                false,
                                ["/mnt/sdcard/amit/amit.png","/mnt/sdcard/amit/amit.png","/mnt/sdcard/amit/amit.png","/mnt/sdcard/amit/amit.png","/mnt/sdcard/amit/amit.png"]
                            );
                        }

             </script>
        </body>
    </html>

EmailComposer.js

    cordova.define("cordova/plugin/emailcomposer", function(require, exports, module) {
        var exec = require('cordova/exec');

        var EmailComposer = function() {
            this.resultCallback = null; // Function
        };

        EmailComposer.ComposeResultType = {
            Cancelled:0,
            Saved:1,
            Sent:2,
            Failed:3,
            NotSent:4
        }


        // showEmailComposer : all args optional
        EmailComposer.prototype.showEmailComposer = function(successCallback,failureCallback,subject,body,toRecipients,ccRecipients,bccRecipients,bIsHTML,attachments) {
        console.log("showEmailComposer()");
        var args = {};
        if(toRecipients)
            args.toRecipients = toRecipients;
        if(ccRecipients)
            args.ccRecipients = ccRecipients;
        if(bccRecipients)
            args.bccRecipients = bccRecipients;
        if(subject)
            args.subject = subject;
        if(body)
            args.body = body;
        if(bIsHTML)
            args.bIsHTML = bIsHTML;
        if(attachments)
            args.attachments = attachments;

        cordova.exec(successCallback, failureCallback, "EmailComposer", "showEmailComposer", [args]);
    }


        var emailcomposer = new EmailComposer();
        module.exports = emailcomposer;
    });

EmailComposer.java

    package com.inic.emailattacher.emailcomposser;

    import java.io.File;
    import java.util.ArrayList;

    import org.apache.cordova.CallbackContext;
    import org.apache.cordova.CordovaPlugin;
    import org.apache.cordova.LOG;
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;

    import android.content.Intent;
    import android.net.Uri;
    import android.text.Html;


    public class EmailComposer extends CordovaPlugin {

       @Override
       public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
           if ("showEmailComposer".equals(action)) {

               try {
                   JSONObject parameters = args.getJSONObject(0);
                   if (parameters != null) {
                       sendEmail(parameters);
                   }
               } catch (Exception e) {

               }
               callbackContext.success();
               return true;
           }
           return false;  // Returning false results in a "MethodNotFound" error.
       }

       private void sendEmail(JSONObject parameters) {

           final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);

           //String callback = parameters.getString("callback");

           boolean isHTML = false;
           try {
               isHTML = parameters.getBoolean("bIsHTML");          
           } catch (Exception e) {
               LOG.e("EmailComposer", "Error handling isHTML param: " + e.toString());
           }

           if (isHTML) {
               emailIntent.setType("text/html");
           } else {
               emailIntent.setType("text/plain");
           }

           // setting subject
           try {
               String subject = parameters.getString("subject");
               if (subject != null && subject.length() > 0) {
                   emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
               }
           } catch (Exception e) {
               LOG.e("EmailComposer", "Error handling subject param: " + e.toString());
           }

           // setting body
           try {
               String body = parameters.getString("body");
               if (body != null && body.length() > 0) {
                   if (isHTML) {                                       
                       String bodyHtml = Html.fromHtml(body).toString();
                       LOG.e("EmailComposer", "Creating HTML email with body: " + bodyHtml);
                       ArrayList<String> extra_text = new ArrayList<String>();
                       extra_text.add(bodyHtml);
                       emailIntent.putStringArrayListExtra(android.content.Intent.EXTRA_TEXT, extra_text);
                   } else {
                       LOG.e("EmailComposer", "Creating text email with body: " + body);
                       ArrayList<String> extra_text = new ArrayList<String>();
                       extra_text.add(body);
                       emailIntent.putStringArrayListExtra(android.content.Intent.EXTRA_TEXT, extra_text);
                   }
               }
           } catch (Exception e) {
               LOG.e("EmailComposer", "Error handling body param: " + e.toString());
           }

           // setting TO recipients
           try {
               JSONArray toRecipients = parameters.getJSONArray("toRecipients");
               if (toRecipients != null && toRecipients.length() > 0) {
                   String[] to = new String[toRecipients.length()];
                   for (int i=0; i<toRecipients.length(); i++) {
                       to[i] = toRecipients.getString(i);
                   }
                   emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, to);
               }
           } catch (Exception e) {
               LOG.e("EmailComposer", "Error handling toRecipients param: " + e.toString());
           }

           // setting CC recipients
           try {
               JSONArray ccRecipients = parameters.getJSONArray("ccRecipients");
               if (ccRecipients != null && ccRecipients.length() > 0) {
                   String[] cc = new String[ccRecipients.length()];
                   for (int i=0; i<ccRecipients.length(); i++) {
                       cc[i] = ccRecipients.getString(i);
                   }
                   emailIntent.putExtra(android.content.Intent.EXTRA_CC, cc);
               }
           } catch (Exception e) {
               LOG.e("EmailComposer", "Error handling ccRecipients param: " + e.toString());
           }

           // setting BCC recipients
           try {
               JSONArray bccRecipients = parameters.getJSONArray("bccRecipients");
               if (bccRecipients != null && bccRecipients.length() > 0) {
                   String[] bcc = new String[bccRecipients.length()];
                   for (int i=0; i<bccRecipients.length(); i++) {
                       bcc[i] = bccRecipients.getString(i);
                   }
                   emailIntent.putExtra(android.content.Intent.EXTRA_BCC, bcc);
               }
           } catch (Exception e) {
               LOG.e("EmailComposer", "Error handling bccRecipients param: " + e.toString());
           }

           // setting attachments
           try {
               JSONArray attachments = parameters.getJSONArray("attachments");
               if (attachments != null && attachments.length() > 0) {
                   ArrayList<Uri> uris = new ArrayList<Uri>();
                   //convert from paths to Android friendly Parcelable Uri's
                   for (int i=0; i<attachments.length(); i++) {
                       try {
                           File file = new File(attachments.getString(i));
                           if (file.exists()) {
                               Uri uri = Uri.fromFile(file);
                               uris.add(uri);
                           }
                       } catch (Exception e) {
                           LOG.e("EmailComposer", "Error adding an attachment: " + e.toString());
                       }
                   }
                   if (uris.size() > 0) {
                       emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
                   }
               }
           } catch (Exception e) {
               LOG.e("EmailComposer", "Error handling attachments param: " + e.toString());
           }

           this.cordova.startActivityForResult(this, emailIntent, 0);
       }

       @Override
       public void onActivityResult(int requestCode, int resultCode, Intent intent) {
           // TODO handle callback
           super.onActivityResult(requestCode, resultCode, intent);
           LOG.e("EmailComposer", "ResultCode: " + resultCode);
           // IT DOESN'T SEEM TO HANDLE RESULT CODES
       }

    }

res-> xml-> config.xml

 <feature name="EmailComposer">
    <param name="android-package" value="com.inic.emailattacher.emailcomposser.EmailComposer" />
</feature>

AndroidManifest.xml

     <uses-permission android:name="android.permission.INTERNET" />

     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

在“ res / xml / config.xml”中定義您的插件

在文件中找到這些行

<feature name="App">
        <param name="android-package" value="org.apache.cordova.App" />
</feature>

並在以下內容后附加:

<feature name="MyPluginName">
        <param name="android-package" value="com.phonegap.plugins.plugin.class" />
</feature>

用實際名稱替換虛擬名稱(MyPluginName,plugins.plugin.class等)。 這可行。

但這在Android中

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM