Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – Phonegap Cordova 2.0 onActivityResult未调用大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在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,value);
                }
            }

            this.startActivityForResult(obj.getString("action"),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,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,String> extras) {
    System.out.println("startActivityForResult invoked");
    /*Intent i = (uri != null ? new Intent(action,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,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://scheR_699_11845@as.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>

有任何想法吗?

解决方法

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

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

this.cordova.setActivityResultCallBACk(MyActivity.this);

在执行startActivityForResult()调用之前调用.有些事情让我感到困扰,似乎没有人回答过.

大佬总结

以上是大佬教程为你收集整理的android – Phonegap Cordova 2.0 onActivityResult未调用全部内容,希望文章能够帮你解决android – Phonegap Cordova 2.0 onActivityResult未调用所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。