我应该用什么代替“插件”为Phonegap 2.9? [英] What am I supposed to use in place of "Plugin" for Phonegap 2.9?

查看:274
本文介绍了我应该用什么代替“插件”为Phonegap 2.9?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为表示我应该使用CordovaPlugin,但Eclipse似乎喜欢甚至更少:





我不知道这是否是WebIntent对我失败的原因,但它似乎是一个很可能的嫌疑人。清除这个文件的任何错误怎么办?



另外,我不知道这是否相关,但代码中有另一行触发另一个depracated警告:





所以可能是WebIntent需要一些更一般的修复或某些东西。



无论如何,我只想让WebIntent工作。关于如何做到这一点的建议,无论是否涉及修复此插件错误,都将不胜感激。






更新:我尝试使用CordovaPlugin编辑代码,但我仍然收到错误。



我收到错误的行是:





...和:





这是整个文件:

  package com.borismus.webintent ; 

import java.util.HashMap;
import java.util.Map;

import org.apache.cordova.DroidGap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.text.Html;

import org.apache.cordova.api.CordovaPlugin;
import org.apache.cordova.api.PluginResult;
import org.apache.cordova.api.CallbackContext;

/ **
* WebIntent是一个PhoneGap插件,用于连接Android Intent和web
*应用程序:
*
* 1.网络应用程序可以产生意图调用本机Android应用程序。 2.
*(在为PhoneGap应用程序设置正确的意图过滤器之后),Android
*意图可以由PhoneGap Web应用程序处理。
*
* @author boris@borismus.com
*
* /
public class WebIntent extends CordovaPlugin {

private String onNewIntentCallback =空值;

/ **
*执行请求并返回PluginResult。
*
* @param action
*要执行的操作。
* @param args
*插件的参数的JSONArray。
* @param callbackContext
*回调到JavaScript时使用的callbackContext。
* @return boolean
* /
public boolean execute(String action,JSONArray args,CallbackContext callbackContext){
try {
if(action.equals(startActivity )){
if(args.length()!= 1){
PluginResult res = new PluginResult(PluginResult.Status.INVALID_ACTION);
callbackContext.sendPluginResult(res);
返回false;
}

//解析参数
JSONObject obj = args.getJSONObject(0);
String type = obj.has(type)? obj.getString(type):null;
Uri uri = obj.has(url)? Uri.parse(obj.getString(url)):null;
JSONObject extras = obj.has(extras)? obj.getJSONObject(extras):null;
映射< String,String> extrasMap = new HashMap< String,String>();

//填充附加值(如果有)
if(extras!= null){
JSONArray extraNames = extras.names(); (int i = 0; i< extraNames.length(); i ++){
String key = extraNames.getString(i);

String value = extras.getString(key);
extrasMap.put(key,value);
}
}

startActivity(obj.getString(action),uri,type,extrasMap);
callbackContext.success();
返回true;

} else if(action.equals(hasExtra)){
if(args.length()!= 1){
PluginResult res = new PluginResult(PluginResult Status.INVALID_ACTION);
callbackContext.sendPluginResult(res);
返回false;
}
意图i =((DroidGap)this.cordova.getActivity())。getIntent();
String extraName = args.getString(0);
PluginResult res = new PluginResult(PluginResult.Status.OK,i.hasExtra(extraName));
callbackContext.sendPluginResult(res);
返回true;

} else if(action.equals(getExtra)){
if(args.length()!= 1){
PluginResult res = new PluginResult(PluginResult Status.INVALID_ACTION);
callbackContext.sendPluginResult(res);
返回false;
}
意图i =((DroidGap)this.cordova.getActivity())。getIntent();
String extraName = args.getString(0);

if(i.hasExtra(extraName)){
PluginResult res = new PluginResult(PluginResult.Status.OK,i.hasExtra(extraName));
callbackContext.sendPluginResult(res);
返回true;

} else {
PluginResult res = new PluginResult(PluginResult.Status.ERROR);
callbackContext.sendPluginResult(res);
返回false;
}

} else if(action.equals(getUri)){
if(args.length()!= 0){
PluginResult res =新的PluginResult(PluginResult.Status.INVALID_ACTION);
callbackContext.sendPluginResult(res);
返回false;
}

意图i =((DroidGap)this.cordova.getActivity())。getIntent();
String uri = i.getDataString();

callbackContext.success(uri);
返回true;

} else if(action.equals(onNewIntent)){
if(args.length()!= 0){
PluginResult res = new PluginResult(PluginResult Status.INVALID_ACTION);
callbackContext.sendPluginResult(res);
返回false;
}

this.onNewIntentCallback = callbackContext;
PluginResult res = new PluginResult(PluginResult.Status.NO_RESULT);
res.setKeepCallback(true);
callbackContext.sendPluginResult(res);
返回true;

} else if(action.equals(sendBroadcast))
{
if(args.length()!= 1){
PluginResult res = new PluginResult(PluginResult.Status.INVALID_ACTION);
callbackContext.sendPluginResult(res);
返回false;
}

//解析参数
JSONObject obj = args.getJSONObject(0);

JSONObject extras = obj.has(extras)? obj.getJSONObject(extras):null;
映射< String,String> extrasMap = new HashMap< String,String>();

//填充附加值(如果有)
if(extras!= null){
JSONArray extraNames = extras.names(); (int i = 0; i< extraNames.length(); i ++){
String key = extraNames.getString(i);

String value = extras.getString(key);
extrasMap.put(key,value);
}
}

sendBroadcast(obj.getString(action),extrasMap);
callbackContext.success();
返回true;

}

PluginResult res = new PluginResult(PluginResult.Status.INVALID_ACTION);
callbackContext.sendPluginResult(res);
返回false;

} catch(JSONException e){
callbackContext.error(e.getMessage());
返回false;
}
}

@Override
public void onNewIntent(Intent intent){
if(this.onNewIntentCallback!= null){
this.onNewIntentCallback.success(intent.getDataString());
}
}

void startActivity(String action,Uri uri,String type,Map< String,String> extras){
Intent i =(uri! null?new Intent(action,uri):new Intent(action));

if(type!= null&& uri!= null){
i.setDataAndType(uri,type); //修复android 2.3.6
}的崩溃问题else {
if(type!= null){
i.setType(type);
}
}

for(String key:extras.keySet()){
String value = extras.get(key);
//如果type是text html,则额外的文本必须以HTML
的形式发送(如果是key.equals(Intent.EXTRA_TEXT)&& type.equals(text / html)){
i.putExtra(key,Html.fromHtml(value));
} else if(key.equals(Intent.EXTRA_STREAM)){
//允许将图像共享为附件。
//在这种情况下,值应该是一个文件的URI
i.putExtra(key,Uri.parse(value));
} else if(key.equals(Intent.EXTRA_EMAIL)){
//允许添加接收者的电子邮件地址
i.putExtra(Intent.EXTRA_EMAIL,new String [] { value});
} else {
i.putExtra(key,value);
}
}
((DroidGap)this.cordova.getActivity())。startActivity(i);
}

void sendBroadcast(String action,Map< String,String> extras){
Intent intent = new Intent();
intent.setAction(action);
for(String key:extras.keySet()){
String value = extras.get(key);
intent.putExtra(key,value);
}

((DroidGap)this.cordova.getActivity())。sendBroadcast(intent);
}
}


