Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2437782
  • 博文数量: 293
  • 博客积分: 2660
  • 博客等级: 少校
  • 技术积分: 3632
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-03 17:50
文章分类

全部博文(293)

文章存档

2015年(13)

2014年(58)

2013年(73)

2012年(25)

2011年(30)

2010年(86)

2009年(8)

分类: Android平台

2013-10-02 10:28:10

 http://blog.csdn.net/xianming01/article/details/7678050
http://blog.csdn.net/qinjuning/article/details/7197359
本文将网络中的《android手把手教你开发launcher》系列文章进行了整理。这篇文章对lancher的基本功能点的实现做了简单介绍,看完后会对lancher有比较深刻的认识。

1、launcher最简单实例

launcher,也就是android的桌面应用程序。下图是android2.3的launcher应用程序:

接下来我们要开发一个自己的launcher,使其替代系统的默认launcher。
怎样使我们的应用程序成为一个launcher?

下面我们就新建一个叫做MyHome的工程,具体步骤略。创建完工程后整个目录结构如下图:

现在我们的AndroidManifest.xml文件这样的:


[java]
  1. "1.0" encoding="utf-8"?>  
  2. ""  
  3.       package="org.bangchui.myhome"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.    
  7.     "@drawable/icon" android:label="@string/app_name">  
  8.         ".MyHome"  
  9.                   android:label="@string/app_name">  
  10.               
  11.                 "android.intent.action.MAIN" />  
  12.                 "android.intent.category.LAUNCHER" />  
  13.               
  14.           
  15.    
  16.       
  17.   
请注意
里面的内容。
下面我们在其中添加上以下两行:
[java]
  1. "android.intent.category.HOME" />  
  2. "android.intent.category.DEFAULT" />  
此时AndroidManifest.xml文件是这样:


[html]
  1. xml version="1.0" encoding="utf-8"?>   
  2. <manifest xmlns:android=""        
  3.         package="org.bangchui.myhome"        
  4.         android:versionCode="1"        
  5.         android:versionName="1.0">           
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">           
  7.         <activity android:name=".MyHome"                    
  8.             android:label="@string/app_name">               
  9.             <intent-filter>                   
  10.                 <action android:name="android.intent.action.MAIN" />                   
  11.                 <category android:name="android.intent.category.LAUNCHER" />                   
  12.                 <category android:name="android.intent.category.HOME" />                   
  13.                 <category android:name="android.intent.category.DEFAULT" />               
  14.             intent-filter>           
  15.         activity>         
  16.     application>   
  17. manifest>  


此时运行程序,我们看不到任何特别之处。当按下home键时(模拟器上按下home会调出桌面应用),程序如图:


我们看到了,我们开发的Myhome跟Launcher出现在了一起。 
重启模拟器,我们看到我们自己的程序已经可以作为home来运行了。
ok。 第一步完成了:把我们的应用程序作为home。
总结一下:要把我们的应用程序作为home,只需要在AndroidManifest.xml中添加:

[java]
  1. "android.intent.category.HOME" />  
  2. "android.intent.category.DEFAULT" />  

2、列出安装的应用程序


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

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


[html]
  1. xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android=""      
  3.                   android:orientation="vertical" android:layout_width="fill_parent"      
  4.                   android:layout_height="fill_parent">          
  5.         <GridView android:layout_width="match_parent"           
  6.                             android:id="@+id/apps_list"          
  7.                             android:numColumns="4"          
  8.                             android:layout_height="wrap_content">      
  9.         GridView>  
  10. LinearLayout>  
二 . 通过PackageManager的api 查询已经安装的apk
我们写一个叫做loadApps的方法将活得的应用程序列表放到private List mApps; 中,如下:
[java]
  1. private void loadApps() {           
  2.         Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);           
  3.         mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);             
  4.         mApps = getPackageManager().queryIntentActivities(mainIntent, 0);       
  5.     }  
三. 实现用于显示Gridview的Adapter,使其显示获得的应用程序列表
最后整个Activity的代码如下



