ps:本文仅做记录,只适合定制自家系统或者一些智能终端或硬件,国内各大厂家未做测试(估计不行,需要ROM)
(ps:Android5.0之后的Android,最好将apk文件push到/system/priv-app目录))
AndroidMainfest.xml中MainActivity添加
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
Application下添加
android:sharedUserId="android.uid.system"
adb push xxx.so /system/lib/xxx.so
(ps:如果设备是64位,system下会有lib以及lib64两个目录)
通过nativeLibrary可以知道,加载so文件路径 [/vendor/lib, /system/lib],所以我将so文件push进system/lib目录
重启后会弹出让你选择launcher的弹框,会看到自己的apk可以被选择设置成launcher(要注意push到system/lib下的apk,需要改一下权限chmod 777 xx.apk,否则重启不会被自动安装)
问题1:
push的时候提示 remote Read-only file system
解决:(设备需要root)重新挂载
1、adb shell
2、mount -o remount /system
3、重新push
问题2:
在push so文件时,由于刚开始system/lib只读,做了一步操作,
1、将so文件push进入/sdcard
2、然后adb shell
3、mv /sdcard/xxx.so /system/lib/xxx.so
结果reboot后,找不到so文件
java.lang.UnsatisfiedLinkError: No implementation found for void org.xxx.xxxFactory.nativeInitializeAndroidGlobals() (tried Java_org_xxx_xxxFactory_nativeInitializeAndroidGlobals and Java_org_xxx_xxxFactory_nativeInitializeAndroidGlobals__)
解决:在重新挂载mount -o remount /system后,直接push到system/lib
修改Android FrameWork源码的方式:
sources目录下ActivityManagerService类startHomeActivityLocked方法中添加
(下面这段我网上拷贝的(先叩谢大佬们),当然也是可以自己定义规则,比如默认launcher包名以及launcher入口,以及以下的CATEGORY_HOME可以定义成自己的,比如<category android:name="android.intent.category.xxxxx_HOME" />,但是此处的CATEGORY_HOME就需要改成CATEGORY_xxxxx_HOME了,还有一些地方需要更改,此方法有点暴力直接可以自动设置launcher,不会提示我们选择launcher)
private void setDefaultLauncher() {
String packageName = "com.android.test"; //默认launcher包名
String className = "com.android.test.MainActivity"; //默认launcher入口
Slog.i(TAG, "defautl packageName = " + packageName + ", default className = " + className);
IPackageManager pm = ActivityThread.getPackageManager();
//清除当前默认launcher
ArrayList<IntentFilter> intentList = new ArrayList<IntentFilter>();
ArrayList<ComponentName> cnList = new ArrayList<ComponentName>();
mContext.getPackageManager().getPreferredActivities(intentList, cnList, null);
IntentFilter dhIF = null;
for(int i = 0; i < cnList.size(); i++) {
dhIF = intentList.get(i);
if(dhIF.hasAction(Intent.ACTION_MAIN) && dhIF.hasCategory(Intent.CATEGORY_HOME)) {
mContext.getPackageManager().clearPackagePreferredActivities(cnList.get(i).getPackageName());
}
}
//获取所有launcher activity
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
List<ResolveInfo> list = new ArrayList<ResolveInfo>();
try {
list = pm.queryIntentActivities(intent, intent.resolveTypeIfNeeded(mContext.getContentResolver()), PackageManager.MATCH_DEFAULT_ONLY);
}catch (RemoteException e) {
throw new RuntimeException("Package manager has died", e);
}
// get all components and the best match
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_MAIN);
filter.addCategory(Intent.CATEGORY_HOME);
filter.addCategory(Intent.CATEGORY_DEFAULT);
final int N = list.size();
Slog.d(TAG, "N:::::hyhyhyhy:::: = " + N);
//设置默认launcher
ComponentName launcher = new ComponentName(packageName, className);
ComponentName[] set = new ComponentName[N];
int defaultMatch = 0;
for (int i = 0; i < N; i++) {
ResolveInfo r = list.get(i);
set[i] = new ComponentName(r.activityInfo.packageName, r.activityInfo.name);
Slog.d(TAG, "r.activityInfo.packageName:::::hyhyhyhy:::: = " + r.activityInfo.packageName);
Slog.d(TAG, "r.activityInfo.name:::::hyhyhyhy:::: = " + r.activityInfo.name);
if(launcher.getClassName().equals(r.activityInfo.name)) {
defaultMatch = r.match;
}
}
//将设置的默认launcher,添加到系统偏好
try {
pm.addPreferredActivity(filter, defaultMatch, set, launcher);
} catch (RemoteException e) {
throw new RuntimeException("factorytest.MainActivity : Package manager has died", e);
}
}