【问题标题】:Android get gmail email subject from notificationAndroid从通知中获取gmail电子邮件主题
【发布时间】:2015-08-03 21:03:38
【问题描述】:

我想从 android 通知中读取新收到的电子邮件 (gmail) 的主题。

我认为我必须使用在后台运行的 NotificationListenerService,实时捕捉新电子邮件的通知,并从中读取主题。

但是我如何阅读主题或正文的一部分?有可能吗?

非常感谢。

【问题讨论】:

  • 尝试让某些东西正常工作,如果您有特定问题,请发布有关该问题的问题。
  • 有可能,查看通知大内容。

标签: android email notifications gmail listener


【解决方案1】:

这是一个捕获通知并将其放入文本文件的示例。我这样做只是为了开发目的,所以我可以看到每个应用程序如何填充通知,但它应该向您展示如何做到这一点。

    @Override
public void onNotificationPosted(StatusBarNotification sbn) {
    super.onNotificationPosted(sbn);

    new Store().execute(sbn);
}

private class Store extends AsyncTask<StatusBarNotification, Integer, Long> {

    @Override
    protected Long doInBackground(StatusBarNotification... params) {

        String temp = "";

        StatusBarNotification sbn = params[0];

        Notification not = sbn.getNotification();
        String pack = sbn.getPackageName();

        // dump all the android notifications
        if (pack.startsWith("com.android")) return null;
        if (pack.startsWith("com.estrongs")) return null;
        if (pack.startsWith("com.motorola")) return null;

        count++;
        if (not.category != null)
            temp += "Category: " + not.category.toString() + "\n";
        else
            temp += "Category = null \n";

        if (not.tickerText != null)
            temp += "TickerText: " + not.tickerText.toString() + "\n";
        else
            temp += "TickerText = null \n";

        temp += "Key: " + sbn.getKey() + "\n" + "ID: " + sbn.getId() + "\n";

        SimpleDateFormat format = new SimpleDateFormat("DD-kk:mm:ss:SSS");
        Long ptime = sbn.getPostTime();
        temp += "Post time: " + format.format(ptime) + "\n";
        Long nottime = not.when;
        temp += "When: " + format.format(nottime) + "\n";

        temp += "Extras..." + "\n";
        Bundle bun = not.extras;
        if (bun.getString(Notification.EXTRA_BIG_TEXT) != null)
            temp += "BigText: " + bun.getString(Notification.EXTRA_BIG_TEXT).toString() + "\n";
        else
            temp += "BigText = null \n";

        if (bun.getString(Notification.EXTRA_SUMMARY_TEXT) != null)
            temp += "SummaryText: " + bun.getString(Notification.EXTRA_SUMMARY_TEXT).toString() + "\n";
        else
            temp += "SummaryText = null \n";

        if (bun.getString(Notification.EXTRA_INFO_TEXT) != null)
            temp += "InfoText: " + bun.getString(Notification.EXTRA_INFO_TEXT).toString() + "\n";
        else
            temp += "InfoText = null \n";

        if (bun.getString(Notification.EXTRA_TEXT) != null)
            temp += "Text: " + bun.getString(Notification.EXTRA_TEXT).toString() + "\n";
        else
            temp += "Text = null \n";

        if (bun.getString(Notification.EXTRA_SUB_TEXT) != null)
            temp += "SubText: " + bun.getString(Notification.EXTRA_SUB_TEXT).toString() + "\n";
        else
            temp += "SubText = null \n";

        if (bun.getString(Notification.EXTRA_TITLE) != null)
            temp += "Title:" + bun.getString(Notification.EXTRA_TITLE).toString() + "\n";
        else
            temp += "Title = null \n";

        if (bun.getString(Notification.EXTRA_TITLE_BIG) != null)
            temp += "Big Title:" + bun.getString(Notification.EXTRA_TITLE_BIG).toString() + "\n";
        else
            temp += "Big Title = null \n";

        temp += "Fields... \n";

        CharSequence[] lines = bun.getCharSequenceArray(Notification.EXTRA_TEXT_LINES);
        if (lines != null) {
            for (CharSequence line : lines) {
                temp += "line: " + line.toString() + "  \n";
            }
        } else {
            temp += " no lines... \n";
        }

        file = new File(save, pack + count + ".txt");
        try {
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(temp.getBytes());
            fos.close();
            //Toast.makeText(this, "File written", Toast.LENGTH_LONG).show();
        } catch (FileNotFoundException e) {
            //Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
            //Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
        }
        return null;
    }
}

请注意,我将 SBN 发送到后台线程。听众不喜欢其线程中的任何内容。此外,调试器的侦听器可能很棘手。我发现您需要在更新应用程序之前在 Android 设置 -> 声音和通知 -> 通知访问中禁用该应用程序。然后,您可以在安装软件包更新后重新启用它。

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 2019-08-04
    • 2012-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    相关资源
    最近更新 更多