[java]
  1. package org.bangchui.myhome;       
  2. import java.util.List;       
  3. import android.app.Activity;   
  4. import android.content.Intent;   
  5. import android.content.pm.ResolveInfo;   
  6. import android.os.Bundle;   
  7. import android.view.View;   
  8. import android.view.ViewGroup;   
  9. import android.widget.BaseAdapter;   
  10. import android.widget.GridView;   
  11. import android.widget.ImageView;       
  12. public class MyHome extends Activity   
  13. {         
  14.     GridView mGrid;             
  15.     /** Called when the activity is first created. */      
  16.     @Override    public void onCreate(Bundle savedInstanceState) {           
  17.         super.onCreate(savedInstanceState);                     
  18.         loadApps();           
  19.         setContentView(R.layout.main);           
  20.         mGrid = (GridView) findViewById(R.id.apps_list);           
  21.         mGrid.setAdapter(new AppsAdapter());       
  22.     }                   
  23.     private List mApps;           
  24.     private void loadApps() {           
  25.         Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);           
  26.         mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);              
  27.         mApps = getPackageManager().queryIntentActivities(mainIntent, 0);      
  28.     }           
  29.     public class AppsAdapter extends BaseAdapter   
  30.     {           
  31.         public AppsAdapter() {         }               
  32.         public View getView(int position, View convertView, ViewGroup parent) {               
  33.             ImageView i;                   
  34.             if (convertView == null) {                   
  35.                 i = new ImageView(MyHome.this);                   
  36.                 i.setScaleType(ImageView.ScaleType.FIT_CENTER);                   
  37.                 i.setLayoutParams(new GridView.LayoutParams(5050));               
  38.             } else {                   
  39.                 i = (ImageView) convertView;               
  40.             }                   
  41.             ResolveInfo info = mApps.get(position);              
  42.             i.setImageDrawable(info.activityInfo.loadIcon(getPackageManager()));                   
  43.             return i;           
  44.         }                   
  45.         public final int getCount() {              
  46.             return mApps.size();           
  47.         }               
  48.         public final Object getItem(int position) {               
  49.             return mApps.get(position);           
  50.         }               
  51.         public final long getItemId(int position) {               
  52.             return position;           
  53.         }       
  54.     }   
  55. }  
3、启动安装的应用程序
1. 监听GridView的onItemClick事件
设置一个监听器是为了当gridView的某项被点击时,会有一个回调函数通知我们。
我们调用mGrid.setOnItemClickListener(listener); 设置一个监听器
mGrid.setOnItemClickListener(listener)中的listener是一个接口,其类型为:android.widget.AdapterView.OnItemClickListener,如下图所示:

下面我们new一个android.widget.AdapterView.OnItemClickListener类型的对象作为参数。我们直接使用eclipde的自动补全功能来完成OnItemClickListener 的定义:



[java]
  1. private OnItemClickListener listener = new OnItemClickListener()   
  2. {  
  3.     @Override public void onItemClick(AdapterView parent, View view, int position,long id) {}  
  4.       
  5. };  
接口OnItemClickListener 中有一个方法叫做onItemClick,我们实现它即可。下面我对onItemClick的几个参数略作说明:
parent     略
view          被点击的view
position     被点击项的位置
id             被点击项的id
2.启动被点击应用的activity
一般来讲,我们根据position即可知道被点击的项目是哪一项了。现在我们根据被点击的项目,取出对应的应用程序数据(主要是其中的主activity),然后启动activity。用下面代码实现:



[java]
  1. @Override public void onItemClick(AdapterView parent, View view, int position,long id) {            
  2.     ResolveInfo info = mApps.get(position);                          
  3.     //该应用的包名            String pkg = info.activityInfo.packageName;               
  4.     //应用的主activity类            String cls = info.activityInfo.name;                           
  5.     ComponentName componet = new ComponentName(pkg, cls);                          
  6.     Intent i = new Intent();              
  7.     i.setComponent(componet);              
  8.     startActivity(i);          
  9. }  
例如,我们点击计算器时,启动了计算器,如下图:

