在android TV第一次上电时,会进行一个国家和语言及搜台的选择,实现的原理是:

写一个单独的apk实现相应的功能,把这个单独的apk设置成Launcher:



    在AndroidManifest.xml中,设置:


<application> 
 
 
         <activity android:name="DefaultActivity" 
 
                android:excludeFromRecents="true" 
 
                android:launchMode="singleInstance"> 
 
            <intent-filter android:priority="1"> 
 
                <action android:name="android.intent.action.MAIN" /> 
 
                <category android:name="android.intent.category.HOME" /> 
 
                <category android:name="android.intent.category.DEFAULT" /> 
 
            </intent-filter> 
 
        </activity> 
 
    </application>


那么开机时,理论上会有两个Launcher启动,用户可以选择,但这里设置了一个属性:intent-filter android:priority="1"





而默认的Launcher没设置这个属性:



< 
   activity
 
  
             
   android:name 
    = 
   "com.haier.haierlauncher2.LauncherActivity"
 
  
             
   android:label 
    = 
   "@string/app_name"
 
  
             
   android:launchMode 
    = 
   "singleInstance"
 
  
             
   android:theme 
    = 
   "@android:style/Theme.Wallpaper.NoTitleBar.Fullscreen"  
   >
 
  
             
   < 
    intent-filter 
   >
 
  
                 
   < 
    action 
     
   android:name 
    = 
   "android.intent.action.MAIN"  
   />
 
  

 
  
                 
   < 
    category 
     
   android:name 
    = 
   "android.intent.category.HOME"  
   />
 
  
                 
   < 
    category 
     
   android:name 
    = 
   "android.intent.category.DEFAULT"  
   />
 
  
                 
   < 
    category 
     
   android:name 
    = 
   "android.intent.category.MONKEY"  
   />
 
  
             
   </ 
    intent-filter 
   >
 
  
         
   </ 
    activity 
   >


没有设置这个属性值,默认为0,:intent-filter android:priority="0"





这个属性用于给过滤器设置一个优先级。其父组件能够通过优先级来依次处理过滤器所描述类型的Intent对象。这个属性对Activity和Broadcast Receiver对象有意义:



 



          1. 它提供了Activity能够如何响应跟过滤器匹配的Intent对象请求的信息(相对与其他的也能响应这个Intent请求的Activity)。当一个Intent对象请求能够被多个带有不同优先级的Activity处理时,Android系统只考虑把高优先级的Intent过滤器作为潜在的Intent对象请求目标。



 



          2. 它控制了Broadcast Receiver执行接收广播消息的顺序。高优先级的过滤器会优先调用。(这个顺序只应用与同步消息,对于异步消息会忽略优先级的设置。)



 



          只有真正想要给接收广播消息的过滤器施加一个特定顺序,或者要强制Android来应用另外一个Activity,才会使用这个属性。



 



          这个属性值必须是一个整数,如:100,数字越大,优先级越高。





那么第一次上电,优先启动的是这个Launcher,





设置完成之后,以后这个Launcher不再启动,可以这样做:


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

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


在用到组件时,有时候我们可能暂时性的不使用组件,但又不想把组件kill掉,比如创建了一个broadcastReceiver广播监听器,用来想监听第一次开机启动后获得系统的许多相关信息,并保存在文件中,这样以后每次开机启动就不需要再去启动该服务了,也就是说如果没有把receiver关闭掉,就算是不做数据处理,但程序却还一直在后台运行会消耗电量和内存,这时候就需要把这个receiver给关闭掉。 



 






如何关闭组件? 



 关闭组件其实并不难,只要创建packageManager对象和ComponentName对象,并调用packageManager对象的setComponentEnabledSetting方法。



 









public void setComponentEnabledSetting (ComponentName componentName, int newState, int flags)



 componentName:组件名称 



 newState:组件新的状态,可以设置三个值,分别是如下: 



 不可用状态:COMPONENT_ENABLED_STATE_DISABLED 



 可用状态:COMPONENT_ENABLED_STATE_ENABLED 



 默认状态:COMPONENT_ENABLED_STATE_DEFAULT 



 flags:行为标签,值可以是DONT_KILL_APP或者0。 0说明杀死包含该组件的app