解决方案

你正在使用Phonegap 2.9,所以插件需要是CordovaPlugin。



但是如果你看到,两者的返回类型是不同的。 插件期望返回为PluginResult实例,其中CordovaPlugin将返回值作为布尔值,并使用CallbackContext实例触发回调。某些东西像

  callbackContext.success(Message:OK); 

  callbackContext.error(Expected one non-empty string argument。); 

您可以深入了解这里的文档,http://docs.phonegap.com/en/2.9.0/guide_plugin-development_android_index .md.html#开发%20a%20插件%20on%20Android


I am trying to implement the WebIntent plugin for Phonegap as part of my Android app.

I have downloaded the latest version of WebIntent, within this week, and it has not been updated for about half a year, so I think it is as up to date as possible. I am using version 2.9 of Phonegap.

WebIntent is not working for me, and in my Eclipse interface, it says that the class "Plugin" is deprecated:

This answer here on Stack Overflow indicates that I should use "CordovaPlugin" instead, but Eclipse seems to like that even less:

I'm not sure if this is the reason WebIntent is failing for me, but it looks to be a very likely suspect. What do I do to clear this file of any errors?

Also, I don't know if this is related, but there is another line in the code that triggers another "depracated" warning:

So it may be that WebIntent needs some more general fix or something.

