Я работаю над push-уведомлением в android, где я использую метод ниже, чтобы показать уведомление, но проблема в том, что теперь ActivityManager.getRunningTasks (1); устаревает. Из одного вопроса stackoverflow я читал, что: «вы можете использовать getAppTasks()
, возвращая List<AppTask>
, где вы можете получить RecentTaskInfo
с getTaskInfo
« но я не могу понять, как его использовать. Пожалуйста, помогите мне здесь в этом отношении.ActivityManager.getRunningTasks устарел 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());
}
}
спасибо за ваш ответ здесь. Можете ли вы объяснить мне в соответствии со своим следующим условием, я хочу вставить его, как следующее условие: ActivityManager am = (ActivityManager) ctx.getSystemService (Context.ACTIVITY_SERVICE); \t \t \t \t \t \t \t \t \t \t \t \t Список TaskInfo = am.getRunningTasks (1); \t \t \t \t \t \t ComponentName componentInfo = taskInfo.get (0) .topActivity; –