Cordova 3 Android 插件 - 如何在主活动中调用函数?

man*_*ank 3 java plugins android cordova

编辑:用户 QuickFix 的回答对我有用。代码就在这个问题的底部。

我正在尝试编写一个 Cordova 3 Android 插件来制作普通和自定义 Toast。但是,我只是一名前端开发人员,对 Cordova 和 Android 非常陌生。我仍在学习,并希望您能提供任何帮助。

到目前为止,我已经成功地单独完成了这两项任务:

  1. 在主要活动中编写一个函数来制作普通和自定义 Toast(自定义 Toast 只是 /res/layout 中的一个 RelativeLayout,它显示了一个图标和一些文本)。
  2. 按照 Devgirl 的教程编写 Cordova 插件:如何为 Android 编写 PhoneGap 3.0 插件

我现在的问题是 - 如何让插件showCustomToast()在主活动中调用函数?正如您在下面的代码块 #2 中看到的那样,我遇到了如何获取主要活动以便我可以调用showCustomToast(). 这是我目前如何执行此操作的摘录:

// Problem?
HelloCordova main = (HelloCordova) cordova.getActivity();  
main.showCustomToast(toastTitle, toastText, duration);
Run Code Online (Sandbox Code Playgroud)

我必须强制转换cordova.getActivity()HelloCordova,否则它不会识别它具有showCustomToast()功能。但这肯定不是正确的方法,尽管它确实“有效”,即我能够在应用程序中显示自定义 Toast。我不禁觉得我完全以错误的方式解决了这个问题。目前它不完全是一个可重用的插件!

如果有人能让我走上实现这一目标的正确道路,我将不胜感激。例如,我是否应该完全放弃插件而只做这个

这是我的第一个 Stackoverflow 问题,所以如果我应该更改或澄清任何内容,请告诉我。谢谢阅读!!

这是我现有的代码:

代码块 #1

这个HelloCordova类是在启动一个新的 Cordova 项目时自动生成的。我加了这个showCustomToast()功能。

package io.cordova.hellocordova;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import org.apache.cordova.*;

public class HelloCordova extends CordovaActivity 
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        super.init();
        // Set by <content src="index.html" /> in config.xml
        super.loadUrl(Config.getStartUrl());
        //super.loadUrl("file:///android_asset/www/index.html")

    }
    public void showCustomToast(String toastTitleText, String toastDescText, int toastDuration) {
        Toast toast = new Toast(this);
        toast.setDuration(toastDuration);   

        LayoutInflater inflater = getLayoutInflater();
        View appearance = inflater.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toastRoot));
        toast.setView(appearance);

        TextView toastTitle = (TextView) appearance.findViewById(R.id.toastTitle);
        toastTitle.setText(toastTitleText);

        TextView toastDesc = (TextView) appearance.findViewById(R.id.toastDescription);
        toastDesc.setText(toastDescText);

        toast.show();
    }
}
Run Code Online (Sandbox Code Playgroud)

代码块 #2

Cordova 插件的 Java 部分。

package com.example.plugins.toast;

//Problem?
import io.cordova.hellocordova.HelloCordova;

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

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

public class ToastPlugin extends CordovaPlugin {

    final String LOG_TAG = "ToastLog";
    public static final String ACTION_NORMAL_TOAST = "normalToast";
    public static final String ACTION_CUSTOM_TOAST = "customToast";

