参考: http://blog.csdn.net/fzh0803/archive/2011/03/26/6279995.aspx
http://gqdy365.iteye.com/blog/897638
http://www.cnblogs.com/playing/archive/2011/03/26/1996209.html
android1.6的版本有3个屏 需要把它改为5个屏 需要修改的地方 如下
1、Launcher.java
- static final int SCREEN_COUNT = 5;
- static final int DEFAULT_SCREN = 2;
2、launcher.xml
- <com.lp.launcher.Workspace
- android:id="@+id/workspace"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- launcher:defaultScreen="2">//从0开始
- <include android:id="@+id/cell1" layout="@layout/workspace_screen" />
- <include android:id="@+id/cell2" layout="@layout/workspace_screen" />
- <include android:id="@+id/cell3" layout="@layout/workspace_screen" />
- <include android:id="@+id/cell4" layout="@layout/workspace_screen" />
- <include android:id="@+id/cell5" layout="@layout/workspace_screen" />
- </com.lp.launcher.Workspace>
defaultScreen 修改为2 然后 加两个<include />
然后 修改默认显示的屏
3、Workspace.java
修改构造方法里面的
- mDefaultScreen = a.getInt(R.styleable.Workspace_defaultScreen, 2);//从0开始
这时 基本上已经可以显示5个屏幕了 默认的屏也是第三个屏了 但是进去后 默认显示的屏什么也没有 我们需要把组建都挪到默认屏上去
4、default_workspace.xml
修改所有的 launcher:screen 为 2
- launcher:screen="2"
修改完这个后 如果你不是make到源码 而是以app的方式安装到手机上的话 就必须先卸载原有的这个程序 然后在部署
但是 这里有一个问题 我们上次修改的标记 也需要修改
5、home_arrows_left.xml home_arrows_right.xml
- <level-list xmlns:android="http://schemas.android.com/apk/res/android">
- <item android:maxLevel="0" android:drawable="@android:color/transparent" />
- <item android:maxLevel="1" android:drawable="@drawable/home_arrows_left_1" />
- <item android:maxLevel="2" android:drawable="@drawable/home_arrows_left_2" />
- <item android:maxLevel="3" android:drawable="@drawable/home_arrows_left_3" />
- <item android:maxLevel="4" android:drawable="@drawable/home_arrows_left_4" />
- </level-list>
- <level-list xmlns:android="http://schemas.android.com/apk/res/android">
- <item android:maxLevel="0" android:drawable="@drawable/home_arrows_right_4" />
- <item android:maxLevel="1" android:drawable="@drawable/home_arrows_right_3" />
- <item android:maxLevel="2" android:drawable="@drawable/home_arrows_right_2" />
- <item android:maxLevel="3" android:drawable="@drawable/home_arrows_right_1" />
- <item android:maxLevel="4" android:drawable="@android:color/transparent" />
- </level-list>
大功告成 over
最后 说一个期间的一个知识
自定义控件的属性 declare-styleable
比如说 我们上面修改的 launcher:defaultScreen="2" 这个就是自定义的属性 因为这属性不是ViewGroup(workspace类是继承于ViewGroup)所定义的属性
1、在\res\values里面的attrs.xml里面定义
- <resources>
- <declare-styleable name="Workspace">
- <!-- The first screen the workspace should display. -->
- <attr name="defaultScreen" format="integer" />
- </declare-styleable>
- <!-- CellLayout specific attributes. These attributes are used to customize
- a CellLayout view in XML files. -->
- <declare-styleable name="CellLayout">
- <!-- The width of a single cell -->
- <attr name="cellWidth" format="dimension" />
- <!-- The height of a single cell -->
- <attr name="cellHeight" format="dimension" />
- <!-- Padding to apply at the start of the long axis -->
- <attr name="longAxisStartPadding" format="dimension" />
- <!-- Padding to apply at the end of the long axis -->
- <attr name="longAxisEndPadding" format="dimension" />
- <!-- Padding to apply at the start of the short axis -->
- <attr name="shortAxisStartPadding" format="dimension" />
- <!-- Padding to apply at the end of the short axis -->
- <attr name="shortAxisEndPadding" format="dimension" />
- <!-- Number of cells on the short axis of the CellLayout -->
- <attr name="shortAxisCells" format="integer" />
- <!-- Number of cells on the long axis of the CellLayout -->
- <attr name="longAxisCells" format="integer" />
- </declare-styleable>
- </resources>
2、layout.xml引用
- 先申明xmlns:launcher="http://schemas.android.com/apk/res/com.lp.launcher"(R.java),
- 这样就可以使用launcher:defaultScreen。
3、java文件取值
- public Workspace(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Workspace, defStyle, 0);
- mDefaultScreen = a.getInt(R.styleable.Workspace_defaultScreen, 2);
- a.recycle();
- initWorkspace();
- }
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
Android Launcher 简单修改
1. 修改Launcher中显示的预置app
default_workspace.xml
packageName:包名 className :在桌面上显示的activity screen : 在哪一个screen添加 x,y: 在screen中的位置
2. 修改屏幕数目
(1)launcher.java
static final int SCREEN_COUNT = 7;
static final int DEFAULT_SCREEN = 3;
static final int NUMBER_CELLS_X = 4;
static final int NUMBER_CELLS_Y = 4;
(2)launcher.xml
(3)home_arrows_left.xml、 home_arrows_right.xml
(4)新增资源文件:
home_arrows_left_5.xml
home_arrows_left_6.xml
home_arrows_right_5.xml
home_arrows_right_6.xml
ic_home_arrows_5_focus.png
ic_home_arrows_5_focus_right.png
ic_home_arrows_5_normal.png
ic_home_arrows_5_normal_right.png
ic_home_arrows_5_press.png
ic_home_arrows_5_press_right.png
ic_home_arrows_6_focus.png
ic_home_arrows_6_focus_right.png
ic_home_arrows_6_normal_right.png
ic_home_arrows_6_press.png
ic_home_arrows_6_press_right.png
ic_home_arrows_6_normal.png
3. 在Launcher桌面添加widget
5. 设置墙纸
替换图片
frameworks/base/core/res/res/drawable/default_wallpaper.jpg
mm
make snod
6. 修改快捷方式的大小
7. 修改桌面的行列
(1)修改行列
Android_src\packages\apps\Launcher\res\layout-port\workspace_screen.xml
launcher:shortAxisCells=”4″ launcher:longAxisCells=”4″
行 列
改为:
launcher:shortAxisCells=”4″ launcher:longAxisCells=”6″
(2)修改widget
这样调整后,laucher home screen的icon变为6行,6列,显得比较紧凑了。但是search widget就比较别扭了,只占据了左边的一部分,并没有占据整个第一行。那么如何解决这个问题?
调整Android_src\packages\apps\Launcher\src\com\android\launcher\Widget.java
/**
* Represents one instance of a Launcher widget, such as search.
*/
class Widget extends ItemInfo {
int layoutResource;
static Widget makeSearch() {
Widget w = new Widget();
w.itemType = LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH;
w.spanX = 4;
w.spanY = 1;
w.layoutResource = R.layout.widget_search;
return w;
}
}
修改为:
w.spanX = 6;
w.spanY = 1;
8. 删除默认widget
(1) 删除数据库
launcherProvide.java
launcher.db
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
让你自己写的Android的Launcher成为系统中第一个启动的,也是唯一的Launcher.
如果你要定制一个Android系统,你想用你自己的Launcher(Home)作主界面来替换Android自己的Home,而且不希望用户安装的Launcher来替换掉你的Launcher.
我们可以通过修改Framework来实现这样的功能。
这里以Android2.1的源代码为例来实际说明。
1)首先了解一下Android的启动过程。
Android系统的启动先从Zygote开始启动,然后......(中间的过程就不说了).....一直到了SystemServer(framework)这个地方,看到这段代码:
/**
* This method is called from Zygote to initialize the system. This will cause the native
* services (SurfaceFlinger, AudioFlinger, etc..) to be started. After that it will call back
* up into init2() to start the Android services.
*/
native public static void init1(String[] args);
public static void main(String[] args) {
if (SamplingProfilerIntegration.isEnabled()) {
SamplingProfilerIntegration.start();
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
SamplingProfilerIntegration.writeSnapshot("system_server");
}
}, SNAPSHOT_INTERVAL, SNAPSHOT_INTERVAL);
}
// The system server has to run all of the time, so it needs to be
// as efficient as possible with its memory usage.
VMRuntime.getRuntime().setTargetHeapUtilization(0.8f);
System.loadLibrary("android_servers");
init1(args);
}
public static final void init2() {
Log.i(TAG, "Entered the Android system server!");
Thread thr = new ServerThread();
thr.setName("android.server.ServerThread");
thr.start();
}
}
从SystemServer的main函数开始启动各种服务。
首先启动init1,然后启动init2.
从上面的注释可以看到:init1这个方法时被Zygote调用来初始化系统的,init1会启动native的服务如SurfaceFlinger,AudioFlinger等等,这些工作做完以后会回调init2来启动Android的service。
这里我们主要来关注init2的过程。
init2中启动ServerThread线程,
ServerThread中启动了一系列的服务,比如这些:
ActivityManagerService
EntropyService
PowerManagerService
TelephonyRegistry
PackageManagerService
AccountManagerService
BatteryService
HardwareService
Watchdog
SensorService
BluetoothService
StatusBarService
ClipboardService
InputMethodManagerService
NetStatService
ConnectivityService
AccessibilityManagerService
NotificationManagerService
MountService
DeviceStorageMonitorService
LocationManagerService
SearchManagerService
FallbackCheckinService
WallpaperManagerService
AudioService
BackupManagerService
AppWidgetService
这些大大小小的服务起来以后,开始
((ActivityManagerService)ActivityManagerNative.getDefault()).systemReady()
在systemReady后开始开始启动Launcher。
在寻找Launcher的时候是根据HOME的filter(在Manifest中定义的<category android:name="android.intent.category.HOME" />)来过滤。
然后根据filter出来的HOME来启动,如果只有一个HOME,则启动这个HOME,如果用户自己装了HOME,那就会弹出来一个列表供用户选择。
我们现在希望从这里弹出我们自己定制的Launcher,同时也不希望弹出选择HOME的界面,我们不希望用户修改我们的home,比如我们的home上放了好多广告,以及强制安装的程序,不希望用户把它干掉。
我们可以通过这样来实现:
2) 定义一个私有的filter选项,然后用这个选项来过滤HOME.
一般情况下我们使用Manifest中定义的<category android:name="android.intent.category.HOME"来过滤的,我们现在增加一个私有的HOME_FIRST过滤。
在Intent.java(frameworks/base/core/java/android/content/Intent.java)中添加两行代码
//lixinso:添加CATEGORY_HOME_FIRST
@SdkConstant(SdkConstantType.INTENT_CATEGORY)
public static final String CATEGORY_HOME_FIRST = "android.intent.category.HOME_FIRST";
3)修改和CATEGORY_HOME相关的所有的地方,都改成HOME_FIRST,主要是framework中的这几个地方:
frameworks/base/services/java/com/android/server/am/ActivityManagerService.java中
//intent.addCategory(Intent.CATEGORY_HOME);
改成intent.addCategory(Intent.CATEGORY_HOME_FIRST); //lixinso:
//if (r.intent.hasCategory(Intent.CATEGORY_HOME)) {
改成if (r.intent.hasCategory(Intent.CATEGORY_HOME_FIRST)) { //lixinso: Intent.CATEGORY_HOME -> Intent.CATEGORY_HOME_FIRST
frameworks/base/services/java/com/android/server/am/HistoryRecorder.java中
// _intent.hasCategory(Intent.CATEGORY_HOME) &&
改成 _intent.hasCategory(Intent.CATEGORY_HOME_FIRST) && //lixinso: Intent.CATEGORY_HOME->Intent.CATEGORY_HOME_FIRST
frameworks/policies/base/mid/com/android/internal/policy/impl/MidWindowManager.java中
//mHomeIntent.addCategory(Intent.CATEGORY_HOME);
改成 mHomeIntent.addCategory(Intent.CATEGORY_HOME_FIRST); //lixinso
frameworks/policies/base/mid/com/android/internal/policy/impl/RecentApplicationsDialog.java中
//new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME),0);
改成 new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME_FIRST),0); //lixinso
frameworks/policies/base/phone/com/android/internal/policy/impl/PhoneWindowManager.java中
//mHomeIntent.addCategory(Intent.CATEGORY_HOME);
改成 mHomeIntent.addCategory(Intent.CATEGORY_HOME_FIRST); //lixinso
frameworks/policies/base/phone/com/android/internal/policy/impl/RecentApplicationsDialog.java中
//ResolveInfo homeInfo = pm.resolveActivity(new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME),0);
改成 ResolveInfo homeInfo = pm.resolveActivity(new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME_FIRST),0); //lixinso
4) 写一个自己的Launcher.
可以参考android sample中的Launcher,或者android源代码中的 /packages/apps/Launcher 来写。
在Launcher中标记其是不是Launcher的最关键的代码时Manifest中的filter:android:name="android.intent.category.HOME"
现在我们定义了自己的filter,那么,我们在我们自己写的Launcher中将Manifest改为:
<application android:process="android.process.acore3" android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".FirstAppActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME_FIRST" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.MONKEY" />
</intent-filter>
</activity>
</application>
然后将编译好的apk放到/out/target/product/generic/system/app目录下。
5)将Android自带的Launcher删除掉,包括源代码(packages/apps/Launcher)和apk(/out/target/product/generic/system/app/Launcher.apk)。
6)
做完这些工作,就可以重新编译Android了,我们可以编译修改过的几个相关的包。
如果之前编译过了Android源码,可以用mmm命令来编译部分的改动。
这里需要这样编译:
$ . build/envsetup.sh
$ mmm frameworks/base
$ mmm frameworks/base/services/java
$ mmm frameworks/policies/base/mid
$ mmm frameworks/policies/base/phone
7)
编译完成后重新生成img文件。
$ make snod
8) 现在可以启动Android模拟器来看效果了。
首先设置环境变量:
$ export ANDROID_PRODUCT_OUT= ./out/target/product/generic
然后切换到
$ cd ./out/host/linux-x86/bin
运行
$ ./emulator
这样我们启动的模拟器里面用的image就是我们刚才编译好的自己定制的东西了。
从模拟器上可以看到启动的Launcher是我们自己的Launcher,不会出现默认的Launcher了,也不会出现选择界面。
9)我们再验证一下,如果用户装上了一个其他的Launcher(Home)会怎么样。
从网上找一个一般的Launcher或者自己写一个一般的Launcher装上去,重新启动,不会出现选择界面。
按HOME键也不会出来两个HOME来选择。
这样我们就牢牢控制了用户的桌面。
只有我们自己定制的HOME才能装上。 这对于定制Android设备的厂商很有用处。