Android从通知中获取gmail电子邮件主题

时间:2015-08-03 21:03:38

标签: android email notifications gmail listener

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

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

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

非常感谢。

1 个答案:

答案 0 :(得分:0)

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

    @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设置中停用该应用 - &gt;声音&amp;通知 - &gt;更新应用程序之前的通知访问权限。然后,您可以在安装软件包更新后重新启用它。

希望有所帮助。

相关问题