Android系统存在多个Launcher时,设置开机进入默认的Launcher

转载 2015年11月20日 16:31:47

问题提出:

      当系统存在多个Launcher时,开机完成并解锁后,系统会弹出一个选择框让用户选择
进入哪一个Launcher,如果不想选择,而是想直接进入某一个默认的Launcher,并且
用户在设置->应用程序->管理应用程序->所有应用程序列表,找到之前设置的那个默
认的Launcher,并且取消了默认值,可以重新弹出launcher选框。

解决方法:

      下面以设置Google
Launcher2作为默认启动的Launcher为例,
其package name 为 com.android.launcher
launcher activity name 为 com.android.launcher2.Launcher

需要以下两个修改点

1)修改Provision应用

编辑packages/apps/Provision/src/com/android/DefaultActivity.java文件

/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.provision;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;

import java.util.List;

/**
 * Application that sets the provisioned bit, like SetupWizard does.
 */
public class DefaultActivity extends Activity {

    private static final String TAG = "DefaultActivity";

    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        //新增这个函数的调用
        setupDefaultLauncher();

        // Add a persistent setting to allow other apps to know the device has been provisioned.
        Settings.Global.putInt(getContentResolver(), Settings.Global.DEVICE_PROVISIONED, 1);

        // remove this activity from the package manager.
        PackageManager pm = getPackageManager();
        ComponentName name = new ComponentName(this, DefaultActivity.class);
        pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP);

        // terminate the activity.
        finish();
    }

    private void setupDefaultLauncher() {
        // remove this activity from the package manager.
        PackageManager pm = getPackageManager();

        String examplePackageName = "com.bluegod.launcher"; //请修改为需要设置的 package name
        String exampleActivityName = "com.android.launcher2.Launcher"; //请修改为需要设置的 launcher activity name

        ComponentName defaultLauncher = null;

        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);

        List<ResolveInfo> resolveInfoList =
                pm.queryIntentActivities(intent, 0);
        if (resolveInfoList != null) {
            int size = resolveInfoList.size();
            for (int j = 0; j < size; ) {
                final ResolveInfo r = resolveInfoList.get(j);
                if (!r.activityInfo.packageName.equals(examplePackageName)) {
                    resolveInfoList.remove(j);
                    size -= 1;
                } else {
                    j++;
                }
            }
            ComponentName[] set = new ComponentName[size];
            defaultLauncher = new ComponentName(examplePackageName, exampleActivityName);
            int defaultMatch = 0;
            for (int i = 0; i < size; i++) {
                final ResolveInfo resolveInfo =
                        resolveInfoList.get(i);
                Log.d(TAG, resolveInfo.toString());
                set[i] = new
                        ComponentName(resolveInfo.activityInfo.packageName, resolveInfo.activityInfo.name);
                if (defaultLauncher.getClassName().equals(resolveInfo.activityInfo.name)) {
                    defaultMatch = resolveInfo.match;
                }
            }
            Log.d(TAG, "defaultMatch =" + Integer.toHexString(defaultMatch));
            IntentFilter filter = new IntentFilter();
            filter.addAction(Intent.ACTION_MAIN);
            filter.addCategory(Intent.CATEGORY_HOME);
            filter.addCategory(Intent.CATEGORY_DEFAULT);

            pm.clearPackagePreferredActivities(defaultLauncher.getPackageName());
            pm.addPreferredActivity(filter, defaultMatch, set, defaultLauncher);
        }
    }
}

在setComponentEnabledSetting函数调用之前,新增setupDefaultLauncher()函数的调用。

然后修改packages/apps/Provision/AndroidManifest.xml文件,增加以下语句:
<uses-permission android:name=”android.permission.SET_PREFERRED_APPLICATIONS” />


2)修改框架文件修改
PackageManagerService.java

