Android 7.1 设置默认启动桌面,并设置为主屏幕应用

frameworks/base/core/java/com/android/internal/app/ResolverActivity.java
1、在此Activity添加方法
private boolean setupDefaultLauncher(String packageName) {
    int position = mAdapter.getDefaultHomePosition(packageName);
    if (position == -1) {
        Log.w(TAG, "not find default Home");
        return false;
    }
	ResolveInfo ri = mAdapter.resolveInfoForPosition(position, true);
    TargetInfo intent = mAdapter.targetInfoForPosition(position, true);
    onTargetSelected(intent, false);
    startSelected(position, true, true);
    dismiss();
    return true;
}

2、在此activity的内部类ResolveListAdapter中添加方法
public int getDefaultHomePosition(String packageName){
    for (int i = 0; i < mDisplayList.size(); i++) {
        ResolveInfo info = mDisplayList.get(i).mResolveInfo;
        if (info.activityInfo.packageName.equals(packageName)) {
            return i;
        }
    }
    return -1;
}

3、在onCreate中调用1中方法,就第一次开机时调用,之后就不能再调用了,不然要是再次安装一个桌面应用,就不会被选择了
protected void onCreate(){
	......
	if (configureContentView(mIntents, initialIntents, rList, alwaysUseOption)) {
            return;
    }
    //add start
	boolean isSetDefLauncher= (Settings.System.getInt(getContentResolver(), "set_def_launcher", 0)  == 0);
	if(isSetDefLauncher){
 		Settings.System.putInt(getContentResolver(), "set_def_launcher", 1);
 		if(setupDefaultLauncher("默认桌面包名")){
             finish();
             return;
        }
	}
}

你可能感兴趣的:(Android 7.1 设置默认启动桌面,并设置为主屏幕应用)