2015-07-01 10 views
11

Ich arbeite an Push-Benachrichtigung in Android, wo ich eine unten Methode verwende, um Benachrichtigung anzuzeigen, aber das Problem ist, dass jetzt ActivityManager.getRunningTasks (1); wird veraltet. Von einer stackoverflow Frage las ich: "Sie können getAppTasks() verwenden, die List<AppTask> zurückbringt, wo Sie die RecentTaskInfo mit getTaskInfo erhalten können" aber ich kann es nicht herausfinden, wie man es benutzt. Bitte helfen Sie mir hier in dieser Hinsicht.ActivityManager.getRunningTasks ist veraltet android

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

Antwort

9

Dies sollten Sie

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); 
} 
+2

danke für Ihre Antwort hier. Können Sie mir bitte erklären, wie ich die folgende Bedingung erfülle, möchte ich sie wie die folgende Bedingung einbetten: ActivityManager am = (ActivityManager) ctx.getSystemService (Context.ACTIVITY_SERVICE); \t \t \t \t \t \t \t \t \t \t \t \t List = Taskinfo am.getRunningTasks (1); \t \t \t \t \t \t Komponentenname componentInfo = taskInfo.get (0) .topActivity; –

2

können Sie diese wollen loszulegen helfen:

/*** 
* Checking Whether any Activity of Application is running or not 
* @param context 
* @return 
*/ 
public static boolean isForeground(Context context) { 

    // Get the Activity Manager 
    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); 

    // Get a list of running tasks, we are only interested in the last one, 
    // the top most so we give a 1 as parameter so we only get the topmost. 
    List<ActivityManager.RunningAppProcessInfo> task = manager.getRunningAppProcesses(); 

    // Get the info we need for comparison. 
    ComponentName componentInfo = task.get(0).importanceReasonComponent; 

    // Check if it matches our package name. 
    if(componentInfo.getPackageName().equals(context.getPackageName())) 
     return true; 

    // If not then our app is not on the foreground. 
    return false; 
} 
+0

getRunningAppProcesses(); Gibt nur eine App zurück, die selbst ausgeführt wird. Es werden nicht alle Hintergrund-Apps angezeigt. Ich benutze Android Lollipop und über –

28

Dieses auch für die Pre Lollipop Geräte funktionieren sollte wie für Lollipop Geräte

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: Es sollte True zurückgeben, wenn die App im Hintergrund ist, nicht das Gegenteil

+0

gibt es irgendwelche Erlaubnis, in Manifest hinzufügen? wie 'android.permission.GET_TASKS' oder' android.permission.REAL_GET_TASKS' –

+1

Danke, dass es funktioniert, ohne irgendwelche Berechtigungen hinzuzufügen –