    @Override
    public boolean execute(String action, JSONArray args,
            CallbackContext callbackContext) throws JSONException {

        final JSONObject arg_object = args.getJSONObject(0);
        final String toastTitle = arg_object.getString("toastTitle");
        final String toastText = arg_object.getString("toastText");
        final String toastDuration = arg_object.getString("toastDuration");

        final CallbackContext ctx = callbackContext;

        try {
            if (ACTION_NORMAL_TOAST.equals(action)) {
                Log.d(LOG_TAG, "Normal toast: " + toastText);

                Runnable runnable = new Runnable() {
                    public void run() {
                        Context context = cordova.getActivity()
                                .getApplicationContext();
                        int duration = Toast.LENGTH_SHORT;
                        if (toastDuration.equals("LONG")) {
                            duration = Toast.LENGTH_LONG;
                        }
                        Toast.makeText(context, toastText, duration).show();
                    }
                };
                this.cordova.getActivity().runOnUiThread(runnable);

                callbackContext.success();
                return true;
            } else if (ACTION_CUSTOM_TOAST.equals(action)) {
                Log.d(LOG_TAG, "Custom toast: " + toastTitle + ": " + toastText);

                Runnable runnable = new Runnable() {
                    public void run() {
                        int duration = Toast.LENGTH_SHORT;
                        if (toastDuration.equals("LONG")) {
                            duration = Toast.LENGTH_LONG;
                        }
                        //Problem?
                        HelloCordova main = (HelloCordova) cordova
                                .getActivity();
                        main.showCustomToast(toastTitle, toastText, duration);
                        ctx.success();

                    }
                };
                this.cordova.getActivity().runOnUiThread(runnable);

                callbackContext.success();
                return true;
            }
            callbackContext.error("Invalid action");
            return false;
        } catch (Exception e) {
            System.err.println("Exception: " + e.getMessage());
            callbackContext.error(e.getMessage());
            return false;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:这是对我有用的解决方案。正如 QuickFix 在下面的回答中提到的,自定义 Toast 代码现在在插件中。

package com.example.plugins.toast;

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

import android.content.Context;
import android.content.res.Resources;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class ToastPlugin extends CordovaPlugin {

    final String LOG_TAG = "ToastLog";
    public static final String ACTION_NORMAL_TOAST = "normalToast";
    public static final String ACTION_CUSTOM_TOAST = "customToast"; 

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {

        final JSONObject arg_object = args.getJSONObject(0);
        final String toastTitle = arg_object.getString("toastTitle");
        final String toastText = arg_object.getString("toastText");
        final String toastDuration = arg_object.getString("toastDuration");

        try {
            if (ACTION_NORMAL_TOAST.equals(action)) {

                Log.i(LOG_TAG, "[Normal toast] toastText: " + toastText);

                Runnable runnable = new Runnable() {
                    public void run() {
                        Context context = cordova.getActivity().getApplicationContext();
                        int duration = Toast.LENGTH_SHORT;
                        if (toastDuration.equals("LONG")) {
                            duration = Toast.LENGTH_LONG;
                        }
                        Toast.makeText(context, toastText, duration).show();
                    }
                };
                this.cordova.getActivity().runOnUiThread(runnable);
                callbackContext.success();
                return true;

            } else if (ACTION_CUSTOM_TOAST.equals(action)) {

                Log.i(LOG_TAG, "[Custom toast] toastTitle: " + toastTitle + "\n toastText: " + toastText);

                Runnable runnable = new Runnable() {
                    public void run() {
                        int duration = Toast.LENGTH_SHORT;
                        if (toastDuration.equals("LONG")) {
                            duration = Toast.LENGTH_LONG;
                        }

                        Context context = cordova.getActivity().getApplicationContext();

                        Toast toast = new Toast(context);
                        toast.setDuration(duration);    

                        LayoutInflater inflater = LayoutInflater.from(context);

                        Resources resources = context.getResources();                       
                        String packageName = context.getPackageName();

                        View appearance = inflater.inflate(resources.getIdentifier("toast_layout","layout",packageName),null);
                        toast.setView(appearance);

                        TextView toastTitleView = (TextView) appearance.findViewById(resources.getIdentifier("toastTitle","id",packageName));
                        toastTitleView.setText(toastTitle);

                        TextView toastDesc = (TextView) appearance.findViewById(resources.getIdentifier("toastDescription","id",packageName));
                        toastDesc.setText(toastText);

                        toast.show();
                    }
                };
                this.cordova.getActivity().runOnUiThread(runnable);
                callbackContext.success();
                return true;

            }
            callbackContext.error("Invalid action");
            return false;
        } catch (Exception e) {
            System.err.println("Exception: " + e.getMessage());
            callbackContext.error(e.getMessage());
            return false;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Qui*_*Fix 7

也许您可以将 showCustomToast 放在插件中而不是应用程序中?

在这种情况下,你将不得不在功能上取代R.layout.layoutnameR.id.viewname

getApplication().getResources().getIdentifier("layoutname","layout",getApplication().getPackageName());
Run Code Online (Sandbox Code Playgroud)

getApplication().getResources().getIdentifier("viewname","id",getApplication().getPackageName());
Run Code Online (Sandbox Code Playgroud)