framework/base/services/java/com/android/server/pm/PackageManagerService.java该文件中找到 findPreferredActivity
方法
,将此方法的比较靠后的一段代码if (!pa.mPref.sameSet(query, priority)) { 添加如下语句

// Okay we found a previously set preferred app.
            // If the result set is different from when this
            // was created, we need to clear it and re-ask the
            // user their preference.
            /*if (!pa.mPref.sameSet(query, priority)) {
                Slog.i(TAG, "Result set changed, dropping preferred activity for "
                        + intent + " type " + resolvedType);
                pir.removeFilter(pa);
                return null;
            }*/

            if (!pa.mPref.sameSet(query, priority)) {
                if (!(intent.getAction() != null
                        && intent.getAction().equals(intent.ACTION_MAIN)
                        && intent.getCategories() != null
                        && intent.getCategories().contains(intent.CATEGORY_HOME))) //添加
                {  //添加这行
                    Slog.i(TAG, "Result set changed, dropping preferred activity: "
                            + intent + " type " + resolvedType);
                    pir.removeFilter(pa);
                    return null;
                }  //添加这行
                Slog.i(TAG, "Result set not change, not drop preferred activity special intent: " + intent);//添加这行
            }

替换上面/* */注释部分的代码,说白了主要增加一个if语句的判断

if (!(intent.getAction() != null
                        && intent.getAction().equals(intent.ACTION_MAIN)
                        && intent.getCategories() != null
                        && intent.getCategories().contains(intent.CATEGORY_HOME)))

如果非Launcher,则执行removeFilter()。

OK,经过以上两个修改,就可以做到开机后设置Google Launcher2作为默认启动的Launcher

原文:http://www.xuebuyuan.com/2141597.html

相关文章推荐

Android Launcher分析和修改1——Launcher默认界面配置(default_workspace)

最近工作都在修改Launcher,所以打算把分析源码和修改源码的过程记录下来,最近会写一些关于Launcher的分析和修改博文。因为我是修改4.0.3的Launcher,所以后面文章里面的Launch...

Android设置默认Launcher

当系统存在多个launcher时,若没有设置默认launcher,开机启动后会弹出提示框,罗列所有launcher,用户选择并设置了默认launcher后,按home键以及以后重启都会进入默认的lau...

当系统存在多个Launcher时,如何设置开机自动进入默认的Launcher?

Platform:MT6572MT6589MT6582MT6571...MT6572MT6589MT6582MT6571MT6582/92+MT6290MT6588MT6592MT6595MT6732...

android ROM设置默认Launcher(主屏幕应用)

LauncherLauncher是android系统的桌面、是android系统的主要组件。android系统允许存在多个Launcher并设置默认主界面。 应用程序作为Home(主界面)需在Ac...

Android设置默认Launcher

当系统存在多个launcher时,若没有设置默认launcher,开机启动后会弹出提示框,罗列所有launcher,用户选择并设置了默认launcher后,按home键以及以后重启都会进入默认的lau...

Android 实现双Launcher的无缝切换

当系统存在多个Launcher的时候 ,点击图标进行切换,这里提供三种方式 进去处理 注意前提条件是具备下面事项,需要系统级别的权限     package="com.rtkj.switchlaun...

Android开机第一个Launcher

Android开机第一个Launcher应该是Provision,其AndroidManifest.xml文件为:

android 当系统存在多个Launcher时,如何设置开机自动进入默认的Launcher?

面以设置Google Launcher2作为默认启动的Launcher为例,其包名为com.android.launcher2 编辑packages/apps/Provision/src/com/a...

Android系统存在多个Launcher时,设置开机进入默认的Launcher

问题提出: 当系统存在多个Launcher时,开机完成并解锁后,系统会弹出一个选择框让用户选择 进入哪一个Launcher,如果不想选择,而是想直接进入某一个默认的Launcher,并且 用户在...

Android系统:当系统存在多个Launcher时,如何设置开机自动进入默认的Launcher?

开机完成并解锁后,如果系统存在多个Launcher,系统会弹出一个选择框让用户选择进入某个Launcher。如果用户不想选择,而是想直接进入某一个默认的Launcher,要怎么修改? 如果没有预置G...
  • lyjIT
  • lyjIT
  • 2017-01-12 13:29
  • 610
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)