gpt4 book ai didi

android - ActivityManager.getRunningTasks 已弃用 android

转载 作者:IT老高 更新时间:2023-10-28 23:27:09 32 4
gpt4 key购买 nike

我正在 android 中处理推送通知,我正在使用以下方法显示通知,但问题是现在 ActivityManager.getRunningTasks(1);正在被弃用。从一个stackoverflow问题我读到:“你可以使用getAppTasks()返回一个List<AppTask>,你可以在其中获得RecentTaskInfogetTaskInfo”,但我不知道如何使用它。请在这方面帮助我。

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

最佳答案

这应该适用于 Lollipop 之前的设备以及 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;
}

编辑:如果应用程序在后台,它应该返回 true,而不是相反

关于android - ActivityManager.getRunningTasks 已弃用 android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31156313/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com