startactivityforresult不向第一个应用程序返回任何值

velaa5lx  于 2021-07-11  发布在  Java
关注(0)|答案(0)|浏览(168)

假设我有应用程序a和应用程序b。
在应用程序a中,我启动了一个以这种方式启动应用程序b的意图。

Intent intent = new Intent("com.example.myAppBAction");
Bundle bundle = new Bundle();
bundle.putString("anId", id);
intent.putExtras(bundle);
startActivityForResult(intent, myRequestCode);

然后在应用程序b中,我接收所有的数据,并在完成一些工作后以这种方式返回一个结果。

@Override
public void onBackPressed() {
    super.onBackPressed();
    Intent i = new Intent();
    int codeWorkDone;//this is valued between 2 or 3
    i.putExtra("FirstResult", anInteger);
    i.putExtra("SecondResult", anotherInteger);
    setResult(codeWorkDone, i);
    finish();
}

最终在我的 onActivityResult 我需要打印应用程序b中的所有附加内容,如下所示:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode){
        case myRequestCode:
            StringBuilder result = new StringBuilder();
            result.append(resultCode + "\n");
            if (data != null){
                Bundle b = data.getExtras();
                if (b != null) {
                    for (String key : b.keySet()) {
                        result.append(key + " : " + (b.get(key) != null ? b.get(key) : "NULL"));
                    }
                }
            }
            Log.d("esito", result.toString());
            break;
    }
}

这里的问题是 requestCode 是正确的,但我收到一封回信 resultCode 这是0(请记住,从应用程序b设置为2或3之间),我的数据是空的。
有什么线索吗?
编辑:应用程序a清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.appA"
    android:versionCode="202040"
    android:versionName="2.2.4" >

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.USE_CREDENTIALS" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <application
        android:name=".application.MyAppAApplication"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar"
        >

        <activity
            android:name=".activity.MainActivity"
            android:configChanges="orientation|screenSize"
            android:theme="@style/AppTheme.NoActionBar"
            android:launchMode="singleTask"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name="com.example.TestActivityToLaunchAppB"
            android:label="@string/title_activity"
            android:theme="@style/AppTheme.NoActionBar"
            android:windowSoftInputMode="stateHidden|adjustPan" >

        </activity>

        <meta-data
            android:name="io.fabric.ApiKey"
            android:value="xxx" />
    </application>

</manifest>

应用程序b清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.myApp.appB">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <uses-feature
        android:name="android.hardware.camera"
        android:required="true" />

    <application
        android:name=".MyAppApplication"
        android:allowBackup="false"
        android:hardwareAccelerated="true"
        android:icon="@drawable/xxhdpi"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:roundIcon="@drawable/xxhdpi"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:ignore="GoogleAppIndexingWarning">

        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="com.cdd.android.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>

        <activity
            android:name=".activity.MyLandingActivity"
            android:configChanges="orientation|screenSize"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="com.example.myAppBAction" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题