现在整个类代码如下:
[java]
  1. package org.bangchui.myhome;  
  2.   
  3. import java.util.List;  
  4.   
  5. import android.app.Activity;  
  6. import android.content.ComponentName;  
  7. import android.content.Intent;  
  8. import android.content.pm.ResolveInfo;  
  9. import android.os.Bundle;  
  10. import android.view.View;  
  11. import android.view.ViewGroup;  
  12. import android.widget.AdapterView;  
  13. import android.widget.BaseAdapter;  
  14. import android.widget.GridView;  
  15. import android.widget.ImageView;  
  16. import android.widget.AdapterView.OnItemClickListener;  
  17.   
  18. public class MyHome extends Activity {  
  19.     private List mApps;  
  20.     GridView mGrid;  
  21.     private OnItemClickListener listener = new OnItemClickListener() {  
  22.         @Override  
  23.         public void onItemClick(AdapterView parent, View view, int position,long id) {  
  24.             ResolveInfo info = mApps.get(position);  
  25.               
  26.             //该应用的包名   
  27.             String pkg = info.activityInfo.packageName;  
  28.             //应用的主activity类   
  29.             String cls = info.activityInfo.name;  
  30.               
  31.             ComponentName componet = new ComponentName(pkg, cls);  
  32.               
  33.             Intent i = new Intent();  
  34.             i.setComponent(componet);  
  35.             startActivity(i);  
  36.         }  
  37.   
  38.     };  
  39.   
  40.     /** Called when the activity is first created. */  
  41.     @Override  
  42.     public void onCreate(Bundle savedInstanceState) {  
  43.         super.onCreate(savedInstanceState);  
  44.   
  45.         loadApps();  
  46.         setContentView(R.layout.main);  
  47.         mGrid = (GridView) findViewById(R.id.apps_list);  
  48.         mGrid.setAdapter(new AppsAdapter());  
  49.   
  50.         mGrid.setOnItemClickListener(listener);  
  51.     }  
  52.   
  53.   
  54.     private void loadApps() {  
  55.         Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);  
  56.         mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);  
  57.   
  58.         mApps = getPackageManager().queryIntentActivities(mainIntent, 0);  
  59.     }  
  60.   
  61.     public class AppsAdapter extends BaseAdapter {  
  62.         public AppsAdapter() {  
  63.         }  
  64.   
  65.         public View getView(int position, View convertView, ViewGroup parent) {  
  66.             ImageView i;  
  67.   
  68.             if (convertView == null) {  
  69.                 i = new ImageView(MyHome.this);  
  70.                 i.setScaleType(ImageView.ScaleType.FIT_CENTER);  
  71.                 i.setLayoutParams(new GridView.LayoutParams(5050));  
  72.             } else {  
  73.                 i = (ImageView) convertView;  
  74.             }  
  75.   
  76.             ResolveInfo info = mApps.get(position);  
  77.             i.setImageDrawable(info.activityInfo.loadIcon(getPackageManager()));  
  78.   
  79.             return i;  
  80.         }  
  81.   
  82.         public final int getCount() {  
  83.             return mApps.size();  
  84.         }  
  85.   
  86.         public final Object getItem(int position) {  
  87.             return mApps.get(position);  
  88.         }  
  89.   
  90.         public final long getItemId(int position) {  
  91.             return position;  
  92.         }  
  93.     }  
  94. }  
4、显示widget


我们要达到这样的效果:点击“add widget” 后弹出widget列表,之后选择一个widget后显示在界面上,如下:

1. 获取widget信息
获取widget其实非常简单,我们只需要发送一个请求到系统,系统就会打开widget的列表,然后我们选择一个即可。代码如下:


[java]
  1. void addWidget() {           
  2.     int appWidgetId = mAppWidgetHost.allocateAppWidgetId();           
  3.     Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);           
  4.     pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);           
  5.     // start the pick activity            
  6.     startActivityForResult(pickIntent, [b]REQUEST_PICK_APPWIDGET[/b]);       
  7. }  
2. 添加widget的view到layout中
当选择一个widget后会通过onActivityResult 通知到activity,widget的信息被包含在 Intent data中,详情看代码注释



