0

ComposeActivityGmailを使用して電子メールを送信しようとしています。私は3つのアプローチを試みました。電子メールを送信しようとすると、ActivityNotFoundExceptionまたはPermissionDenialが発生する

java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.VIEW [email protected] 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を使用してthis questionに基づいています:これにより

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); 

私は次の例外を持っている。これにより

sendIntent.setComponent(new ComponentName("com.google.android.gm", "ComposeActivityGmail")); 

:最初のものは、最も簡単なIntent.setClassNameです次の例外が発生します。

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" の部分を文句を言う:AndroidManifest.xmlをAndroidStudioで、この単純なコードを挿入

<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> 

は文句:未解決のクラスを。

<activity android:name=".ComposeActivityGmail"/> 

これらのソリューションがすべて失敗した場合の解決方法

+0

この問題は解決しましたか? – Nivedh

+0

@Nivedhはい、提案されている解決策を使用していません。私はIntent.createChooserを使用しました – learner

+0

だからセレクタが正しく来るでしょうか?それは直接Gmailに直接直接行くつもりはありません。 – Nivedh

答えて

0

android.content.ActivityNotFoundException:明示的なアクティビティクラス{com.google.android.gm/ComposeActivityGmail}を見つけることができません。 あなたはAndroidManifest.xmlでこのアクティビティを宣言していますか?

はマニフェストにあなたの活動を宣言し、これを試してみてください。この

<manifest xlmns:android...> 
... 
<uses-permission android:name="android.permission.INTERNET" /> 
<application ... 

<activity android:name=".ComposeActivityGmail"/> 

</manifest> 
+0

このクラスは未解決です。私はこれを追加することを忘れました。 – learner

0

のように、インターネットのアクセス許可を追加します。わたしにはできる。

Intent emailIntent = new Intent(Intent.ACTION_SEND); 
     emailIntent.setType("text/html"); 
     emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { Constants.CONTACT_EMAIL_ADDRESS }); 
     final PackageManager pm = this.getPackageManager(); 
     final List<ResolveInfo> matches = pm.queryIntentActivities(emailIntent, 0); 
     String className = null; 
     for (final ResolveInfo info : matches) { 
      if (info.activityInfo.packageName.equals("com.google.android.gm")) { 
       className = info.activityInfo.name; 

       if(className != null && !className.isEmpty()){ 
        break; 
       } 
      } 
     } 

     emailIntent.setClassName("com.google.android.gm", className); 

     try { 
      startActivity(emailIntent); 
     } catch(ActivityNotFoundException ex) { 
      // handle error 
     } 
+0

このコードは、私が示したコードを少し修正したものです。 – learner

+0

私もあなたと同じ例外を受け取りましたが、私のスニペットを使用するとうまくいきました。上記のスニペットを試してもうまくいかない場合は、親切に言及してください。 – proy31

0

gmailの新しいアップデートが行われた後、Googleは作成アクティビティ名を変更しました。静的な値を与える代わりに、以下のコードを使用して問題を解決してください。

Intent emailIntent = new Intent(Intent.ACTION_SEND); 
     emailIntent.setType("text/html"); 
     final PackageManager pm = this.getPackageManager(); 
     final List<ResolveInfo> matches = pm.queryIntentActivities(emailIntent, 0); 
     String className = null; 
     for (final ResolveInfo info : matches) { 
      if (info.activityInfo.packageName.equals("com.google.android.gm")) { 
       className = info.activityInfo.name; 

       if(className != null && !className.isEmpty()){ 
        break; 
       } 
      } 
     } 
     emailIntent.setClassName("com.google.android.gm", className);