Android意图检查/查看电子邮件

时间:2017-06-06 17:59:35

标签: android android-intent

我正在寻找一种方法从我的应用程序启动设备上的电子邮件应用程序以查看电子邮件。注意:我不想发送'任何电子邮件,只是想检查'电子邮件。以下代码崩溃了gmail应用程序 -

          Intent intent = new Intent(Intent.ACTION_VIEW);
          intent.setType("message/rfc822");
          startActivity(intent);

堆栈跟踪是 -

06-06 23:26:20.313 27703-27731/? E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
                                                   Process: com.google.android.gm, PID: 27703
                                                   java.lang.RuntimeException: An error occurred while executing doInBackground()
                                                       at android.os.AsyncTask$3.done(AsyncTask.java:309)
                                                       at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
                                                       at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
                                                       at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                                                       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                                                       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                                                       at java.lang.Thread.run(Thread.java:818)
                                                    Caused by: java.lang.NullPointerException: uri
                                                       at com.android.internal.util.Preconditions.checkNotNull(Preconditions.java:60)
                                                       at android.content.ContentResolver.openInputStream(ContentResolver.java:645)
                                                       at clk.a(SourceFile:8)
                                                       at clk.loadInBackground(SourceFile:74)
                                                       at android.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:312)
                                                       at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:69)
                                                       at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:66)
                                                       at android.os.AsyncTask$2.call(AsyncTask.java:295)
                                                       at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
                                                       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
                                                       at java.lang.Thread.run(Thread.java:818) 

为了查看电子邮件,是否无法触发电子邮件应用程序?

4 个答案:

答案 0 :(得分:2)

您可以使用makeMainSelectorActivity类中的Intent方法执行此操作。

Intent intent = Intent.makeMainSelectorActivity(
                     Intent.ACTION_MAIN,
                     Intent.CATEGORY_APP_EMAIL);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(Intent.createChooser(intent, "Email"));

Intent makeMainSelectorActivity(String selectorAction,                 String selectorCategory)

  

为应用程序的主要活动创建一个Intent,而不指定要运行的特定活动,而是让选择器查找活动。这导致最终的Intent结构与从Home启动应用程序时的结构相同。对于想要以相同方式启动应用程序的任何其他内容,重要的是它们以相同的方式使用Intent结构,并且可以使用此函数来确保这种情况。

CATEGORY_APP_EMAIL

答案 1 :(得分:1)

这应该有效:

Intent intent = new Intent(Intent.CATEGORY_APP_EMAIL);
          intent.setType("message/rfc822");
          startActivity(intent);

答案 2 :(得分:0)

尝试使用此代码,这将打开一个包含设备中所有电子邮件应用的对话框。从弹出窗口中选择应用程序,它将打开。

try {
List<String> emailClientNames = new ArrayList<String>();
final List<String> emailClientPackageNames = new ArrayList<String>();
// finding list of email clients that support send email
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
    "mailto", "abc@gmail.com", null));
PackageManager pkgManager = AppController.getContext().getPackageManager();
List<ResolveInfo> packages = pkgManager.queryIntentActivities(intent, 0);
if (!packages.isEmpty()) {
    for (ResolveInfo resolveInfo : packages) {
        // finding the package name
        String packageName = resolveInfo.activityInfo.packageName;
        emailClientNames.add(resolveInfo.loadLabel(getPackageManager()).toString());
        emailClientPackageNames.add(packageName);
    }
    // a selection dialog  for the email clients
    AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this);
    builder.setTitle("Select email client");
    builder.setItems(emailClientNames.toArray(new String[]{}), new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        // on click we launch the right package
        Intent intent = getPackageManager().getLaunchIntentForPackage(emailClientPackageNames.get(which));
            startActivity(intent);
        }
    });
    AlertDialog dialog = builder.create();
    dialog.show();
}
} catch (ActivityNotFoundException e) {
// Show error message
}

但如果您想直接打开默认电子邮件应用,请使用以下代码:

 Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_APP_EMAIL);
startActivity(intent);  
startActivity(Intent.createChooser(intent, getString(R.string.ChoseEmailClient)));

答案 3 :(得分:0)

Commonsware引用

View.OnTouchListener WkaMovLitRitVyuKlkVaf = new View.OnTouchListener()
{
    float WkaStrVyuXcoVar, WkaStrTchXcoVar, WkaStpTchXcoVar;
    final int MovDstVar = LloHytVal * 2;
    RelativeLayout.LayoutParams EdtBtnRulVaj, DelBtnRulVaj;

    @Override
    public boolean onTouch(View VyuPsgVar, MotionEvent MsnEvtPsgVar)
    {
        switch (MsnEvtPsgVar.getActionMasked())
        {
            case MotionEvent.ACTION_DOWN:
                WkaStrTchXcoVar = MsnEvtPsgVar.getRawX();
                WkaStrVyuXcoVar = (int) VyuPsgVar.getX();
                break;

            case MotionEvent.ACTION_MOVE:
                WkaStpTchXcoVar = MsnEvtPsgVar.getRawX();
                float XcoDstVar = Math.abs(WkaStpTchXcoVar - WkaStrTchXcoVar);

                if (XcoDstVar < MovDstVar)
                {
                    EdtBtnRulVaj = (RelativeLayout.LayoutParams) NamEdtBtnVav.getLayoutParams();
                    DelBtnRulVaj = (RelativeLayout.LayoutParams) NamDelBtnVav.getLayoutParams();
                    int SetBtnWytVar;
                    if (WkaStrVyuXcoVar == 0)
                    {
                        VyuPsgVar.animate()
                                .translationX(-XcoDstVar)
                                .setDuration(0)
                                .start();
                        SetBtnWytVar = (int) XcoDstVar / 2;
                    }
                    else
                    {
                        VyuPsgVar.animate()
                                .x(-MovDstVar + XcoDstVar)
                                .setDuration(0)
                                .start();
                        SetBtnWytVar = (int) (LloHytVal - XcoDstVar / 2);
                    }
                    DelBtnRulVaj.width = SetBtnWytVar;
                    EdtBtnRulVaj.width = SetBtnWytVar;
                    NamEdtBtnVav.setLayoutParams(EdtBtnRulVaj);
                    NamDelBtnVav.setLayoutParams(DelBtnRulVaj);
                }
                break;

            case MotionEvent.ACTION_UP :
            case MotionEvent.ACTION_CANCEL:
                EdtBtnRulVaj = (RelativeLayout.LayoutParams) NamEdtBtnVav.getLayoutParams();
                DelBtnRulVaj = (RelativeLayout.LayoutParams) NamDelBtnVav.getLayoutParams();
                int SetBtnWytVar = 0;
                if (WkaStrVyuXcoVar == 0 && WkaStpTchXcoVar < WkaStrTchXcoVar)
                {
                    VyuPsgVar.animate()
                            .x(-MovDstVar);
                    SetBtnWytVar = LloHytVal;
                }
                else if (WkaStrVyuXcoVar == -MovDstVar)
                {
                    VyuPsgVar.animate()
                            .x(0);
                    SetBtnWytVar = 0;
                }
                DelBtnRulVaj.width = SetBtnWytVar;
                EdtBtnRulVaj.width = SetBtnWytVar;
                NamEdtBtnVav.setLayoutParams(EdtBtnRulVaj);
                NamDelBtnVav.setLayoutParams(DelBtnRulVaj);
                break;
        }
        return true;
    }
};