2012-12-05 179 views
4

我建立在PhoneGap的2.0應用程序使用修改WebIntent插件調用的意圖的形式應用。我可以在用戶成功發送到使用this.cordova.getActivity().startActivityForResult(intCanvas, 0);表單應用程序,但一旦用戶完成了活動,他們被傾倒出來到主屏幕,而不是返回到我的應用程序。PhoneGap的科爾多瓦2.0 onActivityResult不叫

這裏是我使用的代碼。

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 [email protected] 
* 
*/ 
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> 

任何想法?

+0

你運行的內存?即使在這種情況下,您通常會被帶回Cordova,但州通常會丟失,您無法從您的活動中獲得結果。這種行爲在設備上是否一致? –

+0

不幸的是,我只能在ZTE Optik上測試它,因爲Canvas App我打算在最新的Android版本上使用Nexus 7的bug。日食LogCat或控制檯會顯示內存問題消息嗎?我沒有看到這樣的事情。 – LuckyStrike2021

+0

是的,logcat會顯示。這是否在模擬器中做到這一點?我相信模擬器遠遠超過我相信中興通訊設備上發生的事情,這種情況有着不可預測的表演歷史。 –

回答

0

因此,它與屏幕旋轉重新加載應用程序中的OLE問題。我只需要將android:configChanges="orientation" 添加到清單。不幸的是,如果每個人都沒有列出這個選項,就好像你可以在configChanges選項中有多個值一樣。

每一次這樣的錯誤了。 android:configChanges="orientation|keyboard"

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

看到你對你的問題的答案..但一些原生的Android傢伙誰需要掛接到PhoneGap的可能想知道你可以得到你的活動,以回到你的插件是路確保:

this.cordova.setActivityResultCallback(MyActivity.this); 

你做startActivityForResult()調用之前被調用。有些事情真的困擾着我,似乎沒有人回答。

+0

這對Cordova 3.1和我的自定義插件有效 –

相關問題