ActivityManager.getRunningTasks 已弃用 android

2024-05-06

我正在 android 中处理推送通知,我使用下面的方法来显示通知,但问题是现在 ActivityManager.getRunningTasks(1);正在被弃用。从一个 stackoverflow 问题中我读到:“你可以使用getAppTasks()返回一个List<AppTask>你可以在哪里得到RecentTaskInfo with getTaskInfo“但我不知道如何使用它。请在这方面帮助我。

private void postNotification(Context ctx, Intent intent) {
        try {
            if (intent == null) {
                Log.d(TAG, "Receiver intent null");
            } else {
                String action = intent.getAction();
                if (action.equals("com.ziza.cy.UPDATE_STATUS")) {

                    JSONObject json = new JSONObject(intent.getExtras()
                            .getString("com.parse.Data"));

                    String type = json.getString("type");

                    if (type.equals("AlertNotification")) {

                        String msg = json.getString("header");
                        String title = "ncy";

                        ActivityManager am = (ActivityManager) ctx
                                .getSystemService(Context.ACTIVITY_SERVICE);
                        List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
                        ComponentName componentInfo = taskInfo.get(0).topActivity;

                        // don't show notification if app is in foreground
                        if (componentInfo.getPackageName().equalsIgnoreCase(
                                ctx.getPackageName())) {

                            // send broadcast receiver
                            Intent intentBroad = new Intent();
                            intentBroad.setAction(Constants.sNOTIFICATION);
                            intentBroad.putExtra("msg", msg);
                            intentBroad.putExtra("title", title
                                    + " "
                                    + taskInfo.get(0).topActivity.getClass()
                                            .getSimpleName());
                            ctx.sendBroadcast(intentBroad);
                        } else {
                            // Activity Not Running
                            // Generate Notification

                            Intent intnt = new Intent(ctx, LogInActivity.class);
                            PendingIntent contentIntent = PendingIntent
                                    .getActivity(ctx, 0, intnt, 0);
                            intnt.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                                    | Intent.FLAG_ACTIVITY_NEW_TASK);

                            NotificationCompat.Builder builder = new NotificationCompat.Builder(
                                    ctx)
                                    .setContentTitle(title)
                                    .setContentText(msg)
                                    .setTicker(msg)
                                    .setSound(
                                            RingtoneManager
                                                    .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                                    .setStyle(
                                            new NotificationCompat.BigTextStyle()
                                                    .bigText(msg))
                                    .setAutoCancel(true)
                                    .setSmallIcon(R.drawable.iconed)
                                    .setOnlyAlertOnce(true)
                                    .setDefaults(Notification.DEFAULT_VIBRATE);

                            Uri alarmSound = RingtoneManager
                                    .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                            builder.setSound(alarmSound);
                            builder.setContentIntent(contentIntent);
                            NotificationManager notificationManager = (NotificationManager) ctx
                                    .getSystemService(Context.NOTIFICATION_SERVICE);

                            notificationManager.notify(0, builder.build());

                        }
                    }
                }
            }

        } catch (JSONException e) {
            Log.d(TAG, "JSONException: " + e.getMessage());
        }
    }

这应该适用于 Lollipop 之前的设备以及 Lollipop 设备

public static boolean isBackgroundRunning(Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        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())) {
                        //If your app is the process in foreground, then it's not in running in background
                        return false;
                    }
                }
            }
        }


        return true;
    }

编辑:如果应用程序在后台,它应该返回 true,而不是相反

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ActivityManager.getRunningTasks 已弃用 android 的相关文章

