Android 10 高通平台,开机启动自定义的launcher(APK)

时间:2025-01-31 09:26:05

在安卓系统中,开机启动了原生打apk(home桌面软件)

当添加了一个设置了类似于以下属性打apk后,会弹出选择页面,选择需要启动的应用作为home

 <category android:name=""/> 
 <category android:name="" /> 

现在,需要跳过该选择的操作,直接进入我们所需启动的apk

前提,在apk的代码中,有以下属性

 <intent-filter>
               <action android:name="" />
               <category android:name=""/> 
               <category android:name="" />

然后,修改对应的代码

代码路径是
frameworks/base/services/core/java/com/android/server/pm/

在该文件中添加一个方法,如下

private void setTargetActivityAsPreferredActivity(Intent intent,List<ResolveInfo> query, int userId){
        (TAG, " setTargetActivityAsPreferredActivity");
        final int N =();
        (TAG, " setTargetActivityAsPreferredActivity N="+N);

        ComponentName[] set = new ComponentName[N];
        ComponentName componentName = null;
        int bestMatch = 0;
        for(int i = 0;i < N; i++){
            ResolveInfo r = (i);
            set[i] = new ComponentName(, );
            (TAG, " setTargetActivityAsPreferredActivity PackageName="+);

            if ( > bestMatch) bestMatch = ;
            if("".equals()){ /*modify target apk packageName  填写你的包名*/
                componentName=set[i];
            }
        }
        IntentFilter filter = new IntentFilter();
        if (() != null) {
            (());
        }
        Set<String> categories = ();
        if (categories != null) {
            for (String cat : categories) {
                (cat);
            }
        }
        (Intent.CATEGORY_DEFAULT);
        if(null!=componentName){
            addPreferredActivity(filter, bestMatch,set,componentName,userId);
        }

    }

然后,在同一个文件中,在chooseBestActivity 方法中调用上述定义的方法

frameworks/base/services/core/java/com/android/server/pm/

private ResolveInfo chooseBestActivity(Intent intent, String resolvedType,
            int flags, List<ResolveInfo> query, int userId) {
        if (query != null) {
            final int N = ();
            if (N == 1) {
                return (0);
            } else if (N > 1) {
                setTargetActivityAsPreferredActivity(intent,query,userId); //这里就是调用的方法啦

                final boolean debug = ((() & Intent.FLAG_DEBUG_LOG_RESOLUTION) != 0);
                // If there is more than one activity with the same priority,
                // then let the user decide between them.
                ResolveInfo r0 = (0);
.....
。。。。

然后,全编译,烧录验证。

参考链接:/cw102055234/article/details/88538121

/yiranfeng/article/details/103752203

如有帮助到你,请点赞谢谢。