android create your own launcher

Introduction

In its most basic form, a launcher is an application that does the following:

  • it represents the home screen of a device
  • it lists and launches applications that are installed on the device

In
other words, it is the application that shows up when you press the
home button. Unless you‘ve already installed a custom launcher, you are
currently using the default launcher that comes with your Android
installation. A lot of device manufacturers have their own default,
custom launchers that conform to their proprietary look and feel, for
example, Samsung TouchWiz and HTC Sense.

In this tutorial, we are going to create a simple launcher with a basic user interface. It will have two screens:

  • a home screen showing the device‘s wallpaper
  • a screen showing the icons and details of the applications installed on the device

1. Requirements

You need to have the following installed and configured on your development machine:

  • Android SDK and platform tools
  • Eclipse IDE 3.7.2 or higher with the ADT plugin
  • an emulator or Android device running Android 2.2 or higher

You can download the SDK and platform tools the Android developer portal.

2. Project Setup

Launch Eclipse and create a new Android application project. I‘m naming the application SimpleLauncher, but you can name it anything you want. Make sure you use a unique package. The lowest SDK version our launcher supports is Froyo and the target SDK is Jelly Bean.

Since we don‘t want to create an Activity, deselect Create Activity. Click Finish to continue.

3. Project Manifest

The next step is modifying the AndroidManifest.xml file by adding two activities. The first Activity displays the home screen. Let‘s name it HomeActivity as shown below.

By adding the categories android.intent.category.HOME and android.intent.category.DEFAULT to the intent-filter group, the associated Activity behaves like a launcher and shows up as an option when you press the device‘s home button.

We also need to set the launchMode to singleTask to make sure that only one instance of this Activity is held by the system at any time. To show the user‘s wallpaper, set the theme to Theme.Wallpaper.NoTitleBar.FullScreen.

The second Activity we
need to add displays the applications that are installed on the user‘s
device. It‘s also responsible for launching applications. We don‘t need
any special configuration for this Activity. Name it AppsListActivity.

4. Activity Layouts

Create an XML file for the HomeActivity class in the project‘s res/layout folder and name it activity_home.xml. The layout has a single Button that responds to click events. Clicking the button takes the user from the home screen to the list of applications.

Next, create an XML file for the AppsListActivity class in the project‘s res/layout folder and name it activity_apps_list.xml. The layout contains a ListView that takes up the entire screen.

Finally, create a third XML file in the same location and name it list_item.xml. This file defines the layout of an item in the ListView.
Each list view item represents an application installed on the
user‘s device. It shows the application‘s icon, label, and package name.
We display the application icon using an ImageView instance and TextView instances for the label and package name.

5. Implementing the Activity Classes

HomeActivity

With the layouts of the application created, it‘s time to create the two Activity
classes. When creating the two classes, make sure the name of each
class matches the one you specified in the project‘s manifest file
earlier.

Create a new class named HomeActivity and set android.app.Activity as its superclass.

In the class‘s onCreate method, we invoke setContentView, passing in the layout we created earlier. You may remember that we added a button to the activity_home layout that triggers a method named showApps. We now need to implement that method in the HomeActivity class. The implementation is pretty simple, we create an Intent for the AppsListActivity class and start it.

AppsListActivity

Create another Activity class named AppsListActivity and set android.app.Activity as its superclass. In the class‘s onCreate method, we invoke setContentView, passing in the activity_apps_list layout we created earlier.

Even though our launcher isn‘t finished yet, you can save and run your
application at this point. When you press the device‘s home button, you
should see a pop-up asking you which launcher you‘d like to use.

If you choose Simple Launcher Home, you should see your new home
screen with a single button in the top right corner of the screen. You
should also see your device‘s current wallpaper.

Go back to Eclipse and create a class named AppDetail that
will contain the details of an application, its package name, label,
and application icon. The interface is pretty basic as you can see
below.

6. Fetching Applications

In the loadApps method of the AppsListActivity class, we use the queryIntentActivities method of the PackageManager class to fetch all the Intents that have a category of Intent.CATEGORY_LAUNCHER.
The query returns a list of the applications that can be launched by a
launcher. We loop through the results of the query and add each item to a
list named apps. Take a look at the following code snippet for clarification.

7. Displaying the List of Applications

With the apps variable containing all the details we need, we can show the list of applications using the ListView class. We create a simple ArrayAdapter and override its getView method to render the list‘s items. We then associate the ListView with the adapter.

8. Listening for Clicks

When the user clicks an item in the ListView, the corresponding application should be launched by our launcher. We use the getLaunchIntentForPackage method of the PackageManager class to create an Intent with which we start the application. Take a look at the following code snippet.

9. Putting It All Together

To make everything work together, we need to invoke loadApps, loadListView, and addClickListener in the onCreate method of the AppsListActivity class as shown below.

Build and run your application once more to see the result. You should
now be able to see the applications that can be launched when you click
the button on the home screen of our launcher. Click on an item to
launch the corresponding application.

