我正在尝试使用 ComposeActivityGmail 发送电子邮件。我尝试了 3 种方法。第一个是最简单的Intent.setClassName:
sendIntent.setComponent( new ComponentName( "com.google.android.gm", "ComposeActivityGmail" ) );
有了这个我有以下例外:
java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.VIEW dat=myemail@gmail.com cmp=com.google.android.gm/.ComposeActivityGmail (has extras) } from ProcessRecord{30a898b6 10871:org.madebyalex.myperiod/u0a255} (pid=10871, uid=10255) not exported from uid 10067
at android.os.Parcel.readException(Parcel.java:1546)
at android.os.Parcel.readException(Parcel.java:1499)
at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:2468)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1563)
at android.app.Activity.startActivityForResult(Activity.java:3755)
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:48)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:77)
at android.app.Activity.startActivityForResult(Activity.java:3716)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859)
at android.app.Activity.startActivity(Activity.java:4036)
at android.app.Activity.startActivity(Activity.java:3998)
at org.madebyalex.myperiod.EmailMessage.sendMessage(EmailMessage.java:93)
at org.madebyalex.myperiod.SecurityFragment$2.onClick(SecurityFragment.java:159)
at android.view.View.performClick(View.java:4785)
at android.view.View$PerformClick.run(View.java:19884)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
第二种方法是基于使用 PackageManager的这个问题:
Intent sendIntent = new Intent( Intent.ACTION_VIEW );
sendIntent.setType( "plain/text" );
sendIntent.setData( Uri.parse( receivers.get( 0 ) ) );
final PackageManager packageManager = context.getPackageManager();
final List<ResolveInfo> matches = packageManager.queryIntentActivities( sendIntent, 0 );
String className = "";
for( ResolveInfo info : matches ){
if( info.activityInfo.packageName.equals( "com.google.android.gm" ) ){
className = info.activityInfo.name;
if( className != null && !className.isEmpty() ){
break;
}
}
}
sendIntent.setClassName( "com.google.android.gm", className );
Log.e("ERROR", "CLASS NAME: "+className);
sendIntent.putExtra( Intent.EXTRA_SUBJECT, getSubject() );
sendIntent.putExtra( Intent.EXTRA_TEXT, message+"\n" );
context.startActivity( sendIntent );
使用这种方法会引发以下异常:
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.google.android.gm/ComposeActivityGmail}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1868)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1568)
at android.app.Activity.startActivityForResult(Activity.java:3755)
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:48)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:77)
at android.app.Activity.startActivityForResult(Activity.java:3716)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859)
at android.app.Activity.startActivity(Activity.java:4036)
at android.app.Activity.startActivity(Activity.java:3998)
at org.madebyalex.myperiod.EmailMessage.sendMessage(EmailMessage.java:93)
at org.madebyalex.myperiod.SecurityFragment$2.onClick(SecurityFragment.java:159)
at android.view.View.performClick(View.java:4785)
at android.view.View$PerformClick.run(View.java:19884)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
并使用此代码引发了与上述完全相同的异常:
sendIntent.setComponent( new ComponentName( "com.google.android.gm", "ComposeActivityGmail" ) );
SO上的一些答案说要在Android清单上声明此活动。这样做 AndroidStudio 会抱怨“android.gm/ComposeActivityGmail”部分:
<activity
android:name="com.google.android.gm/ComposeActivityGmail"
android:screenOrientation="locked"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
</intent-filter>
</activity>
在 AndroidManifest.xml AndroidStudio 中插入这个简单的代码会抱怨:未解析的类。
<activity android:name=".ComposeActivityGmail"/>
如果所有这些解决方案都失败了,如何解决这个问题?