简体   繁体   English

返回意图始终作为 resultCode 0 接收,并且 Intent 数据为 null

[英]Returning intent always received as resultCode 0 and Intent data is null

I have two apps that I trying to get to talk to each other.我有两个应用程序,我试图互相交谈。 App 1 is an old app coded in Java in Android Studio App 2 has been written in Xamarin.Forms but currently only coded for Android App 1 is an old app coded in Java in Android Studio App 2 has been written in Xamarin.Forms but currently only coded for Android

App 1 contains information that it is trying to change with information from App 2. App 1 calls app 2 via the following function:应用 1 包含它尝试使用来自应用 2 的信息更改的信息。应用 1 通过以下 function 调用应用 2:

private void launchApp(String packageName) {
    
    Intent launchIntent = getPackageManager().getLaunchIntentForPackage(packageName);
    
    String locationID = mCurrentJob.locationDetailDto.location;
    String taskID = String.valueOf(mCurrentJob.id);
    String userID = String.valueOf(Global.myUser.id);
    String deviceID = Urls.DEVICE_ID;

    if (launchIntent != null) {
        try {
            launchIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
            launchIntent.putExtra("LocationID", locationID);
            launchIntent.putExtra("TaskID", taskID);
            launchIntent.putExtra("UserID", userID);
            launchIntent.putExtra("DeviceID", deviceID);
            launchIntent.setFlags(0);
            launchIntent.setType("text/plain");
            startActivityForResult(launchIntent, LAUNCH_APP);
        } catch (ActivityNotFoundException error) {
            TelemetryLog.error(TAG, Statics.ERROR_MESSAGE_TRY_CATCH_BLOCK + error, error);
        }
    } else {
        TelemetryLog.debug(TAG, "[launchApp][Error]");
    }
}

Here is the Receiver code in the same file/activity:这是同一文件/活动中的接收器代码:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // COMPLETION ACTIVITY
    
    if (resultCode == RESULT_OK) {
        switch (requestCode) {
            case LAUNCH_APP:
                String intentAssetID = Util.clean(data.getStringExtra("ID"));
                String intentAssetDescription = Util.clean(data.getStringExtra("Description"));
                String assetBarCode = Util.clean(data.getStringExtra("Barcode"));
                String assetNumberRef = Util.clean(data.getStringExtra("AssetNumber"));
                TelemetryLog.debug(TAG, "[onActivityResult]" + intentAssetID);
                TelemetryLog.debug(TAG, "[onActivityResult]" + intentAssetDescription);
                //If there is not (id + desc) and no barcode and no assetNumberRef
                if ((TextUtils.isEmpty(intentAssetID) || TextUtils.isEmpty(intentAssetDescription)) && TextUtils.isEmpty(assetBarCode) && TextUtils.isEmpty(assetNumberRef)) {
                    Toast.makeText(mContext, mContext.getString(R.string.completion_activity_no_asset_from_app), Toast.LENGTH_LONG).show();
                } else if (isPlanned) {
                    Toast.makeText(mContext, "Asset cannot be changes on a PPM job.", Toast.LENGTH_LONG).show();
                } else {
                    try {
                        if (!TextUtils.isEmpty(intentAssetID)) {
                            long id = Long.parseLong(intentAssetID);
                            mAssetUpdateRequired = true;
                            mNewAssetId = id;
                            mNewAssetDesc = intentAssetDescription;
                            showFieldsForNewAsset();
                        } else {
                            //The Asset app did not return an asset ID - it may have returned a barcode or reference
                            if (!TextUtils.isEmpty(assetBarCode)) {
                                mAssetUpdateRequired = true;
                                mNewAssetId = -1;
                                mAssetBarCode = assetBarCode;
                                mNewAssetDesc = "Barcode: " + assetBarCode; //Use asset barcode for displayed description
                                showFieldsForNewAsset();
                            } else if (!TextUtils.isEmpty(assetNumberRef)) {
                                mAssetUpdateRequired = true;
                                mNewAssetId = -1;
                                mAssetNumRef = assetNumberRef;
                                mNewAssetDesc = "Asset Number: " + assetNumberRef; //Use asset number ref for displayed description
                                showFieldsForNewAsset();
                            }
                        }
                        
                    } catch (Exception e) {
                        Toast.makeText(mContext, mContext.getString(R.string.completion_activity_no_asset_from_app), Toast.LENGTH_LONG).show();
                        TelemetryLog.error(TAG, "[onActivityResult][LAUNCH_APP] " + Statics.ERROR_MESSAGE_TRY_CATCH_BLOCK + e, e);
                    }
                }
                break;
            default:
                super.onActivityResult(requestCode, resultCode, data);
                break;
        }
    }
}

On App 2 the information is received correctly, a list of options is available, one is selected and upon button pressed, the selected item is converted into the required formatted model.在 App 2 上正确接收信息,提供选项列表,选择一个,按下按钮后,所选项目将转换为所需的格式 model。 I then use MessagingCenter to pass it from the Forms side to the Android side and this is where it is received and calls back to App 1:然后我使用 MessagingCenter 将它从 Forms 端传递到 Android 端,这是接收它并回调到 App 1 的地方:

