2012-12-05 3 views
4

수정 된 WebIntent 플러그인을 사용하여 인 텐트를 양식 앱에 전화하는 Phonegap 2.0에 앱을 제작하고 있습니다. this.cordova.getActivity().startActivityForResult(intCanvas, 0);을 사용하여 양식 앱에 사용자를 성공적으로 보낼 수 있지만 일단 사용자가 활동을 완료하면 내 앱으로 돌아가는 대신 홈 화면으로 빠져 나옵니다.Phonegap Cordova 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

메모리가 부족합니까? 이 경우에도 일반적으로 코르도바로 돌아 오게되지만 국가는 일반적으로 잃어 버리고 활동 결과를 얻을 수 없습니다. 이 동작은 여러 장치에서 일관됩니까? –

+0

안타깝게도 ZTE Optik에서만 테스트 할 수 있습니다. Canvas App은 최신 Android 빌드에서 Nexus 7과 버그가 있음을 나타냅니다. 일식 LogCat 또는 Console에 메모리 문제 메시지가 표시됩니까? 나는 그런 것을 보지 않고있다. – LuckyStrike2021

+0

예, logcat이 표시합니다. 이 작업은 에뮬레이터에서 수행합니까? 나는 에뮬레이터가 ZTE 장치에서 일어나는 일을 신뢰하는 것보다 훨씬 더 많은 것을 믿습니다. 예측할 수없는 행동의 역사가 있습니다. –

답변

0

그래서 앱을 다시로드하는 것은 화면 회전과 관련된 문제였습니다. 매니페스트에 android:configChanges="orientation" 을 추가하기 만하면되었습니다. 불행히도이 옵션은 configChanges 옵션에 여러 값을 가질 수있는 것처럼 모든 사람이이 옵션을 나열하지 않은 경우 더 빨리 해결되었을 것입니다.

이 오류는 매번 발생합니다. android:configChanges="orientation|keyboard"

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

은 .. 당신의 문제에 대한 답을보고 있지만, 폰갭으로 연결해야 네이티브 안드로이드들 중 일부는 당신이 인 당신의 활동이 플러그에 돌아올 얻을 수있는 방법을 알고 할 수 있습니다 당신이 startActivityForResult() 호출을하기 전에

this.cordova.setActivityResultCallback(MyActivity.this); 

가 호출되어 있는지 확인하십시오. 아무도 대답하지 않은 것 같아서 정말 괴롭 혔습니다.

+0

이것은 Cordova 3.1 및 사용자 정의 플러그인과 함께 저에게 효과적이었습니다. –

관련 문제