• Android Launcher简易Launcher开发


     第一步,把我们的应用程序作为home(即实现按下home键,启动自己的launcher.)

    要把我们的应用程序作为home,只需要在AndroidManifest.xml中添加:
     <category android:name="android.intent.category.HOME" />
     <category android:name="android.intent.category.DEFAULT" />

    AndroidManifest.xml
    
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="org.bangchui.myhome"
          android:versionCode="1"
          android:versionName="1.0">
    
        <application android:icon="@drawable/icon" android:label="@string/app_name">
            <activity android:name=".MyHome"
                      android:label="@string/app_name">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                    <category android:name="android.intent.category.HOME" />
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
            </activity>
    
        </application>
    </manifest>

    第二步,列出安装的应用程序

    列出已经安装的应用程序是作为launcher比不可少的功能。下面我们就讲解怎样将应用程序列出来。程序运行后的样子如下:

    1. 修改main.xml,在其中添加一个GridView用来显示应用程序列表。

    main.xml 
    
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="fill_parent">
         
        <GridView android:layout_width="match_parent"
            android:id="@+id/apps_list"
            android:numColumns="4"
            android:layout_height="wrap_content">
        </GridView>
     
    </LinearLayout

    2 . 通过PackageManager的api 查询已经安装的apk

    privatevoid loadApps() {
    Intent mainIntent
    =new Intent(Intent.ACTION_MAIN, null);
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);

    mApps
    = getPackageManager().queryIntentActivities(mainIntent, 0);
    }

    3. 实现用于显示Gridview的Adapter,使其显示获得的应用程序列表

    public class AppsAdapter extends BaseAdapter {
            public AppsAdapter() {
            }
     
            public View getView(int position, View convertView, ViewGroup parent) {
                ImageView i;
     
     
                if (convertView == null) {
                    i = new ImageView(MyHome.this);
                    i.setScaleType(ImageView.ScaleType.FIT_CENTER);
                    i.setLayoutParams(new GridView.LayoutParams(50, 50));
                } else {
                    i = (ImageView) convertView;
                }
     
                ResolveInfo info = mApps.get(position);
                i.setImageDrawable(info.activityInfo.loadIcon(getPackageManager()));
     
                return i;
            }
     
            public final int getCount() {
                return mApps.size();
            }
     
            public final Object getItem(int position) {
                return mApps.get(position);
            }
     
            public final long getItemId(int position) {
                return position;
            }

    4.监听GridView的onItemClick事件

    设置一个监听器是为了当gridView的某项被点击时,会有一个回调函数通知我们。
    我们调用mGrid.setOnItemClickListener(listener); 设置一个监听器
    mGrid.setOnItemClickListener(listener)中的listener是一个接口,其类型为:android.widget.AdapterView.OnItemClickListener

    private OnItemClickListener listener =new OnItemClickListener() {
    @Override
    publicvoid onItemClick(AdapterView<?> parent, View view, int position,long id) {
    //事件内容....
    }
    };

    5.启动被点击应用的activity

    一般来讲,我们根据position即可知道被点击的项目是哪一项了。现在我们根据被点击的项目,取出对应的应用程序数据(主要是其中的主activity),然后启动activity。用下面代码实现:

    复制代码
    publicvoid onItemClick(AdapterView<?> parent, View view, int position,long id) {
    ResolveInfo info
    = mApps.get(position);

    //该应用的包名
    String pkg = info.activityInfo.packageName;
    //应用的主activity类
    String cls = info.activityInfo.name;

    ComponentName componet
    =new ComponentName(pkg, cls);

    Intent i
    =new Intent();
    i.setComponent(componet);
    startActivity(i);
    }
    复制代码

    最后整个Activity的代码如下:

    MyHome.java
    
    package org.bangchui.myhome;
    
    import java.util.List;
    
    import android.app.Activity;
    import android.content.ComponentName;
    import android.content.Intent;
    import android.content.pm.ResolveInfo;
    import android.os.Bundle;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.BaseAdapter;
    import android.widget.GridView;
    import android.widget.ImageView;
    import android.widget.AdapterView.OnItemClickListener;
    
    public class MyHome extends Activity {
        private List<ResolveInfo> mApps;
        GridView mGrid;
        private OnItemClickListener listener = new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
                ResolveInfo info = mApps.get(position);
                
                //该应用的包名
                String pkg = info.activityInfo.packageName;
                //应用的主activity类
                String cls = info.activityInfo.name;
                
                ComponentName componet = new ComponentName(pkg, cls);
                
                Intent i = new Intent();
                i.setComponent(componet);
                startActivity(i);
            }
    
        };
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            loadApps();
            setContentView(R.layout.main);
            mGrid = (GridView) findViewById(R.id.apps_list);
            mGrid.setAdapter(new AppsAdapter());
    
            mGrid.setOnItemClickListener(listener);
        }
    
    
        private void loadApps() {
            Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
            mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    
            mApps = getPackageManager().queryIntentActivities(mainIntent, 0);
        }
    
        public class AppsAdapter extends BaseAdapter {
            public AppsAdapter() {
            }
    
            public View getView(int position, View convertView, ViewGroup parent) {
                ImageView i;
    
                if (convertView == null) {
                    i = new ImageView(MyHome.this);
                    i.setScaleType(ImageView.ScaleType.FIT_CENTER);
                    i.setLayoutParams(new GridView.LayoutParams(50, 50));
                } else {
                    i = (ImageView) convertView;
                }
    
                ResolveInfo info = mApps.get(position);
                i.setImageDrawable(info.activityInfo.loadIcon(getPackageManager()));
    
                return i;
            }
    
            public final int getCount() {
                return mApps.size();
            }
    
            public final Object getItem(int position) {
                return mApps.get(position);
            }
    
            public final long getItemId(int position) {
                return position;
            }
        }
    }

    转载自:http://www.cnblogs.com/playing/archive/2011/04/13/2014705.html

  • 相关阅读:
    HTML
    数据挖掘之分类——基于规则的分类器
    Ubuntu 14.04下Hadoop2.4.1集群安装配置教程
    Jdk1.7+eclipse搭建Java开发环境
    约瑟夫环问题
    Linux 绝对路径与相对路径
    排序算法汇总
    朋友圈问题
    HTTP状态码
    哈希冲突,哈希函数
  • 原文地址:https://www.cnblogs.com/sy171822716/p/2915297.html
Copyright © 2020-2023  润新知