如何从cordova访问移动设置

时间:2015-11-29 17:04:48

标签: android ios cordova mobile

我正在开发来自cordova的移动应用程序,我设计的应用程序可以在离线模式下工作 如果用户使用Cordova检查自动日期和时间,我正在尝试访问移动设置

1 个答案:

答案 0 :(得分:0)

据我所知,现有的Cordova插件不能提供在Android上显示日期/时间设置页面的功能。话虽如此,创建自己的相当容易。在我的头顶,你可以把你自己的"日期设置"插件是这样的:

<强>的plugin.xml

<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
    xmlns:android="http://schemas.android.com/apk/res/android"
    id="cordova-plugin-date-setting"
    version="0.0.1">

    <name>Date Setting</name>
    <description></description>
    <author></author>
    <license>MIT</license>
    <keywords>ecosystem:cordova</keywords>
    <engines>
        <engine name="cordova" version=">=3.0.0" />
    </engines>

    <platform name="android">
        <config-file target="config.xml" parent="/*">
            <feature name="DateSetting" >
                <param name="android-package" value="cordova.plugins.DateSetting"/>
            </feature>
        </config-file>

        <js-module src="datesetting.js" name="Diagnostic">
            <clobbers target="cordova.plugins.DateSetting" />
        </js-module>
        <source-file src="DateSetting.java" target-dir="src/cordova/plugins" />
    </platform>
</plugin>

<强> datesetting.js

var DateSetting = (function(){

    DateSetting = {
        open: function(successFn, errorFn){
            cordova.exec(successFn,errorFn,'DateSetting','open',[]);
        }
    };
    return DateSetting;
});
module.exports = new DateSetting();

<强> DateSetting.java

package cordova.plugins;

import android.content.Intent;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;

public class DateSetting extends CordovaPlugin {
    public DateSetting() {}

    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        try {
            if (action.equals("open")){
                cordova.getActivity().startActivity(new Intent(android.provider.Settings.ACTION_DATE_SETTINGS));
                callbackContext.success();
            }else {
                callbackContext.error("Invalid action");
                return false;
            }
        }catch(Exception e ) {
            callbackContext.error("Exception occurred: ".concat(e.getMessage()));
            return false;
        }
        return true;
    }
}