当Email未设置账户时,通过ACTION_SENDTO启动会报ActivityNotFoundException

当Email未设置账户时,通过ACTION_SENDTO启动会报ActivityNotFoundException

手机里已经安装了email的apk,检查manitfest中也有对应的action,并且activity也没有被disable掉,为何就是找不到呢?
<activity
            android:name="com.android.email.activity.ComposeActivityEmail"
            android:label="@string/app_name"
            android:documentLaunchMode="intoExisting"
            android:autoRemoveFromRecents="true"
            android:theme="@style/ComposeTheme">
            <intent-filter>
                <action
                    android:name="android.intent.action.VIEW" />
                <action
                    android:name="android.intent.action.SENDTO" />
                <data
                    android:scheme="mailto" />
                <category
                    android:name="android.intent.category.DEFAULT" />
                <category
                    android:name="android.intent.category.BROWSABLE" />
            </intent-filter>

1.我下载了一个QQ邮箱安装到手机,发现ACTION_SENDTO启动QQ邮箱的设置界面
2.我把QQ邮箱卸载了,在原生Email里面登录一个账号,再ACTION_SENDTO可以启动对应的ComposeActivityEmail
3.那就是没有账号不能启动ComposeActivityEmail
4.ActivityStarter中startActivityMayWait的ResolveInfo rInfo = mSupervisor.resolveIntent(intent, resolvedType, userId);
和 ActivityInfo aInfo = mSupervisor.resolveActivity(intent, rInfo, startFlags, profilerInfo);当有账号rInfo 和aInfo 都不为null,当没有账号返回的是null.

5.通过分析得知ComposeActivityEmail是动态被enable和disable的,所以搜索setComponentEnabledSetting,找到了关键代码
http://androidxref.com/7.1.1_r6/xref/packages/apps/Email/provider_src/com/android/email/provider/AccountReconciler.java
 if (!TextUtils.isEmpty(composeActivityName)) {
            // If there are no accounts remaining after reconciliation, disable the compose activity
            final boolean enableCompose = emailProviderAccounts.size() - accountsDeleted > 0;
            final ComponentName componentName = new ComponentName(context, composeActivityName);
            context.getPackageManager().setComponentEnabledSetting(componentName,
                    enableCompose ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED :
                            PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                    PackageManager.DONT_KILL_APP);
            LogUtils.d(LogUtils.TAG, "Setting compose activity to "
                    + (enableCompose ? "enabled" : "disabled"));
        }


将这部分代码拿掉没有账号也可以启动email来处理sendto

具体为何google要这样做,目前还没有找到其理由。

猜你喜欢

转载自aijiawang-126-com.iteye.com/blog/2394737