【问题标题】:Adding Custom Calendar Events – Phonegap Android plugin添加自定义日历事件 – Phonegap Android 插件
【发布时间】:2013-04-02 12:11:32
【问题描述】:

下面是我的 CalendarEventPlugin.js 文件:

window.addcalendareventplugin = function(startDate, endDate, allDay, successCallback){     
//call the Plugin execute method()
console.log(successCallback,function(err){
callback('Error: ' + err);      
},"CalendarEventPlugin","addCalendarEvent",[startDate, endDate, allDay]); 
}

我使用的是 cordova-2.5.0 和 Android 2.2 及更高版本。

下面是我的 CalendarEventPlugin.java 文件:

package org.apache.cordova.plugin;

import java.util.Calendar;
import org.json.JSONArray;
import org.json.JSONException;
import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;

import android.content.Intent;
import android.provider.CalendarContract;
import android.provider.CalendarContract.Events;

public class CalendarEventPlugin extends CordovaPlugin {        
    @Override       
    public boolean execute(String action, JSONArray data, CallbackContext callbackContext) throws JSONException {           
            if (action.equals("addCalendarEvent")) {
                    if(data.length() > 0)
                    {
                            //if you are passing some data eg. start,end date, all day and parameters for the Calendar
                            //from plugin's javascript interface then you can retrieve and parse the data from data[] here. 
                           String startDate = data.getString(0);
                    String endDate = data.getString(1);
                    boolean allDay = data.getBoolean(2);                                                        
                            if(this.addCalendarEvent())
                            {
                                    callbackContext.success("Done!!");
                            }
                            else
                            {
                                    callbackContext.error("Error!!");
                            }
                    }                       
                    return true;
            }
            return false;
    }       

    private boolean addCalendarEvent(){     
            Boolean ret = false;
            try{
                    Calendar beginTime = Calendar.getInstance(); //the begin time and end time can come from javascript if user enters this values in a form
            beginTime.set(2013, 1, 20, 7, 30);
            Calendar endTime = Calendar.getInstance();
            endTime.set(2013, 1, 20, 8, 30); //set your own time here and above as well or you can get the current time.

            Intent intent = new Intent(Intent.ACTION_EDIT);
            intent.setType("vnd.android.cursor.item/event");
            intent.setAction(Intent.ACTION_INSERT);
            intent.putExtra(Events.TITLE, "A new event"); //can come from javascript.
            intent.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, true);
            intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, beginTime.getTimeInMillis());
            intent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime.getTimeInMillis());

            this.cordova.startActivityForResult(this, intent, 0);

            ret = true;
            }
            catch(Exception e){
                    e.printStackTrace();
                    ret = false;
            }
            return ret;
    }       
   }

下面是我的索引文件:

     <!DOCTYPE HTML>
     <html>
     <head>
     <title>Cordova</title>
     <script type="text/javascript" charset="utf-8" src="cordova-2.5.0.js"></script>
     <script type="text/javascript" charset="utf-8" src="CalenderEventPlugin.js">           </script>
     <script type="text/javascript" charset="utf-8" src="app.js"></script>
     </head>
     <body>
     <h4>Custom Plugin Development</h4>
     <button onclick="test5()">Calendar Event Plugin</button>

     <br/>
     </body>
     </html>

下面是我的 app.js 文件:

    function test5(){
//alert("call of app js file");
    window.addcalendareventplugin("2015-09-09T16:00:00-06:00", "2015-09-09T18:00:00-06:00","true",function(val){
    alert(val);   //once success message come and you have tested it, you can remove this alert.
    });
  }

我无法调用 addCalenderEvent 。我收到 Json 错误。 任何人都可以帮忙。 提前谢谢..:)

【问题讨论】:

    标签: android cordova phonegap-plugins android-calendar


    【解决方案1】:

    JS 应该是这样的:

    window.addcalendareventplugin = function(startDate, endDate, allDay, successCallback){     
        //call the Plugin execute method()
        cordova.exec(successCallback,function(err){
            console.log('Error: ' + err);      
        },"CalendarEventPlugin","addCalendarEvent",[startDate, endDate, allDay]); 
    }
    

    Java 应该是这样的:

    package org.apache.cordova.plugin;
    
    import java.util.Calendar;
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.apache.cordova.api.CallbackContext;
    import org.apache.cordova.api.CordovaPlugin;
    
    import android.content.Intent;
    import android.provider.CalendarContract;
    import android.provider.CalendarContract.Events;
    
    public class CalendarEventPlugin extends CordovaPlugin {        
    @Override       
    public boolean execute(String action, JSONArray data, CallbackContext callbackContext) throws JSONException {           
            if (action.equals("addCalendarEvent")) {
                    if(data.length() > 0)
                    {
                            //if you are passing some data eg. start,end date, all day and parameters for the Calendar
                            //from plugin's javascript interface then you can retrieve and parse the data from data[] here.                                                         
                            String startDate = data.getString(0);
                            String endDate = data.getString(1);
                            String allDay = data.getBoolean(2);
                            if(this.addCalendarEvent())
                            {
                                    callbackContext.success("Done!!");
                            }
                            else
                            {
                                    callbackContext.error("Error!!");
                            }
                    }                       
                    return true;
            }
            return false;
    }       
    
    private boolean addCalendarEvent(){     
            Boolean ret = false;
            try{
                    Calendar beginTime = Calendar.getInstance(); //the begin time and end time can come from javascript if user enters this values in a form
            beginTime.set(2013, 1, 20, 7, 30);
            Calendar endTime = Calendar.getInstance();
            endTime.set(2013, 1, 20, 8, 30); //set your own time here and above as well or you can get the current time.
    
            Intent intent = new Intent(Intent.ACTION_EDIT);
            intent.setType("vnd.android.cursor.item/event");
            intent.setAction(Intent.ACTION_INSERT);
            intent.putExtra(Events.TITLE, "A new event"); //can come from javascript.
            intent.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, true);
            intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, beginTime.getTimeInMillis());
            intent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime.getTimeInMillis());
    
            this.cordova.startActivityForResult(this, intent, 0);
    
            ret = true;
            }
            catch(Exception e){
                    e.printStackTrace();
                    ret = false;
            }
            return ret;
    }       
    }
    

    【讨论】:

    • 我正在使用 Cordova 2.5.0,出现 ReferenceError: Can't find the variable:callback at file://android_asset/WWW/cordova-2.5.0.js:970
    • 哎呀抱歉,我修复了 JS 代码中的错误。当您尝试在文本字段中编写代码时会发生这种情况。
    • callback('Error: ' + err); 更改为 console.log('Error: ' + err); 我得到错误:文件中的 JSON 错误:///android_asset/www/CalenderEventPlugin.js:4 :(
    • 那么该文件的第 4 行是什么?
    • 我看到 function(err) 正在执行。和console.log('错误:' + err);在日志中显示 Json 错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多