protected override void OnStart()
    {
        base.OnStart();
        // App should return here after Submitting an Asset from the DetailsPage
        Xamarin.Forms.MessagingCenter.Subscribe<DetailsPage, SendAsset>(this, "Asset", (sender, arg) =>
        {
            SendAsset asset = arg;
            if (asset != null)
            {
                ReturnToCaller(asset);
            }
        });
    }

public void ReturnToCaller(SendAsset assetDetails)
    {
        Intent returnAsset = new Intent(Intent.ActionSend);

        returnAsset.PutExtra("ID", assetDetails.ID);
        returnAsset.PutExtra("AssetNumber", assetDetails.Code);
        returnAsset.PutExtra("Description", assetDetails.Description);
        returnAsset.PutExtra("Barcode", assetDetails.Barcode);
        returnAsset.PutExtra("Serial", assetDetails.Serial);
        SetResult(Result.Ok, returnAsset);
        Finish();
    }

Now, all this code works fine up until it hits the 'onActivityResult' on App 1. I have debugged through these steps tweaking multiple parts of the intent on both ends (eg removing flags, removing type, specifying type) and I can see that the return intent is as intended.现在,所有这些代码都可以正常工作,直到它到达 App 1 上的“onActivityResult”。我已经通过这些步骤进行了调试,调整了两端意图的多个部分(例如,删除标志、删除类型、指定类型),我可以看到返回意图符合预期。 It is populated with the Extras and is marked with ReultCode.OK (-1) but every single time it gets to App 1 the resultCode is always 0 with a matching requestCode to when App 2 is called.它填充了 Extras 并标有 ReultCode.OK (-1),但每次到达 App 1 时,resultCode 始终为 0,并且在调用 App 2 时具有匹配的 requestCode。

Have I missed something obvious?我错过了什么明显的东西吗? Or is there some deeper routed problem?还是有一些更深层次的路由问题? Any suggestion are very appreciated as I'm very stuck on this, thanks in advance.任何建议都非常感谢,因为我非常坚持这一点,在此先感谢。

Note: I have cleaned up the code to fit in here and removed the pieces that are not relevant or just plain commented out and changed some naes so that the code remains anonymous.注意:我已经清理了代码以适合此处,并删除了不相关的部分或只是简单地注释掉并更改了一些 naes,以便代码保持匿名。

Update: So I am still having some troubles with this, but I have found that the reason I was always receiveing resultCode 0 and NULL intent data was because I was calling the SetResult and Finish from MainActivity instead of the SplashScreen which is set as the MainLauncher for the app.更新:所以我仍然遇到一些麻烦,但我发现我总是收到 resultCode 0 和 NULL 意图数据的原因是因为我从 MainActivity 而不是设置为 MainLauncher 的 SplashScreen 调用 SetResult 和 Finish为应用程序。 Now it will return a proper result but only when called during the processing of the SplashScreen.现在它将返回正确的结果,但仅在处理 SplashScreen 期间调用。 If I try to use MessagingCenter to subscribe to the response from the Forms page where I get the details, the message is received twice and doesn't close down the app (therefore not returning to App 1 at all)如果我尝试使用 MessagingCenter 订阅来自获取详细信息的 Forms 页面的响应,则会收到两次消息并且不会关闭应用程序(因此根本不会返回到应用程序 1)

I am trying to find the right way to Subscribe to this MessagingCenter but it seems that no matter where I put the Subscribe and Unsubscribe in the apps lifecycle, it will either call twice or not at all.我正在尝试找到订阅此 MessagingCenter 的正确方法,但似乎无论我将订阅和取消订阅放在应用程序生命周期中的哪个位置,它都会调用两次或根本不调用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 onActivityResult中的空意图(int requestCode,int resultCode,Intent data) - Null intent in onActivityResult(int requestCode, int resultCode, Intent data) Android 意图:总是得到 resultCode = 0 - Android Intent : Always get resultCode = 0 片段startActivityForResult始终在回调onActivityResult上返回resultCode 0和intent null - Fragments startActivityForResult always return resultCode 0 and intent null on callback onActivityResult Android拍照int resultCode = -1,意图数据= null? - Android take photos int resultCode = -1, Intent data = null? 相机意图 resultCode 总是返回取消的牛轧糖 - Camera intent resultCode always return cancelled for Nougat 三星S4中的onActivityResult(int requestCode,int resultCode,Intent data)中的相机意图数据为null - camera intent data null in onActivityResult(int requestCode, int resultCode, Intent data) in Samsung S4 当我进行Facebook登录时,onActivityResult(int requestCode,int resultCode,Intent data)中的数据意图等于null - data intent equal null in onActivityResult(int requestCode, int resultCode, Intent data) when i make facebook login 三星S3中的onActivityResult(int requestCode,int resultCode,Intent data)中的相机意图数据为null - camera intent data null in onActivityResult(int requestCode, int resultCode, Intent data) in Samsung S3 onActivityResult()Intent数据始终为null - onActivityResult() Intent data is always null 位图在相机意图中始终返回null - Bitmap always returning null in camera intent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM