Phonegap Cordova 2.0 onActivityResult未调用

时间:2012-12-05 18:21:25

标签: android cordova android-intent

我正在Phonegap 2.0中构建一个应用程序,该应用程序使用修改后的WebIntent插件将Intent调用到表单应用程序。我可以使用this.cordova.getActivity().startActivityForResult(intCanvas, 0);成功将用户发送到表单应用程序,但是一旦用户完成活动,他们就会被转出到主屏幕而不是返回到我的应用程序。

这是我正在使用的代码。

WebIntent.java

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.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.text.Html;

import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;

/**
 * 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 Plugin {

private String onNewIntentCallback = null;
private String callback;
public static final int REQUEST_CODE = 0;
/**
 * Executes the request and returns PluginResult.
 * 
 * @param action
 *            The action to execute.
 * @param args
 *            JSONArray of arguments for the plugin.
 * @param callbackId
 *            The callback id used when calling back into JavaScript.
 * @return A PluginResult object with a status and message.
 */
public PluginResult execute(String action, JSONArray args, String callbackId) {
    try {
        if (action.equals("startActivity")) {
            if (args.length() != 1) {
                return new PluginResult(PluginResult.Status.INVALID_ACTION);
            }

            // 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);
            return new PluginResult(PluginResult.Status.OK);

        } else if(action.equals("startActivityForResult")) {
            if (args.length() != 1) {
                return new PluginResult(PluginResult.Status.INVALID_ACTION);
            }
            this.callback = callbackId;
            // 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);
                }
            }

            this.startActivityForResult(obj.getString("action"), uri, type, extrasMap);
            PluginResult result = new PluginResult(PluginResult.Status.OK);
            result.setKeepCallback(true);
            return result;

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

        } else if (action.equals("getExtra")) {
            if (args.length() != 1) {
                return new PluginResult(PluginResult.Status.INVALID_ACTION);
            }
            Intent i = ((DroidGap)this.cordova.getContext()).getIntent();
            String extraName = args.getString(0);
            if (i.hasExtra(extraName)) {
                return new PluginResult(PluginResult.Status.OK, i.getStringExtra(extraName));
            } else {
                return new PluginResult(PluginResult.Status.ERROR);
            }
        } else if (action.equals("getUri")) {
            if (args.length() != 0) {
                return new PluginResult(PluginResult.Status.INVALID_ACTION);
            }

            Intent i = ((DroidGap)this.cordova.getContext()).getIntent();
            String uri = i.getDataString();
            return new PluginResult(PluginResult.Status.OK, uri);
        } else if (action.equals("onNewIntent")) {
            if (args.length() != 0) {
                return new PluginResult(PluginResult.Status.INVALID_ACTION);
            }

            this.onNewIntentCallback = callbackId;
            PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT);
            result.setKeepCallback(true);
            return result;
        } else if (action.equals("sendBroadcast")) 
        {
            if (args.length() != 1) {
                return new PluginResult(PluginResult.Status.INVALID_ACTION);
            }

            // 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);
            return new PluginResult(PluginResult.Status.OK);
        }
        return new PluginResult(PluginResult.Status.INVALID_ACTION);
    } catch (JSONException e) {
        e.printStackTrace();
        return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
    }
}

@Override
public void onNewIntent(Intent intent) {
    if (this.onNewIntentCallback != null) {
        PluginResult result = new PluginResult(PluginResult.Status.OK, intent.getDataString());
        result.setKeepCallback(true);
        this.success(result, this.onNewIntentCallback);
    }
}

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);
        }
    }
    this.cordova.getActivity().startActivity(i);
}
void startActivityForResult(String action, Uri uri, String type, Map<String, String> extras) {
    System.out.println("startActivityForResult invoked");
    /*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);
        }
    }*/
    //this.cordova.getActivity().startActivityForResult(i, REQUEST_CODE);
    //this.cordova.startActivityForResult(this, i, REQUEST_CODE);
    Intent intCanvas = new Intent("com.gocanvas.launchApp");
    intCanvas.setPackage("com.gocanvas");
    intCanvas.putExtra("Appname", "appname");
    intCanvas.putExtra("Username", "username");
    //System.out.println("intent call " + intCanvas);
    this.cordova.getActivity().startActivityForResult(intCanvas, 0);
}



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.getContext()).sendBroadcast(intent);
}

/**
 * Called when the activity exits
 *
 * @param requestCode       The request code originally supplied to startActivityForResult(),
 *                          allowing you to identify who this result came from.
 * @param resultCode        The integer result code returned by the child activity through its setResult().
 * @param intent            An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
 */
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == REQUEST_CODE) {
        if (resultCode == Activity.RESULT_OK) {
            this.success(new PluginResult(PluginResult.Status.OK), this.callback);
        } else {
            this.error(new PluginResult(PluginResult.Status.ERROR), this.callback);
        }
    }

}

}

清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="app.ivn"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" ></uses-sdk>
<supports-screens 
android:largeScreens="true" 
android:normalScreens="true" 
android:smallScreens="true" 
android:resizeable="false"
android:anyDensity="true" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
<application
    android:icon="@drawable/icon"
    android:label="@string/app_name" >
    <activity
        android:name=".IVNActivity"
        android:label="@string/app_name"
        android:windowSoftInputMode="adjustResize"
        android:screenOrientation="landscape"
        android:alwaysRetainTaskState="true"
        android:launchMode="standard">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

有什么想法吗?

2 个答案:

答案 0 :(得分:4)

this.cordova.getActivity().startActivityForResult(intCanvas, 0)

看到了你的问题答案..但是一些需要挂钩PhoneGap的原生Android玩家可能想知道你的活动回到你的插件的方式是确保:

this.cordova.setActivityResultCallback(MyActivity.this);
在进行startActivityForResult()调用之前会调用

。真的让我困扰的是,似乎没有人回答。

答案 1 :(得分:0)

所以这是屏幕旋转重新加载应用程序的问题。我只需要添加android:configChanges="orientation" 清单。不幸的是,如果每个人都没有列出这个选项,就好像你可以在configChanges选项中有多个值一样,这个问题就会得到解决。

每次都会出错。 android:configChanges="orientation|keyboard"