ActivityManager.getRunningTasks 已弃用 android [英] ActivityManager.getRunningTasks is deprecated android

查看:213
本文介绍了ActivityManager.getRunningTasks 已弃用 android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working on push notification in android where i am using a below method to show notification, but the problem is that now ActivityManager.getRunningTasks(1); is being deprecated. From one stackoverflow question i read that: " you can use getAppTasks() returning a List<AppTask> where you can get the RecentTaskInfo with getTaskInfo" but i can't figure it out how to use it. Please help me out here in this regard.

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());
        }
    }

推荐答案

这应该可以帮助您入门

ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.AppTask> tasks = activityManager.getAppTasks();

for (ActivityManager.AppTask task : tasks) {
    Log.d(TAG, "stackId: " + task.getTaskInfo().stackId);
}

这篇关于ActivityManager.getRunningTasks 已弃用 android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