In any case, I just want to get WebIntent working. Advice on how to do that, whether it involves fixing this Plugin error or not, would be most appreciated.


Update: I have tried to edit the code using "CordovaPlugin", but I am still getting errors.

The lines I am getting errors on are:

... and:

This is the whole file:

package com.borismus.webintent;

import java.util.HashMap;
import java.util.Map;

import org.apache.cordova.DroidGap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.text.Html;

import org.apache.cordova.api.CordovaPlugin;
import org.apache.cordova.api.PluginResult;
import org.apache.cordova.api.CallbackContext;

/**
 * WebIntent is a PhoneGap plugin that bridges Android intents and web
 * applications:
 *
 * 1. web apps can spawn intents that call native Android applications. 2.
 * (after setting up correct intent filters for PhoneGap applications), Android
 * intents can be handled by PhoneGap web applications.
 *
 * @author boris@borismus.com
 *
 */
public class WebIntent extends CordovaPlugin {

    private String onNewIntentCallback = null;

    /**
     * Executes the request and returns PluginResult.
     *
     * @param action
     *            The action to execute.
     * @param args
     *            JSONArray of arguments for the plugin.
     * @param callbackContext
     *            The callbackContext used when calling back into JavaScript.
     * @return boolean
     */
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
        try {
            if (action.equals("startActivity")) {
                if (args.length() != 1) {
                    PluginResult res = new PluginResult(PluginResult.Status.INVALID_ACTION);
                    callbackContext.sendPluginResult(res);
                    return false;
                }

                // Parse the arguments
                JSONObject obj = args.getJSONObject(0);
                String type = obj.has("type") ? obj.getString("type") : null;
                Uri uri = obj.has("url") ? Uri.parse(obj.getString("url")) : null;
                JSONObject extras = obj.has("extras") ? obj.getJSONObject("extras") : null;
                Map<String, String> extrasMap = new HashMap<String, String>();

                // Populate the extras if any exist
                if (extras != null) {
                    JSONArray extraNames = extras.names();
                    for (int i = 0; i < extraNames.length(); i++) {
                        String key = extraNames.getString(i);
                        String value = extras.getString(key);
                        extrasMap.put(key, value);
                    }
                }

                startActivity(obj.getString("action"), uri, type, extrasMap);
                callbackContext.success();
                return true;

            } else if (action.equals("hasExtra")) {
                if (args.length() != 1) {
                    PluginResult res = new PluginResult(PluginResult.Status.INVALID_ACTION);
                    callbackContext.sendPluginResult(res);
                    return false;
                }
                Intent i = ((DroidGap)this.cordova.getActivity()).getIntent();
                String extraName = args.getString(0);
                PluginResult res = new PluginResult(PluginResult.Status.OK, i.hasExtra(extraName));
                callbackContext.sendPluginResult(res);
                return true;

            } else if (action.equals("getExtra")) {
                if (args.length() != 1) {
                    PluginResult res = new PluginResult(PluginResult.Status.INVALID_ACTION);
                    callbackContext.sendPluginResult(res);
                    return false;
                }
                Intent i = ((DroidGap)this.cordova.getActivity()).getIntent();
                String extraName = args.getString(0);

                if (i.hasExtra(extraName)) {
                    PluginResult res = new PluginResult(PluginResult.Status.OK, i.hasExtra(extraName));
                    callbackContext.sendPluginResult(res);
                    return true;

                } else {
                    PluginResult res = new PluginResult(PluginResult.Status.ERROR);
                    callbackContext.sendPluginResult(res);
                    return false;
                }

            } else if (action.equals("getUri")) {
                if (args.length() != 0) {
                    PluginResult res = new PluginResult(PluginResult.Status.INVALID_ACTION);
                    callbackContext.sendPluginResult(res);
                    return false;
                }

                Intent i = ((DroidGap)this.cordova.getActivity()).getIntent();
                String uri = i.getDataString();

                callbackContext.success(uri);
                return true;

            } else if (action.equals("onNewIntent")) {
                if (args.length() != 0) {
                    PluginResult res = new PluginResult(PluginResult.Status.INVALID_ACTION);
                    callbackContext.sendPluginResult(res);
                    return false;
                }

                this.onNewIntentCallback = callbackContext;
                PluginResult res = new PluginResult(PluginResult.Status.NO_RESULT);
                res.setKeepCallback(true);
                callbackContext.sendPluginResult(res);
                return true;

            } else if (action.equals("sendBroadcast"))
            {
                if (args.length() != 1) {
                    PluginResult res = new PluginResult(PluginResult.Status.INVALID_ACTION);
                    callbackContext.sendPluginResult(res);
                    return false;
                }

                // Parse the arguments
                JSONObject obj = args.getJSONObject(0);

                JSONObject extras = obj.has("extras") ? obj.getJSONObject("extras") : null;
                Map<String, String> extrasMap = new HashMap<String, String>();

                // Populate the extras if any exist
                if (extras != null) {
                    JSONArray extraNames = extras.names();
                    for (int i = 0; i < extraNames.length(); i++) {
                        String key = extraNames.getString(i);
                        String value = extras.getString(key);
                        extrasMap.put(key, value);
                    }
                }

                sendBroadcast(obj.getString("action"), extrasMap);
                callbackContext.success();
                return true;

            }

            PluginResult res = new PluginResult(PluginResult.Status.INVALID_ACTION);
            callbackContext.sendPluginResult(res);
            return false;

        } catch (JSONException e) {
            callbackContext.error(e.getMessage());
            return false;
        }
    }

    @Override
    public void onNewIntent(Intent intent) {
        if (this.onNewIntentCallback != null) {
            this.onNewIntentCallback.success(intent.getDataString());
        }
    }

    void startActivity(String action, Uri uri, String type, Map<String, String> extras) {
        Intent i = (uri != null ? new Intent(action, uri) : new Intent(action));

        if (type != null && uri != null) {
            i.setDataAndType(uri, type); //Fix the crash problem with android 2.3.6
        } else {
            if (type != null) {
                i.setType(type);
            }
        }

        for (String key : extras.keySet()) {
            String value = extras.get(key);
            // If type is text html, the extra text must sent as HTML
            if (key.equals(Intent.EXTRA_TEXT) && type.equals("text/html")) {
                i.putExtra(key, Html.fromHtml(value));
            } else if (key.equals(Intent.EXTRA_STREAM)) {
                // allowes sharing of images as attachments.
                // value in this case should be a URI of a file
                i.putExtra(key, Uri.parse(value));
            } else if (key.equals(Intent.EXTRA_EMAIL)) {
                // allows to add the email address of the receiver
                i.putExtra(Intent.EXTRA_EMAIL, new String[] { value });
            } else {
                i.putExtra(key, value);
            }
        }
        ((DroidGap)this.cordova.getActivity()).startActivity(i);
    }

    void sendBroadcast(String action, Map<String, String> extras) {
        Intent intent = new Intent();
        intent.setAction(action);
        for (String key : extras.keySet()) {
            String value = extras.get(key);
            intent.putExtra(key, value);
        }

        ((DroidGap)this.cordova.getActivity()).sendBroadcast(intent);
    }
}

解决方案

If you are using Phonegap 2.9, so the "Plugin" needs to be "CordovaPlugin".

But if you see, the return type of both are different. "Plugin" expects return as a "PluginResult" instance, where as "CordovaPlugin" expects return as a boolean value, and the callback is triggered using "CallbackContext" instance. some thing like

callbackContext.success("Message : OK");

or

callbackContext.error("Expected one non-empty string argument.");

You can have a deep look into this over the docs here http://docs.phonegap.com/en/2.9.0/guide_plugin-development_android_index.md.html#Developing%20a%20Plugin%20on%20Android

这篇关于我应该用什么代替“插件”为Phonegap 2.9?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