1: 在android 清单文件中添加过滤条件
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.MONKEY" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</activity>
2: 构建数据库 相关的资料,数据data
iteminfo
package com.kodulf.mylauncher.data;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.UserHandle;
import android.os.UserManager;
import android.util.Log;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
/**
* Represents an item in the launcher.
*/
class ItemInfo {
static final int NO_ID = -1;
/**
Fix 这里需要用的是long
*/
long id = NO_ID;
String title;
/**
启动的意图
*/
String intent;
int screenId = -1;
/**
application,shortcut,widget
*/
int itemType = -1;
/**
hotseat,workspace,folder
*/
int container = NO_ID;
/**
* x的格子数
*/
int X = 1;
/**
* y的格子数
*/
int Y = 1;
/**
所占的x的宽度,几个行格子
*/
int spanX = 1;
/**
所占的y的宽度,几个列格子
*/
int spanY = 1;
/**
最小的span的宽度跨越的格数
*/
int minSpanX = 1;
/**
最小的span的高度跨越的格数
*/
int minSpanY = 1;
/**
* 当前的用户
*/
UserHandle user;
ItemInfo(){
//获取当前的用户
user = android.os.Process.myUserHandle();
}
@Override
public String toString() {
return "ItemInfo{" +
"id=" + id +
", title='" + title + '\'' +
", intent='" + intent + '\'' +
", screenId=" + screenId +
", itemType=" + itemType +
", container=" + container +
", X=" + X +
", Y=" + Y +
", spanX=" + spanX +
", spanY=" + spanY +
", minSpanX=" + minSpanX +
", minSpanY=" + minSpanY +
", user=" + user +
'}';
}
//todo 下面是没有用到的原生里面的内容
/*
static final String EXTRA_PROFILE = "profile";
static final int NO_ID = -1;
没有用到的原生的
boolean requiresDbUpdate = false;
CharSequence contentDescription;
int[] dropPos = null;
ItemInfo() {
user = android.os.Process.myUserHandle();
}
ItemInfo(ItemInfo info) {
id = info.id;
cellX = info.cellX;
cellY = info.cellY;
spanX = info.spanX;
spanY = info.spanY;
screen = info.screen;
itemType = info.itemType;
container = info.container;
user = info.user;
contentDescription = info.contentDescription;
// tempdebug:
LauncherModel.checkItemInfo(this);
}
Returns the package name that the intent will resolve to, or an empty string if
* none exists.
static String getPackageName(Intent intent) {
if (intent != null) {
String packageName = intent.getPackage();
if (packageName == null && intent.getComponent() != null) {
packageName = intent.getComponent().getPackageName();
}
if (packageName != null) {
return packageName;
}
}
return "";
}
protected void updateUser(Intent intent) {
if (intent != null && intent.hasExtra(EXTRA_PROFILE)) {
user = (UserHandle) intent.getParcelableExtra(EXTRA_PROFILE);
}
}
* Write the fields of this item to the DB
*
* @param context A context object to use for getting a UserManager
* instance.
* @param values
void onAddToDatabase(Context context, ContentValues values) {
values.put(LauncherSettings.BaseLauncherColumns.ITEM_TYPE, itemType);
values.put(LauncherSettings.Favorites.CONTAINER, container);
values.put(LauncherSettings.Favorites.SCREEN, screen);
values.put(LauncherSettings.Favorites.CELLX, cellX);
values.put(LauncherSettings.Favorites.CELLY, cellY);
values.put(LauncherSettings.Favorites.SPANX, spanX);
values.put(LauncherSettings.Favorites.SPANY, spanY);
long serialNumber = ((UserManager) context.getSystemService(Context.USER_SERVICE))
.getSerialNumberForUser(user);
values.put(LauncherSettings.Favorites.PROFILE_ID, serialNumber);
}
void updateValuesWithCoordinates(ContentValues values, int cellX, int cellY) {
values.put(LauncherSettings.Favorites.CELLX, cellX);
values.put(LauncherSettings.Favorites.CELLY, cellY);
}
static byte[] flattenBitmap(Bitmap bitmap) {
// Try go guesstimate how much space the icon will take when serialized
// to avoid unnecessary allocations/copies during the write.
int size = bitmap.getWidth() * bitmap.getHeight() * 4;
ByteArrayOutputStream out = new ByteArrayOutputStream(size);
try {
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
return out.toByteArray();
} catch (IOException e) {
Log.w("Favorite", "Could not write icon");
return null;
}
}
static void writeBitmap(ContentValues values, Bitmap bitmap) {
if (bitmap != null) {
byte[] data = flattenBitmap(bitmap);
values.put(LauncherSettings.Favorites.ICON, data);
}
}
* It is very important that sub-classes implement this if they contain any references
* to the activity (anything in the view hierarchy etc.). If not, leaks can result since
* ItemInfo objects persist across rotation and can hence leak by holding stale references
* to the old view hierarchy / activity.
void unbind() {
}
*/
}
这篇博客介绍了如何在Android应用中创建启动器。首先,通过在Android清单文件中添加过滤条件来配置启动活动。接着,详细讨论了构建数据库的过程,包括数据存储和iteminfo的相关操作。
481





