Android Launcher--简易Launcher开发

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

55_2_48be3591a12df52.jpg

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

ContractedBlock.gif ExpandedBlockStart.gif 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比不可少的功能。下面我们就讲解怎样将应用程序列出来。程序运行后的样子如下:

55_2_3b434ff967012d6.jpg

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

ContractedBlock.gif ExpandedBlockStart.gif 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
 
  
private void loadApps() {
Intent mainIntent
= new Intent(Intent.ACTION_MAIN, null );
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);

mApps
= getPackageManager().queryIntentActivities(mainIntent, 0 );
}
3. 实现用于显示Gridview的Adapter,使其显示获得的应用程序列表
ContractedBlock.gif ExpandedBlockStart.gif View Code
 
   
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
public void onItemClick(AdapterView <?> parent, View view, int position, long id) {
// 事件内容....
}
};

5.启动被点击应用的activity

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

 
  
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);
}

最后整个Activity的代码如下:

ContractedBlock.gif ExpandedBlockStart.gif 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;
        }
    }
}

转载于:https://www.cnblogs.com/playing/archive/2011/04/13/2014705.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值