Android5.0

ActivityManager:addAppTask (Activity activity, Intent intent, ActivityManager.TaskDescription description, Bitmap thumbnail) 1.作用 为应用创建一个新的任务,并放在任务栈的栈顶。 参数:activity :

ActivityManager:addAppTask (Activity activity, Intent intent, ActivityManager.TaskDescription description, Bitmap thumbnail)

1.作用

为应用创建一个新的任务,并放在任务栈的栈顶。

参数:activity :需要被添加的Activity,用于确定进入任务栈的入口

intent :用于启动Activity的Intent

description:携带的信息描述

thumbnail :缩略图资源

2.源码

public int addAppTask(@NonNull Activity activity, @NonNull Intent intent,

@Nullable TaskDescription description, @NonNull Bitmap thumbnail) {

Point size;

synchronized (this) {

ensureAppTaskThumbnailSizeLocked();

size = mAppTaskThumbnailSize;

}

final int tw = thumbnail.getWidth();

final int th = thumbnail.getHeight();

if (tw != size.x || th != size.y) {

Bitmap bm = Bitmap.createBitmap(size.x, size.y, thumbnail.getConfig());

float scale;

float dx = 0, dy = 0;

if (tw * size.x > size.y * th) {

scale = (float) size.x / (float) th;

dx = (size.y - tw * scale) * 0.5f;

} else {

scale = (float) size.y / (float) tw;

dy = (size.x - th * scale) * 0.5f;

}

Matrix matrix = new Matrix();

matrix.setScale(scale, scale);

matrix.postTranslate((int) (dx + 0.5f), 0);

Canvas canvas = new Canvas(bm);

canvas.drawBitmap(thumbnail, matrix, null);

canvas.setBitmap(null);

thumbnail = bm;

}

if (description == null) {

description = new TaskDescription();

}

try {

return ActivityManagerNative.getDefault().addAppTask(activity.getActivityToken(),

intent, description, thumbnail);

} catch (RemoteException e) {

throw new IllegalStateException("System dead?", e);

}

}

3.分析及说明

我们首先来看一下ensureAppTaskThumbnailSizeLocked()

private void ensureAppTaskThumbnailSizeLocked() {

if (mAppTaskThumbnailSize == null) {

try {

mAppTaskThumbnailSize = ActivityManagerNative.getDefault().getAppTaskThumbnailSize();

} catch (RemoteException e) {

throw new IllegalStateException("System dead?", e);

}

}

}

而ActivityManagerNative.getDefault().getAppTaskThumbnailSize()返回的是一个Size,该size是确保addAppTask()中的Bitmap参数所需的Size。

其次我们来看一下addAppTask()的返回值,返回新添加的任务id应用任务,如果添加失败则返回-1;

如果创建失败了,那么极大地原因是没有更多的空间为你的应用程任务服务。

知秋君
上一篇 2024-08-26 12:02
下一篇 2024-08-26 11:36

相关推荐