2015-07-01 18 views
11

Tôi đang làm việc trên thông báo đẩy trong Android, nơi tôi đang sử dụng một phương pháp dưới đây để hiển thị thông báo, nhưng vấn đề là bây giờ ActivityManager.getRunningTasks (1); không được chấp nhận. Từ một câu hỏi stackoverflow tôi đọc rằng: "bạn có thể sử dụng getAppTasks() trả lại một List<AppTask> nơi bạn có thể nhận được RecentTaskInfo với getTaskInfo" nhưng tôi không thể tìm ra cách sử dụng nó. Xin hãy giúp tôi ở đây về vấn đề này.ActivityManager.getRunningTasks không được chấp nhận 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()); 
     } 
    } 

Trả lời

9

Điều này sẽ giúp bạn bắt đầu

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

cảm ơn câu trả lời của bạn tại đây. Bạn có thể vui lòng giải thích cho tôi theo điều kiện sau đây của tôi, tôi muốn nhúng nó như sau điều kiện: 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 ComponentName componentInfo = taskInfo.get (0) .topActivity; –

2

bạn có thể muốn điều này:

/*** 
* 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(); chỉ trả về một ứng dụng đang chạy nó, nó không hiển thị tất cả các ứng dụng nền. Tôi đang sử dụng kẹo Android và trên –

28

này nên làm việc cho các thiết bị Lollipop trước cũng như cho các thiết bị 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; 
    } 

Chỉnh sửa: Nó sẽ trả về true nếu ứng dụng ở chế độ nền, không phải đối diện

+0

có bất kỳ sự cho phép nào cần thêm vào trong Tệp kê khai không? như 'android.permission.GET_TASKS' hoặc' android.permission.REAL_GET_TASKS' –

+1

Cảm ơn bạn nó hoạt động mà không cần thêm bất kỳ quyền nào –

Các vấn đề liên quan