随机推荐

  • 如何隐藏组件而不卸载它?

    我有一个列表组件 我希望在不需要时将其保留在 DOM 中 以便滚动位置不会丢失 我尝试将样式设置为 display none 但这会卸载该组件 我也尝试过设置 flex 0 0001 哪种有效 但感觉像是一种黑客攻击 他们稍后可能会优化为
  • 无法写入文本文件

    我正在运行一些测试并需要写入文件 当我运行测试时open file r 不写入文件 测试脚本如下 class GetDetailsIP TestGet def runTest self self category PTZ try This
  • C++ 中跨多个文件的类

    我几乎 100 确定我在这两个类中的语法都是正确的 但是我收到以下错误 对于 CShape cpp 错误 C2011 CShape class 类型重新定义 对于 CCircle cpp 错误 CS2504 CShape 基类未定义 这是
  • 在 CSS 中设置 TableView 样式 (JavaFX)

    如何在 TableView 中设置 THIS 点的样式 我的 CSS 代码如下所示 Empty Stylesheet file root fx background color 262626 table view fx background
  • MSTest:如何增加测试时间

    我有一个测试需要运行超过 1 分钟 VS2008 MSTest 测试是从 VisualStudio 启动的 const int TestTimeout 1 TestMethod Timeout 10 60 1000 10 minutes p
  • 在 htaccess 中设置内存限制 [关闭]

    Closed 这个问题是无关 help closed questions 目前不接受答案 我正在使用 WordPress 我需要增加内存 因此我将以下行添加到我的 htaccess 文件中 BEGIN WordPress
  • 使用 gcc 的中间 GIMPLE 格式

    根据本文 http en wikipedia org wiki Intermediate languagegcc 在生成代码之前使用多种中间格式 我读到 GIMPLE 格式使用三个地址代码 这似乎是最容易使用的中间语言 但我需要更多细节 因
  • 从管理页面删除 Django 中的 ManyToManyField 并单击按钮

    我有两个模型如下 模型 py from django db import models from django contrib auth models import User class Skill models Model skill m
  • 通过SSIS动态创建Excel表

    在 SSIS 中使用执行 SQL 任务 创建的 Excel 文件 包含多个列和不同的数据类型 这里的问题是 INT Money 列在 Excel 中显示为文本列 即使 Excel 表是使用 Int Money 数据类型创建的 我尝试过使用双
  • UITextField 清除按钮不显示

    I want to show a clear button always in a UITextField I have tried to both set this in xib Interface Builder and in code
  • 如何在警报或确认框中显示粗体文本?

    如何获取粗体文本并更改警报或确认框中文本的颜色 var conFrm confirm Following List s already Sent n strong List Name strong ss replace n b Name b
  • 如何使用 Jackson 重命名 JSON 序列化中的根密钥

    我正在使用 Jackson 对对象列表进行 JSON 序列化 这是我得到的 ArrayList id 1 name test name 但我想要这个 rootname id 1 name test name ie showing the s
  • 将两个 .less 文件合并为一个

    假设我少了两个文件 first less a b font size 13px color lime and second less import first less a font family sans serif 我想要一种将它们组合
  • 您知道 PAC 设计模式的任何示例吗?

    任何人都可以指出正在使用的任何网站或网络应用程序表现 抽象 控制 http www dossier andreas net software architecture pac html设计模式而不是MVC 像这样的桌面应用程序很容易找到 例
  • 如何在android中的地图触摸位置上动态绘制形状

    我想绘制一个形状 无论用户在哪里触摸地图 都应该动态绘制 我对绘制这些形状没有任何想法 我怎样才能做到这一点 我可以通过画布完成这个工作吗 我正在使用 MapFragment 请看图片 请给我任何想法 任何帮助表示赞赏 您可以通过添加一个来
  • 在 yii2 gridview 中输入时进行过滤

    我需要在您键入时过滤网格视图 默认情况下 仅当按下回车键时才进行过滤 您应该使用自己的 js 来处理这个问题 例如 this gt registerJs body on keyup yiiGridView grid id filters i
  • Objective C 中如何对数组进行排序? [复制]

    这个问题在这里已经有答案了 可能的重复 如何在 iPhone 中对包含数字作为字符串的数组进行排序 https stackoverflow com questions 6097119 how to sort array having num
  • 在本地主机中保存更改后,React 组件不会在浏览器中重新加载

    我使用 Create React App 创建了一个新的 React 项目 在旧项目中 每当我在组件中进行更改并保存组件时 它都会反映在浏览器中 但在新项目中 当我保存代码中的更改时 浏览器不会重新加载 也不会反映更改 所以我停止了正在运行
  • Nginx反向代理返回404

    我的 Nginx 安装并运行 下面是配置 etc nginx nginx conf 我要全部转发 api 到我的 tomcat 服务器 该服务器在同一服务器上的端口 9100 上运行 类型http myhost 9100 api apps有
  • ActivityManager.getRunningTasks 已弃用 android

    我正在 android 中处理推送通知 我使用下面的方法来显示通知 但问题是现在 ActivityManager getRunningTasks 1 正在被弃用 从一个 stackoverflow 问题中我读到 你可以使用getAppTas