在安卓系统中,开机启动了原生打apk(home桌面软件)
当添加了一个设置了类似于以下属性打apk后,会弹出选择页面,选择需要启动的应用作为home
<category android:name="android.intent.category.HOME"/>
<category android:name="android.intent.category.DEFAULT" />
现在,需要跳过该选择的操作,直接进入我们所需启动的apk
前提,在apk的代码中,有以下属性
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME"/>
<category android:name="android.intent.category.DEFAULT" />
然后,修改对应的代码
代码路径是
frameworks/base/services/core/java/com/android/server/pm/PackageManagerService.java
在该文件中添加一个方法,如下
private void setTargetActivityAsPreferredActivity(Intent intent,List<ResolveInfo> query, int userId){
Slog.e(TAG, " setTargetActivityAsPreferredActivity");
final int N =query.size();
Slog.e(TAG, " setTargetActivityAsPreferredActivity N="+N);
ComponentName[] set = new ComponentName[N];
ComponentName componentName = null;
int bestMatch = 0;
for(int i = 0;i < N; i++){
ResolveInfo r = query.get(i);
set[i] = new ComponentName(r.activityInfo.packageName, r.activityInfo.name);
Slog.e(TAG, " setTargetActivityAsPreferredActivity PackageName="+r.activityInfo.packageName);
if (r.match > bestMatch) bestMatch = r.match;
if("com.android.xxxx".equals(r.activityInfo.packageName)){ /*modify target apk packageName 填写你的包名*/
componentName=set[i];
}
}
IntentFilter filter = new IntentFilter();
if (intent.getAction() != null) {
filter.addAction(intent.getAction());
}
Set<String> categories = intent.getCategories();
if (categories != null) {
for (String cat : categories) {
filter.addCategory(cat);
}
}
filter.addCategory(Intent.CATEGORY_DEFAULT);
if(null!=componentName){
addPreferredActivity(filter, bestMatch,set,componentName,userId);
}
}
然后,在同一个文件中,在chooseBestActivity 方法中调用上述定义的方法
frameworks/base/services/core/java/com/android/server/pm/PackageManagerService.java
private ResolveInfo chooseBestActivity(Intent intent, String resolvedType,
int flags, List<ResolveInfo> query, int userId) {
if (query != null) {
final int N = query.size();
if (N == 1) {
return query.get(0);
} else if (N > 1) {
setTargetActivityAsPreferredActivity(intent,query,userId); //这里就是调用的方法啦
final boolean debug = ((intent.getFlags() & 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 = query.get(0);
.....
。。。。
然后,全编译,烧录验证。
参考链接:https://blog.csdn.net/cw102055234/article/details/88538121
https://blog.csdn.net/yiranfeng/article/details/103752203
如有帮助到你,请点赞谢谢。
|