[java]
  1. @Override   protected void onActivityResult(int requestCode, int resultCode, Intent data) {           
  2.     // The pattern used here is that a user PICKs a specific application,            
  3.     // which, depending on the target, might need to CREATE the actual            
  4.     // target.              
  5.     // For example, the user would PICK_SHORTCUT for "Music playlist", and            
  6.     // we            
  7.     // launch over to the Music app to actually CREATE_SHORTCUT.              
  8.     if (resultCode == RESULT_OK) {               
  9.         switch (requestCode) {               
  10.         case REQUEST_PICK_APPWIDGET:                   
  11.             addAppWidget(data);                   
  12.             break;               
  13.         case REQUEST_CREATE_APPWIDGET:                   
  14.             completeAddAppWidget(data);                   
  15.             break;                 
  16.         }           
  17.     }       
  18. }         
  19. void addAppWidget(Intent data) {           
  20.     // TODO: catch bad widget exception when sent            
  21.     int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);           
  22.     AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);             
  23.     //widget 包含设置信息不为空,则启动widget的设置界面           
  24.     if (appWidget.configure != null) {               
  25.         // Launch over to configure widget, if needed                
  26.         Intent intent = new Intent(                       
  27.                 AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);               
  28.         intent.setComponent(appWidget.configure);               
  29.         intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);                 
  30.         startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);           
  31.     } else {           
  32.         //    widget 包含设置信息为空,直接添加widget到layout中               
  33.         // Otherwise just add it                
  34.         onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);           
  35.         }       
  36. }         
  37. void startActivityForResultSafely(Intent intent, int requestCode) {           
  38.     try {               
  39.         startActivityForResult(intent, requestCode);           
  40.         } catch (ActivityNotFoundException e) {               
  41.             Toast.makeText(this"activity_not_found", Toast.LENGTH_SHORT).show();           
  42.         } catch (SecurityException e) {               
  43.             Toast.makeText(this"activity_not_found", Toast.LENGTH_SHORT).show();           
  44.         }       
  45. }   
  46. /**      * 添加widget信息到layout中        
  47.  * * @param data 包含了widget的信息      */      
  48. private void completeAddAppWidget(Intent data) {           
  49.     Bundle extras = data.getExtras();           
  50.     int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);             
  51.     Log.d(TAG, "dumping extras content=" + extras.toString());             
  52.     AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);             
  53.     // Perform actual inflation because we're live            
  54.     synchronized (mLock) {                             
  55.         //获取显示widget的view                
  56.         mHostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);               
  57.         mHostView.setAppWidget(appWidgetId, appWidgetInfo);                 
  58.         //将获取的view添加早layout中               
  59.         LayoutParams lp = new LinearLayout.LayoutParams(appWidgetInfo.minWidth, appWidgetInfo.minHeight);               
  60.         mainLayout.addView(mHostView, lp);                 
  61.         mHostView.requestLayout();           
  62.     }      
  63. }  
5、显示和设置壁纸


显示壁纸也是launcher必不可少的功能,下面我们看看如何让我们开发的launcher来显示壁纸。
新建一个叫做ShowWallpaper的工程,具体步骤略。
一. 显示壁纸
要在我们的activity里显示一个壁纸非常简单(包括动态壁纸也如此),我们只需要定义一个theme使其继承自android:Theme.Wallpaper,然后在activity中使用这个theme就ok了。
在res/valuse下面增加一个xml文件,其名称为styles.xml ,内容如下:


[html]
  1. <resources>       
  2.     <style name="Theme" parent="android:Theme.Wallpaper">           
  3.               
  4.         <item name="android:windowNoTitle">trueitem>       
  5.     style>   
  6. resources>  
此时整个工程的结果如下:


下面在AndroidManifest.xml中使用这个theme,如下代码所示:


[html]
  1. xml version="1.0" encoding="utf-8"?>   
  2. <manifest xmlns:android=""        
  3.     package="com.test"        
  4.     android:versionCode="1"        
  5.     android:versionName="1.0">       
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">           
  7.         <activity android:name=".ShowWallpaper"                       
  8.             android:theme="@style/Theme"                    
  9.             android:label="@string/app_name">               
  10.             <intent-filter>                   
  11.                 <action android:name="android.intent.action.MAIN" />                   
  12.                 <category android:name="android.intent.category.LAUNCHER" />               
  13.          intent-filter>           
  14.      activity>           
  15.     application>  
  16. manifest>  
好了,运行程序,可以看到壁纸的显示效果了:(显示的是预设置的动态壁纸:星系)


用代码设置壁纸也是非常地简单的事,我们只需要向系统发送一个“设置请求”就足够了,其它的事情系统处理。

用下面代码表示:


[java]
  1. public void onSetWallpaper(View view) {                           
  2.     //生成一个设置壁纸的请求                 
  3.     final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);                   
  4.     Intent chooser = Intent.createChooser(pickWallpaper,"chooser_wallpaper");                   
  5.     //发送设置壁纸的请求                    
  6.     startActivity(chooser);       
  7. }  
为了调用上面这段代码,我们在xml中添加一个button,并设置回调函数,如下图:
[html]
  1. xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android=""  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="@string/hello" />  
  11.   
  12.     <Button  
  13.         android:id="@+id/button1"  
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="wrap_content"  
  16.         android:text="setWallpaper"  
  17.         android:onClick="onSetWallpaper" />  
  18.   
  19. LinearLayout>  
最后运行代码,步骤如下图所示:


设置壁纸后:

阅读(20264) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~