当Android中有传入的电子邮件时,我们可以检测到通知吗?
我可以尝试任何解决方案,教程或示例代码吗?
由于
答案 0 :(得分:1)
尝试实现NotificationListenerService。 这是官方文档https://developer.android.com/reference/android/service/notification/NotificationListenerService.html
答案 1 :(得分:1)
你应该实现广播接收器并听“android.intent.action.PROVIDER_CHANGED”意图
<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>
答案 2 :(得分:1)
gor的回答有效(我编辑了一下)!!!谢谢。
然后将其添加到您的清单中。 这是我用来向我的应用添加通知的接收器类:
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);
}
}
答案 3 :(得分:0)
答案 4 :(得分:0)
我认为您正在搜索BroadcastReceiver(仅当您自己管理电子邮件而且它不是第三方电子邮件时。在这种情况下,您可能无能为力):
http://www.vogella.com/tutorials/AndroidBroadcastReceiver/article.html