Detect incoming email notification in android

2020-04-18 08:51发布

Can we detect a notification when there is incoming email in android ?

is there any solution, tutorial, or sample code I can try?

Thanks

5条回答
爷、活的狠高调
2楼-- · 2020-04-18 09:04

you should implement a broadcast receiver and listen to "android.intent.action.PROVIDER_CHANGED" intent

<receiver android:name="GmailReceiver">
        <intent-filter>
            <action android:name="android.intent.action.PROVIDER_CHANGED"
                android:priority="-10">
            </action>
            <action android:name="android.intent.action.VIEW" />
            <data android:scheme="content" android:host="gmail-ls"
                android:pathPattern="/unread/.*">
            </data>
        </intent-filter>
    </receiver>
查看更多
该账号已被封号
3楼-- · 2020-04-18 09:07

gor's answer worked (i edited it a bit)!!! thanks.

So add that to your manifest. And here is the receiver class I used to add notification to my app:

public class GmailReceiver extends BroadcastReceiver{

Context cntxt;

@Override
public void onReceive(Context context, Intent intent) {
    Toast.makeText(context, "Email Received", Toast.LENGTH_LONG).show();
   showNotification(context);
}

private void showNotification(Context context) {

    Intent notificationIntent = new Intent(context, YOUR_ACTIVITY_HERE.class);
    PendingIntent contentIntent = PendingIntent.getActivity(context,
            0, notificationIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);

    NotificationManager nm = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    Resources res = context.getResources();
    Notification.Builder builder = new Notification.Builder(context);

    builder.setContentIntent(contentIntent)
            .setSmallIcon(R.drawable.YOUR_APP_icon)
            .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.YOUR_APP_icon))
            .setTicker(res.getString(R.string.app_name))
            .setWhen(System.currentTimeMillis())
            .setAutoCancel(true)
            .setContentTitle(res.getString(R.string.app_name))
            .setContentText(res.getString(R.string.app_name));
    Notification n = builder.getNotification();
    nm.notify(1, n);
}
} 
查看更多
对你真心纯属浪费
4楼-- · 2020-04-18 09:12

You want below api Jelly bean you should use the accessibility service

reffer the following class

accessibility class

查看更多
Rolldiameter
5楼-- · 2020-04-18 09:19

I think that you searching for BroadcastReceiver (only if you manage the email by yourself and it is not a third-party email. In this case, probably you can do nothing):

http://www.vogella.com/tutorials/AndroidBroadcastReceiver/article.html

查看更多
再贱就再见
6楼-- · 2020-04-18 09:21

Try to implements NotificationListenerService. here is the official documentation https://developer.android.com/reference/android/service/notification/NotificationListenerService.html

and you can take a look to this question NotificationListenerService Implementation

查看更多
登录 后发表回答