Friday, 31 August 2012

Simple Android Application Launcher

A Launcher presents the main view of the phone and is responsible for starting other apps and hosting live widgets

Here the resource (res) consist of  "drawable", "layout", "menu", "values" Folders.

Use the following codes in "layout"

app.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/app_icon"
android:layout_width="48px"
android:layout_height="48px"
android:padding="3px"
android:scaleType="fitXY"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/app_name"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"/>
</LinearLayout>
</LinearLayout>

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">

<ListView 
   android:layout_width="fill_parent" 
   android:layout_height="wrap_content"
   android:id="@android:id/list"/>
   
</LinearLayout>

Use the following to the "menu" folder

menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu
  xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_about" 
    android:titleCondensed="@string/menu_about" 
    android:title="@string/menu_about_desc" 
    android:icon="@android:drawable/ic_menu_help"></item>
</menu>

Use the following codes in "values"

string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    
    <string name="app_name">App Launcher</string>
<string name="app_desc">Simple application to show how to enumerate and launch external application</string>
<string name="menu_about">About</string>
<string name="menu_about_desc">About App Launcher</string>
</resources>


img.1



The Source (src) consist of two classes 

AppLauncher.java and MainAvtivity.java
follow this cods in the class AppLauncher.java :
package com.samples.applauncher;
import android.app.Application;
import android.content.res.Configuration;

public class AppLauncher extends Application 
{
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}

@Override
public void onCreate() {
super.onCreate();
}

@Override
public void onLowMemory() {
super.onLowMemory();
}

@Override
public void onTerminate() {
super.onTerminate();
}
}

follow this cods in the class MainAvtivity.java :

package com.samples.applauncher;

import java.util.ArrayList;
import java.util.List;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainAvtivity extends ListActivity {
PackageManager packageManager = null;
List<ApplicationInfo> applist = null;
ApplicationAdaptor listadaptor = null;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        packageManager = getPackageManager();
        
        new LoadApplicationTask().execute();
    }
    
    public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
   inflater.inflate(R.menu.menu, menu);
   
return true;
}
    
    public boolean onOptionsItemSelected(MenuItem item) {
    boolean result = true;
   
switch(item.getItemId())
{
case R.id.menu_about:
{
displayAboutDialog();
break;
}
default:
{
result = super.onOptionsItemSelected(item);
break;
}
}
return result;

    }
    
    
    private void displayAboutDialog()
{
    final AlertDialog.Builder builder = new AlertDialog.Builder(this);

    builder.setTitle(getString(R.string.app_name));
    builder.setMessage(getString(R.string.app_desc));
   
    builder.show();
}
        
    @Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
ApplicationInfo app = applist.get(position);
try 
{
Intent intent = packageManager.getLaunchIntentForPackage(app.packageName);
if(null != intent)
{
startActivity(intent);
}
catch (ActivityNotFoundException e)
{
Toast.makeText(MainAvtivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
}
catch (Exception e) 
{
Toast.makeText(MainAvtivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}

    private List<ApplicationInfo> checkForLaunchIntent(List<ApplicationInfo> list)
    {
    ArrayList<ApplicationInfo> applist = new ArrayList<ApplicationInfo>(); 
   
    for(ApplicationInfo info: list)
    {
    try {
    if(null != packageManager.getLaunchIntentForPackage(info.packageName))
    {
    applist.add(info);
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
   
return applist;
    }
    
    private class LoadApplicationTask extends AsyncTask<Void, Void, Void>
    {
    private ProgressDialog progress = null;
   
@Override
protected Void doInBackground(Void... params) {
applist = checkForLaunchIntent(
        packageManager.getInstalledApplications(PackageManager.GET_META_DATA));
       listadaptor = new ApplicationAdaptor(MainAvtivity.this, R.layout.app, applist);
       
return null;
}

@Override
protected void onCancelled() {
super.onCancelled();
}

@Override
protected void onPostExecute(Void result) {
setListAdapter(listadaptor);
progress.dismiss();
super.onPostExecute(result);
}

@Override
protected void onPreExecute() {
progress = ProgressDialog.show(
MainAvtivity.this, null, "Loading application info...");
super.onPreExecute();
}

@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
    }
    
private class ApplicationAdaptor extends ArrayAdapter<ApplicationInfo>
    {
    private List<ApplicationInfo> objects = null;

public ApplicationAdaptor(Context context, 
int textViewResourceId,
List<ApplicationInfo> objects) 
{
super(context, textViewResourceId, objects);
this.objects = objects;
}
   
@Override
public int getCount() {
return ((null != objects) ? objects.size() : 0);
}

@Override
public ApplicationInfo getItem(int position) {
return ((null != objects) ? objects.get(position) : null);
}

@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if(null == view)
{
LayoutInflater vi = (LayoutInflater)MainAvtivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.app, null);
}
ApplicationInfo data = objects.get(position);
if(null != data)
{
TextView textName = (TextView)view.findViewById(R.id.app_name);
ImageView iconview = (ImageView)view.findViewById(R.id.app_icon);
textName.setText(data.loadLabel(packageManager) + " (" + data.packageName + ")");
iconview.setImageDrawable(data.loadIcon(packageManager));
}
return view;
}
    };
}


The AndroidManifest.xml look like :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      android:versionCode="1"
      android:versionName="1.0" package="com.samples.applauncher">
    <application android:icon="@drawable/icon" android:label="@string/app_name"
         android:name="AppLauncher" android:description="@string/app_desc">
        <activity android:name=".MainAvtivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
    <uses-sdk android:minSdkVersion="3" />

</manifest> 

Note: Modify the above code as you requirements.