Android通知:打开应用程序时不显示通知吗?

编程入门 行业动态 更新时间:2024-10-27 04:26:30
本文介绍了Android通知:打开应用程序时不显示通知吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在一个项目上,并且我将Intent Service与wakefulBroadcastReceiver一起使用来安排警报并发送通知.到目前为止没有任何问题.但是我想在打开应用程序时不再显示通知.对这个问题有任何想法吗?.

I'm working on a project and I use Intent Service with wakefulBroadcastReceiver to schedule alarm and send notification. There are no problems this far. But I want when I open my app, it don't displaying notification again. Please any idea for this problem?.

推荐答案

在通知接收器中,当您收到通知时,您可以检查应用是否在后台,并做出相应显示通知的决定.

In your notification receiver when you receive the notification you can check whether the app is in background or not and take a decision of showing the notification accordingly.

这里是检查应用程序是否在后台的代码.

Here's a code to check whether the app is in background or not.

public class MyGcmPushReceiver extends GcmListenerService { /** * Called when message is received. * @param from SenderID of the sender. * @param bundle Data bundle containing message data as key/value pairs. * For Set of keys use data.keySet(). */ @Override public void onMessageReceived(String from, Bundle bundle) { // Check here whether the app is in background or running. if(isAppIsInBackground(getApplicationContext())) { // Show the notification } else { // Don't show notification } } /** * Method checks if the app is in background or not */ private boolean isAppIsInBackground(Context context) { boolean isInBackground = true; ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) { List<ActivityManager.RunningAppProcessInfo> runningProcesses = am.getRunningAppProcesses(); for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) { if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { for (String activeProcess : processInfo.pkgList) { if (activeProcess.equals(context.getPackageName())) { isInBackground = false; } } } } } else { List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1); ComponentName componentInfo = taskInfo.get(0).topActivity; if (componentInfo.getPackageName().equals(context.getPackageName())) { isInBackground = false; } } return isInBackground; } }

更多推荐

Android通知:打开应用程序时不显示通知吗?

本文发布于:2023-11-27 03:22:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636363.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:通知   应用程序   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!