Launcher的简单介绍

来源:互联网 发布:淘宝企业店铺缴税 编辑:程序博客网 时间:2024/06/16 00:37

Launcher

什么是Launcher

   Launcher是安卓系统中的桌面启动器,安卓系统的桌面UI统称为LauncherLauncher的启动实际上就是启动一个ActivityLauncher.java)。

当我们点击手机的home键时,实际上就是启动桌面管理器,会启动LaunCher.java Activity

我们也可以将自己的应用程序中的Activity作为Launcher,只要在我们的Activity注册时,在意图过滤器中添加categoryandroid:name="android.intent.category.HOME"android:name="android.intent.category.DEFAULT"这样当我们点击home键时会弹窗让我们选择开启那个Lanuncher

   

清单文件

<application

        android:allowBackup="true"

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@android:style/Theme.NoTitleBar.Fullscreen">

        <activity

            android:name=".MainActivity"

            android:label="@string/app_name">

            <intent-filter>

                <actionandroid:name="android.intent.action.MAIN"/>

                <categoryandroid:name="android.intent.category.LAUNCHER"/>

                <categoryandroid:name="android.intent.category.HOME"/> 

                <categoryandroid:name="android.intent.category.DEFAULT"/>

            </intent-filter>

        </activity>

</application>

 

 

 

//查询所有的应用程序并显示 ResolveInfo的使用和获取所有安装程序列表的实现

Activity:

 

publicclass ShowDevelopments extends Activity {

         private GridView show_developments;

         private List<ResolveInfo> info;

         @Override

         protected void onCreate(BundlesavedInstanceState) {

                   super.onCreate(savedInstanceState);

                   setContentView(R.layout.activity_developments);

                   show_developments=(GridView)findViewById(R.id.show_developments);

                   info=newArrayList<ResolveInfo>();

                   getData();

                   MyAdapter adapter=newMyAdapter(info);

                   show_developments.setAdapter(adapter);

                   show_developments.setOnItemClickListener(newAdapterView.OnItemClickListener() {

 

                            @Override

                            public voidonItemClick(AdapterView<?> parent, View view,

                                               intposition, long id) {

                                     start(position);

                            }

                   });

         }

         /**

          * 界面跳转的方法:ComponentName的使用

          */

         private void start(int i){

                   ResolveInfoinfo = this.info.get(i); 

       //该应用的包名 

       String pkg =info.activityInfo.packageName; 

       //应用的主activity 

       String cls =info.activityInfo.name; 

       ComponentName componet = newComponentName(pkg, cls); 

       Intent intent = new Intent(); 

       intent.setComponent(componet); 

       startActivity(intent);

         }

         /**

          * 获取所有的安装的应用程序

          */

         private void getData(){

                   IntentmainIntent = new Intent(Intent.ACTION_MAIN, null); 

           mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); 

//         new ImageView(ShowDevelopments.this); 

           info = getPackageManager().queryIntentActivities(mainIntent, 0);

         }

         private class MyAdapter extendsBaseAdapter{

                   privateList<ResolveInfo> info;

                   private LayoutInflaterinflater;

                   privateMyAdapter(List<ResolveInfo> info){

                            this.info=info;

                            inflater=getLayoutInflater();

                   }

                   @Override

                   public int getCount() {

                            returninfo!=null?info.size():0;

                   }

 

                   @Override

                   public Object getItem(intposition) {

                            returninfo.get(position);

                   }

 

                   @Override

                   public long getItemId(intposition) {

                            return position;

                   }

 

                   @Override

                   public View getView(intposition, View convertView, ViewGroup parent) {

                            View[] view=newView[2];

                            if(convertView==null){

                                     convertView=inflater.inflate(R.layout.item_showdata,null);

                                     view[0]=convertView.findViewById(R.id.show_image);

                                     view[1]=convertView.findViewById(R.id.chengxu);

                                     convertView.setTag(view);

                            }

                            view=(View[])convertView.getTag();

                            ((TextView)view[1]).setText(info.get(position).loadLabel(getPackageManager()));

                            ((ImageView)view[0]).setImageDrawable((info.get(position).loadIcon(getPackageManager())));

                            return convertView;

                   }                

         }

}

 

0 0
原创粉丝点击