Android Launcher-simple Launcher development

Source: Internet
Author: User

 

Step 1: Use our application as the home(That is, press the home key to start your launcher .)

To use our application as home, you only need to add the following in 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>

 

Step 2: List the installed applications

List Installed applications as an indispensable feature for launcher. The following describes how to list applications. After the program is run, it looks as follows:

1. modify main. xml and add a GridView to display the Application List.

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. query the installed apk through the PackageManager api

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

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

3. Implement the Adapter used to display the Gridview to display the list of applications obtained

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. Listen to the onItemClick event of the GridView

Set a listener to notify us of a callback function when a certain item of the gridView is clicked.
We call mGrid. setOnItemClickListener (listener); set a listener.
Listener in mGrid. setOnItemClickListener (listener) is an interface of the following type: android. widget. AdapterView. OnItemClickListener

Private OnItemClickListener listener = new OnItemClickListener (){
@ Override
Publicvoid onItemClick (AdapterView <?> Parent, View view, int position, long id ){
// Event content ....
}
};

5. Start the activity of the clicked Application

Generally, we can know which project is clicked based on position. Now we extract the corresponding application data (mainly the main activity) based on the project to be clicked, and then start the activity. Use the following code:

Publicvoid onItemClick (AdapterView <?> Parent, View view, int position, long id ){
ResolveInfo info = mApps. get (position );

// Package name of the application
String pkg = info. activityInfo. packageName;
// Main activity class of the application
String cls = info. activityInfo. name;

ComponentName componet = new ComponentName (pkg, cls );

Intent I = new Intent ();
I. setComponent (componet );
StartActivity (I );
}

The code for the entire Activity is as follows:

MyHome. javapackage 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. imageV Iew; 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); // The package name of the application String pkg = info. activityInfo. packageName; // main activity class of the application 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. performance_list); mGrid. setAdapter (new external adapter (); 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 extends adapter extends BaseAdapter {public extends adapter () {} 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 ;}}}

Reprinted from: http://www.cnblogs.com/playing/archive/2011/04/13/2014705.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.