搭载基于RK3229的Android5.1修改开机默认桌面Launcher

1、找到ActivityManagerService.java

在..\rk3229_5.1_box\frameworks\base\services\core\java\com\android\server\am目录找到ActivityManagerService.java文件。在文件里找到startHomeActivityLocked函数里的setDefaultLauncher。

 boolean startHomeActivityLocked(int userId, String reason) {

        setDefaultLauncher(userId);

        if (mFactoryTest == FactoryTest.FACTORY_TEST_LOW_LEVEL
                && mTopAction == null) {
            // We are running in factory test mode, but unable to find
            // the factory test app, so just sit around displaying the
            // error message and don't try to start anything.
            return false;
        }
        Intent intent = getHomeIntent();
        ActivityInfo aInfo =
            resolveActivityInfo(intent, STOCK_PM_FLAGS, userId);
        if (aInfo != null) {
            intent.setComponent(new ComponentName(
                    aInfo.applicationInfo.packageName, aInfo.name));
            // Don't do this if the home app is currently being
            // instrumented.
            aInfo = new ActivityInfo(aInfo);
            aInfo.applicationInfo = getAppInfoForUser(aInfo.applicationInfo, userId);
            ProcessRecord app = getProcessRecordLocked(aInfo.processName,
                    aInfo.applicationInfo.uid, true);
            if (app == null || app.instrumentationClass == null) {
                intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
                mStackSupervisor.startHomeActivity(intent, aInfo, reason);
            }
        }

        return true;
    }

2、设置开机默认桌面包名

在setDefaultLauncher设置开机默认桌面launch的包名。我开发的固件,所要启动的包名如下:

String packageName = SystemProperties.get("persist.sys.yz.defpackage", "com.zhai.ads");

String className = SystemProperties.get("persist.sys.yz.defclass", "com.zhai.touchhome.Loading");

    private void setDefaultLauncher(int userId)
    {
        // get default component
        //String  packageName = SystemProperties.get("persist.sys.yz.defpackage", "com.zhaisoft.app.hesiling");
       // String  className = SystemProperties.get("persist.sys.yz.defclass", "com.zhaisoft.app.hesiling.activity.MainActivity");
		
		 String  packageName = SystemProperties.get("persist.sys.yz.defpackage", "com.zhai.ads");
         String  className = SystemProperties.get("persist.sys.yz.defclass", "com.zhai.touchhome.Loading");
		
		
        Slog.i(TAG, "default packageName = " + packageName + ", default className = " + className);
        if(!packageName.equals("no") && !className.equals("no")){
            boolean firstLaunch = SystemProperties.getBoolean("persist.sys.sw.firstLaunch", true);
            Slog.d(TAG, "firstLaunch = " + firstLaunch);
            if(firstLaunch){
                mFirstLaunch = true;
                // do this only for the first boot
                SystemProperties.set("persist.sys.sw.firstLaunch", "false");
            }
            Slog.d(TAG, "mFirstLaunch = " + mFirstLaunch);
           // if(mFirstLaunch){
                IPackageManager pm = ActivityThread.getPackageManager();

                //clear the current preferred launcher
                ArrayList<IntentFilter> intentList = new ArrayList<IntentFilter>();
                ArrayList<ComponentName> cnList = new ArrayList<ComponentName>();
                mContext.getPackageManager().getPreferredActivities(intentList, cnList, null);
                IntentFilter dhIF;
                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());
                    }
                }

                // get all Launcher activities
                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, userId);
                }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();
                ComponentName[] set = new ComponentName[N];
                int bestMatch = 0;
                for (int i = 0; i < N; i++)
                {
                    ResolveInfo r = list.get(i);
                    set[i] = new ComponentName(r.activityInfo.packageName,
                            r.activityInfo.name);
                    if (r.match > bestMatch) bestMatch = r.match;
                }
                // add the default launcher as the preferred launcher
                ComponentName launcher = new ComponentName(packageName, className);
                try
                {
                    pm.addPreferredActivity(filter, bestMatch, set, launcher, userId);
                } catch (RemoteException e) {
                    throw new RuntimeException("Package manager has died", e);
                }
            //}
        }
    }

3、寻找HomeSettings.java

在..\rk3229_5.1_box\packages\apps\Settings\src\com\android\settings里寻找HomeSettings.java文件。在makeCurrentHome里第二次设置自己需要的桌面。主要是客户有两个桌面,需要频繁切换。

    void makeCurrentHome(HomeAppPreference newHome) {
        if (mCurrentHome != null) {
            mCurrentHome.setChecked(false);
        }
        newHome.setChecked(true);
        mCurrentHome = newHome;

        mPm.replacePreferredActivity(mHomeFilter, IntentFilter.MATCH_CATEGORY_EMPTY,
                mHomeComponentSet, newHome.activityName);
	//huangxing add 
		System.out.println("huangxing----activityName== " + newHome.activityName);
		//ComponentName chatActivity =new ComponentName();
		
		String launcherInfo  = newHome.activityName.toString();
		System.out.println("huangxing----launcherInfo== " + launcherInfo);
		if(launcherInfo.indexOf("com.android.launcher3") != -1){
			SystemProperties.set("persist.sys.yz.defpackage", "com.android.launcher3");
			SystemProperties.set("persist.sys.yz.defclass", "com.android.launcher3.Launcher");
			System.out.println("huangxing---launcher3-----");
		}else if(launcherInfo.indexOf("com.zhai.ads") != -1){
			SystemProperties.set("persist.sys.yz.defpackage", "com.zhai.ads");
			SystemProperties.set("persist.sys.yz.defclass", "com.zhai.touchhome.Loading");
			System.out.println("huangxing---com.zhai.ads-----");
		}else {
			//wjz add
			System.out.println("huangxing---heshiling launcher-----");
			SystemProperties.set("persist.sys.yz.defpackage", "com.zhaisoft.app.hesiling");
			SystemProperties.set("persist.sys.yz.defclass", "com.zhaisoft.app.hesiling.activity.MainActivity");
			//SystemProperties.set("persist.sys.yz.defpackage", "com.zhai.ads");
			//SystemProperties.set("persist.sys.yz.defclass", "com.zhai.touchhome.Loading");
		}
        getActivity().setResult(Activity.RESULT_OK);
		
	
    }
相关推荐
丫头,冲鸭!!!5 天前
安卓开发实现图像处理相关功能
图像处理·人工智能·计算机视觉·安卓
hello world smile16 天前
Flutter常用命令整理
android·flutter·移动开发·android studio·安卓
潘帕斯的雄鹰18 天前
【1个月速成Java】基于Android平台开发个人记账app学习日记——第4天,注册登录逻辑代码
java·学习·安卓·自定义安卓app图标
jingling55523 天前
Android系统架构
android·arm开发·系统架构·安卓
qiuqiushuibx1 个月前
安卓基础001
安卓
第三女神程忆难1 个月前
Android Kotlin 高阶函数详解及其在协程中的应用
android·开发语言·kotlin·移动开发·安卓·高阶函数·1024程序员节
x0241 个月前
Android Room(SQLite) too many SQL variables异常
sqlite·安卓·android jetpack·1024程序员节
惜.己1 个月前
Appium环境搭建全流程(含软件)
python·测试工具·node.js·appium·pytest·安卓·1024程序员节
shandianchengzi1 个月前
【记录】Android|安卓平板 猫游戏(四款,peppy cat,含下载教程和链接)
android·游戏·安卓·平板·cat··tablet
jingling5551 个月前
adb常见指令以及问题解决
开发语言·功能测试·测试工具·adb·安卓