Conclusion

You now have your own custom
launcher. It‘s very basic, but you can add all the customizations you
want. If you want to dig deeper into custom launchers, I encourage you
to take a look at the sample applications on the Android Developer Portal.

come from a overseas

时间: 2024-08-16 10:34:58

android create your own launcher的相关文章

Android Metro风格的Launcher开发系列第二篇

前言: 各位小伙伴们请原谅我隔了这么久才开始写这一系列的第二篇博客,没办法忙新产品发布,好了废话不说了,先回顾一下:在我的上一篇博客Android Metro风格的Launcher开发系列第一篇写了如何配置Android开发环境,只是用文字和图片展示了开发Metro风格Launcher的初步设计和产品要求,这一篇文章将会从代码上讲解如何实现对应的UI效果,好了,评书开讲! Launcher主体框架实现: Launcher主体框架我选用的是大家所熟悉的ViewPager控件,因为ViewPager

【转载】Android Metro风格的Launcher开发系列第二篇

前言: 各位小伙伴们请原谅我隔了这么久才开始写这一系列的第二篇博客,没办法忙新产品发布,好了废话不说了,先回顾一下:在我的上一篇博客Android Metro风格的Launcher开发系列第一篇写了如何配置Android开发环境,只是用文字和图片展示了开发Metro风格Launcher的初步设计和产品要求,这一篇文章将会从代码上讲解如何实现对应的UI效果,好了,评书开讲! Launcher主体框架实现: Launcher主体框架我选用的是大家所熟悉的ViewPager控件,因为ViewPager

Android Metro风格的Launcher开发系列第三篇

前言: 各位小伙伴,又到了每周更新文章了时候了,本来是周日能发出来呢,这不是赶上清明节吗,女王大人发话了,清明节前两天半陪她玩,只留给我周一下午半天时间写博客,哪里有女王哪里就有压迫呀有木有!好了闲话少说,上一篇博客(Android Metro风格的Launcher开发系列第二篇)说到Launcher主体框架用ViewPager来实现,这一篇博客咱们来说说每一个page的具体实现. PagerAdapter: Launcher主体ViewPager实现就引出了PagerAdapter,Pager

Android中源码Launcher主屏幕程序排列详解【安卓Launcher进化一】

最近研究Lancher,从短信Mms的框架中过度到Launcher的bug和需求修改中,下面对launcher最简单的主屏幕程序的程序的布局的详 解,给读者一个入门的感觉,android的主屏幕一共分为5个屏幕,从左到右依次是:0,1,2,3,4.所以想在那个主屏幕放哪些程序或者 widget都可以在布局文件中实现,下面我就说说这个布局文件default_workspace.xml.这是对主屏幕进行排列的布局文件,在这里面 写好,程序运行后就可以实现哪个屏幕放那些特定的程序或widget了.下面

关于android使用自己的launcher替换默认launcher的方法

关于android使用自己的launcher替换默认launcher的方法 有时候,你可能希望用你自己的launcher程序替换默认的android launcher2和launcher3.而且希望作为系统起来就启动你的launcher程序,而不需要用户选择. 特别是如果你的launcher程序和默认launcher程序共存时,即使你的launcher已经被用户选择为默认启动launcher,突然运行时,你的程序出现crash,那么系统会再次弹出选择哪个launcher进行启动的选择框,而不是直

Android自定义应用抽屉Launcher

可以根据应用名称和安装时间进行排序,工程具体代码可以在我的GitHub上查看,链接见文末,效果图: 核心代码: package com.zms.launcher; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import android.app.Activity; import android.app.AlertDialog; import android.content.Activit

android编程常见问题-No Launcher activity found!

新手编程常见的问题: 问题表现: console提示:No Launcher activity found! The launch will only sync the application package on the device! 解决办法:检查AndroidManifest.xml中是否添加如下代码: <intent-filter > <action android:name="android.intent.action.MAIN"/> <cat

Android应用设置为Launcher时onCreate启动俩次的问题

这几天有个需求就是把自己的应用程序要设置为Launcher的形式,设置后onCreate就会启动俩次,在网上找了资料后说是横竖屏设置造成的先说结果:在MainAcitivity里设置android:configChanges="mcc|mnc"就能解决问题的. 今天在看log的时候发现,Launcher activity会被onDestroy掉一次,然后再重启. 可能原因猜测: 1.横竖屏切换 2.MCC MNC等Configuration改变引起的 MCC(移动国家码)和 MNC(移

Android Launcher浅析(二)

1,如何修改主菜单图标的位置? [DESCRIPTION] 默认主菜单图标在中间,如何修改它的位置? Launcher3: DynamicGrid.java文件 hotseatAllAppsRank = (int) (numColumns/2); //默认是列数除以2取整,可以设置为需要的值 Launcher2: 1. 请修改packages/apps/Laucher2/res/values/config.xml 中hotseat_all_apps_index的值,例如修改为1 2. 默认在ho