Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActivityManager.getRunningTasks is deprecated android

Tags:

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());         }     } 
like image 698
android_explorer Avatar asked Jul 01 '15 08:07

android_explorer


People also ask

What is ActivityManager in Android?

android.app.ActivityManager. This class gives information about, and interacts with, activities, services, and the containing process. A number of the methods in this class are for debugging or informational purposes and they should not be used to affect any runtime behavior of your app.

How do I get running activities on Android?

There's an easy way of getting a list of running tasks from the ActivityManager service. You can request a maximum number of tasks running on the phone, and by default, the currently active task is returned first. Once you have that you can get a ComponentName object by requesting the topActivity from your list.


2 Answers

This should work for the pre Lollipop devices as well as for Lollipop devices

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

Edit: It should return true if the app is in background, not the opposite

like image 176
Mihir Patel Avatar answered Sep 21 '22 21:09

Mihir Patel


This should help you get started

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); } 
like image 45
guy.gc Avatar answered Sep 18 '22 21:09

guy.gc