4

変更された WebIntent プラグインを使用してフォーム アプリにインテントを呼び出す Phonegap 2.0 でアプリを構築しています。ユーザーをフォームアプリに正常に送信できますが、ユーザー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>

何か案は?

4

2 に答える 2

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

あなたの問題に対するあなたの答えを見ました..しかし、PhoneGapにフックする必要があるネイティブAndroidの人の中には、アクティビティをプラグインに戻す方法を知りたいと思う人もいるかもしれません:

this.cordova.setActivityResultCallback(MyActivity.this);

呼び出しを行う前に呼び出されstartActivityForResult()ます。誰も答えていないように見える、本当に私を悩ませていた何か。

于 2013-01-08T16:38:19.200 に答える
0

したがって、画面の回転がアプリをリロードするという問題がありました。android:configChanges="orientation" マニフェストに追加する必要がありまし た。残念ながら、configChanges オプションに複数の値を指定できるかのように、誰もがこのオプションをリストしていなければ、これはもっと早く解決されていたでしょう。

これは毎回エラーになります。 android:configChanges="orientation|keyboard"

于 2012-12-18T16:49:34.043 に答える