Android笔记--动态设置Launcher

Posted ljt2724960661

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android笔记--动态设置Launcher相关的知识,希望对你有一定的参考价值。

          动态设置桌面Launcher,代码如下:

	//获取默认Launcher
    private fun getDefaultLauncher(context:Context) :String?
        val intentList = java.util.ArrayList<IntentFilter>()
        val cnList = java.util.ArrayList<ComponentName>()
        context.packageManager.getPreferredActivities(intentList, cnList, null)
        for (i in cnList.indices) 
            val iLCurrent = intentList[i]
            if (iLCurrent.hasAction(Intent.ACTION_MAIN) && iLCurrent.hasCategory(Intent.CATEGORY_HOME)) 
                //默认launcher
                try 
                    return cnList[i].packageName
                 catch (ex: Exception) 
                
            
        
        return null
    
	
	//清除默认launcher
	 private fun clearDefaultLauncher(context:Context) 
        val intentList = java.util.ArrayList<IntentFilter>()
        val cnList = java.util.ArrayList<ComponentName>()
        context.packageManager.getPreferredActivities(intentList, cnList, null)
        for (i in cnList.indices) 
            val iLCurrent = intentList[i]
            if (iLCurrent.hasAction(Intent.ACTION_MAIN) && iLCurrent.hasCategory(Intent.CATEGORY_HOME)) 
                //遍历过滤所有launcher
                try 
                    val name = cnList[i].packageName
                    //清除原有的默认launcher
                    context.packageManager.clearPackagePreferredActivities(name)
                 catch (ex: Exception) 
                
            
        
    
	
	
	//设置Launcher
	private fun setLauncher(pkg:String) 
        val set = arrayOfNulls<ComponentName>(mHomeList.size)
        var curLauncher:String
        var curLauncherActivity:String
        var launcher:ComponentName? = null
        val filter = IntentFilter()
        filter.addAction(Intent.ACTION_MAIN)
        filter.addCategory(Intent.CATEGORY_HOME)
        filter.addCategory(Intent.CATEGORY_DEFAULT)
        for(i in 0 until mHomeList.size) 
            val r: ResolveInfo = mHomeList.get(i)
            set[i] = ComponentName(r.activityInfo.packageName, r.activityInfo.name)
            if(r.activityInfo.packageName.equals(pkg.trim())) 
                curLauncher = r.activityInfo.packageName
                curLauncherActivity = r.activityInfo.name
                launcher = ComponentName(curLauncher, curLauncherActivity)
            
        
        launcher?.let 
            this.packageManager.addPreferredActivity(filter, 1081344, set, launcher)
        
    

以上是关于Android笔记--动态设置Launcher的主要内容,如果未能解决你的问题,请参考以下文章

android学习笔记 动态注册广播例程

Android逆向笔记-IDA Pro动态调试Android程序(真机)

Android知识体系梳理笔记三:动态代理模式---插件加载机制学习笔记

Android底层笔记:APP通过JNI调用动态库.so

Android进阶笔记热修复(代码资源动态链接库)

Android:安卓学习笔记之广播机制的简单理解和使用