Cordova 3 Android插件 - 如何在主活动中调用函数? [英] Cordova 3 Android plugin - how to call a function in main activity?
问题描述
编辑:用户QuickFix的答案对我有用。
我试图写一个Cordova 3 Android插件,可以使正常和自定义的Toast。但是,我只是一个前端开发人员,非常新的Cordova和Android。
I am trying to write a Cordova 3 Android plugin that makes normal and custom Toast. However, I am just a front-end developer and very new to Cordova and Android. I am still learning and would appreciate any help you can give.
到目前为止,我已经成功地完成了这两个任务:
So far I have managed to do these 2 tasks individually and successfully:
- 在主活动中编写一个函数,使正常和自定义Toast(自定义Toast仅仅是一个显示图标和一些文本的/ res / layout中的RelativeLayout)。
- 按照Devgirl的教程编写Cordova插件:
My problem now is - how do I get the plugin to call the showCustomToast()
function in the main activity? As you can see in Code block #2 below, I ran into the problem of how to even get the main activity so I could call showCustomToast()
. Here is the extract of how I am currently doing this:
p>我现在的问题是 - 我如何让插件调用主活动中的 showCustomToast()
函数?正如你可以在下面的代码块#2中看到的,我遇到了如何甚至>主要活动的问题,所以我可以调用 showCustomToast()
。下面是我目前如何做的提取:
// Problem?
HelloCordova main = (HelloCordova) cordova.getActivity();
main.showCustomToast(toastTitle, toastText, duration);
I have to cast cordova.getActivity()
to HelloCordova
, otherwise it won't recognise that it has the showCustomToast()
function. But surely this is not the correct approach, although it does "work", ie, I am able to get custom Toast to show in the app. I just can't help but feel that I've gone about this completely the wrong way. It's not exactly a reusable plugin at the moment!
我必须演示 cordova.getActivity()
到 HelloCordova
,否则它不会识别它
I would be very grateful if someone could set me on the right path of how to achieve this. For example, should I give up on the plugin completely and just do this instead?
如果有人能让我如何实现这一点,我将非常感谢。例如,我应该完全放弃插件,然后执行此操作 ?
This is my first Stackoverflow question so please let me know if I should change or clarify anything. Thank you for reading!!
这是我的第一个Stackoverflow问题,所以请让我知道,如果我应该更改或澄清任何东西。谢谢您阅读!
Here's my existing code:
这是我现有的代码:
代码块# b>
This HelloCordova
class was automatically generated when starting a new Cordova project. I added the showCustomToast()
function.
这个 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();
}
}
Code block #2
代码块#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;
}
}
}
Edit: Here is the solution that worked for me. As QuickFix mentioned in their answer below, the custom toast code is now in the plugin.
是为我工作的解决方案。正如下面的答案中提到的QuickFix一样,自定义Toast代码现在在插件中。
推荐答案
在这种情况下你必须在函数中替换 R.layout.layoutname
和 R.id.viewname
与
In that case you will have to replace in the function the R.layout.layoutname
and R.id.viewname
with
getApplication().getResources().getIdentifier("layoutname","layout",getApplication().getPackageName());
和
getApplication().getResources().getIdentifier("viewname","id",getApplication().getPackageName());
这篇关于Cordova 3 Android插件 - 如何在主活动中调用函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!