編輯:用戶QuickFix的答案爲我工作。代碼在這個問題的底部。Cordova 3 Android插件 - 如何在主要活動中調用函數?
我想寫一個科爾多瓦3 Android插件,使正常和自定義吐司。不過,我只是一名前端開發人員,對科爾多瓦和Android來說都是新手。我仍然在學習,並希望你能給予任何幫助。
到目前爲止,我已經成功地單獨和成功地做到這2項任務:
- 編寫一個函數中的主要活動,使正常的和自定義吐司(自定義吐司簡直是在/ RES /佈局的RelativeLayout顯示一個圖標和一些文字)。
- 按照Devgirl的教程編寫Cordova插件:How to Write a PhoneGap 3.0 Plugin for Android。
我現在的問題是 - 如何讓插件在主要活動中調用showCustomToast()
函數?正如你可以在下面的代碼塊#2中看到的,我遇到了如何甚至得到的主要活動,所以我可以打電話showCustomToast()
的問題。下面是我當前如何這樣做的摘錄:
// Problem?
HelloCordova main = (HelloCordova) cordova.getActivity();
main.showCustomToast(toastTitle, toastText, duration);
我要投cordova.getActivity()
到HelloCordova
,否則也不會承認它有的showCustomToast()
功能。但肯定這是不是正確的方法,雖然它「工作」,即我能夠得到自定義Toast顯示在應用程序中。我只是忍不住覺得我已經完全錯誤地瞭解了這一點。這不完全是一個可重用的插件!
如果有人能讓我走上如何實現這一目標的正確道路,我將不勝感激。例如,我應該完全放棄插件,而只是放棄do this?
這是我的第一個Stackoverflow問題,請讓我知道如果我應該改變或澄清任何東西。感謝您的閱讀!
這裏是我現有的代碼:
代碼塊#1
這HelloCordova
類開始一個新的科爾多瓦項目時自動生成。我添加了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();
}
}
代碼塊#2
科爾多瓦插件的爪哇一部分。
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;
}
}
}
編輯:這是爲我工作的解決方案。正如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;
}
}
}
感謝@QuickFix更換,我不知道你能做到這一點!這已經成功了。對於任何需要幫助的人,我已經更新了我的原始問題,以顯示適合我的代碼。再次感謝,我很感激! – mank