`

change default Home Application

 
阅读更多

from: http://stackoverflow.com/questions/3836215/android-change-default-home-application

Method installPackageMethod = null;
Method deletePackageMethod = null;
Method setPreferredActivityMethod = null;
Method replacePreferredActivityMethod = null;
Object pm = null;


    @Override
public void onCreate() {
    super.onCreate();
        if (pm == null)
        pm = getPackageManager();
    try {
        if (setPreferredActivityMethod == null)
            setPreferredActivityMethod = pm.getClass().getMethod(
                    "addPreferredActivity", IntentFilter.class, int.class,
                    ComponentName[].class, ComponentName.class);
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    }
}


private final class ServiceHandler extends Handler {
    private Context context;

    public ServiceHandler(Looper looper, Context ctx) {
        super(looper);
        context = ctx;
    }

    @Override
    public void handleMessage(Message msg) {
        Intent intent = (Intent) msg.getData().getParcelable(
                UPDATER_SERVICE_ACTION);
        int request = intent.getIntExtra(
                REQUEST_KEY,
                REQUEST_UNKNOWN);
        Bundle bundle = intent.getExtras();

        switch (request) {
        case INSTALL_APPLICATION: {
            if (bundle != null) {
                String appPath = bundle
                        .getString(APP_PATH_KEY);
                if (appPath != null) {
                    LogUtil.e(TAG, "try to install " + appPath);

                    try {
                        am.installPackage(appPath);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    LogUtil.e(TAG, "install of " + appPath + " done");
                }
            }
            break;
        }
        case UNISTALL_PACKAGE: {
            if (bundle != null) {
                String packagename = bundle
                        .getString(PACKAGE_NAME_KEY);
                if (packagename != null) {
                    LogUtil.e(TAG, "unistall " + packagename);

                    try {
                        deletePackageMethod
                                .invoke(pm, packagename, null, 0);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
            break;
        }
        case SET_PREFERRED_LAUNCHER: {
            if (bundle != null) {
                String package_name = bundle
                        .getString(PREFERRED_PACKAGE_KEY);
                if (package_name == null) {
                    LogUtil.e(TAG,
                            "WARNING: setDefaultActivity cannot continue, package is NULL");
                    return;
                }
                String activity_name = bundle
                        .getString(PREFERRED_ACTIVITY_KEY);
                if (activity_name == null) {
                    LogUtil.e(TAG,
                            "WARNING: setDefaultActivity cannot continue, activity is NULL");
                    return;
                }

                LogUtil.e(TAG, "setDefaultActivity activity="
                        + activity_name + " package=" + package_name);

                IntentFilter filter = new IntentFilter(
                        "android.intent.action.MAIN");
                filter.addCategory("android.intent.category.HOME");
                filter.addCategory("android.intent.category.DEFAULT");
                ComponentName[] components = new ComponentName[] {
                        new ComponentName("com.android.launcher",
                                "com.android.launcher2.Launcher"),
                        new ComponentName(package_name, activity_name) };
                ComponentName activity = new ComponentName(package_name,
                        activity_name);
                try {
                    setPreferredActivityMethod.invoke(pm, filter,
                            IntentFilter.MATCH_CATEGORY_EMPTY, components,
                            activity);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        }

    }

}
分享到:
评论

相关推荐

    Whm-1.1.88.tar.gz

    If operator forgets his password then administrator can change it to default password. i.e. op which is defined in this application. Logs ---- This application also logs Log in time and log out time ...

    pyloris-3.2-win32

    Using the Terminal Application: hotdog:/Users/Motoma/ motoma$ python pyloris-3.0.py motomastyle.com Using HTTPLoris in Windows is a little different. One will need to know the location of the ...

    apktool documentation

    This is a big change from 1.4.2. Please read the smali updates here for more information -o / --output is now used for the output of apk/directory -t / --tag is required for tagging framework files...

    IBM下weblogic-10.3安装和应用程序

    "BEA Home"=[Enter new value or use default "/bea/weblogic/bea"] ``` **操作指导:** - 按回车键继续使用默认值。 ##### 4. 选择产品组件 在此步骤,用户可以选择安装哪些 WebLogic 组件。每个组件都提供了相应...

    zigbee 入门-3

    Z-Stack支持多种Zigbee应用框架,例如Smart Energy Profile (SEP)、Home Automation (HA)、Commercial Building Automation (CBA)等。 #### 二、Sample Application详解 在Z-Stack CC2430 DB和CC2430 EB开发板上的...

    8-07-14_MegaCLI for linux_windows

    LSIP200232954 (DFCT) Need to Support all the MFC default values in the command AdpSettings. LSIP200245968 (DFCT) In EFICLI not able to flash latest firmware to controller without using -nosigchk -...

    VB编程资源大全(英文源码 控件)

    Add/delete entries from INI files, load pictures, change strings, dail phone numbers, send emails, view home pages, and more. Must see.<END><br>31 , fldrvw21.zip FolderView ActiveX Control 2.1 ...

    ehlib_vcl_src_9_3.26

    returns default PrinterPreview object and shows data in default preview form. TDBEditEh component represents a single or multi-line edit control that can display and edit a field in a dataset or ...

    EhLib 9.1.024

    returns default PrinterPreview object and shows data in default preview form. TDBEditEh component represents a single or multi-line edit control that can display and edit a field in a dataset or ...

    最全的oracle常用命令大全.txt

    SQL>select username,default_tablespace from user_users; 查看当前用户的角色 SQL>select * from user_role_privs; 查看当前用户的系统权限和表级权限 SQL>select * from user_sys_privs; SQL>select * ...

    UE(官方下载)

    A quick reference guide to UltraEdit's default keyboard shortcuts Keymapping and custom hotkeys How to customize 键映射s and menu hotkeys Column Markers The benefit of a column maker is that it can ...

    计算机网络第六版答案

    For example, she can easily change the phrase “Alice, I owe you $1000” to “Alice, I owe you $10,000”. Furthermore, Trudy can even drop the packets that are being sent by Bob to Alice (and vise-...

    adb1.0.26包含fastboot.exe

    -t 允许安装 AndroidManifest.xml 里 application 指定 android:testOnly="true" 的应用 -s 将应用安装到 sdcard -d 允许降级覆盖安装 -g 授予所有运行时权限 运行命令后如果见到类似如下输出(状态为 Success)代表...

    EhLib 8.0 Build 8.0.023 Pro Edition FullSource for D7-XE8

    returns default PrinterPreview object and shows data in default preview form. TDBEditEh component represents a single or multi-line edit control that can display and edit a field in a dataset or ...

    Senfore_DragDrop_v4.1

    If you can provide me with a minimal application which reproduces the problem, I can almost guarantee that I will be able to fix the problem in very short time. Please supply only the source files ...

    Visual C++ 编程资源大全(英文源码 表单)

    21.zip Parse phone fields 解析电话区域(11KB)<END><br>22,22.zip Changing the default file open/save dialogs in an MFC doc/view application 初始化对话框和支持动态数据交换(DDX)(15KB)<END><br>...

    EhLib 6.3 Build 6.3.176 Russian version. Full source included.

    returns default PrinterPreview object and shows data in default preview form. TDBEditEh component represents a single or multi-line edit control that can display and edit a field in a dataset or ...

    springboot整合vue实现上传下载文件

    private final static String rootPath = System.getProperty("user.home")+File.separator+fileDir+File.separator; @RequestMapping("/upload") public Object uploadFile(@RequestParam("file") ...

Global site tag (gtag.js) - Google Analytics