External App Launcher In Android

This example will show how you can read Rss feeds from your activity.

Algorithm:

1.) Create a new project by File-> New -> Android Project name it ExternalAppLauncher.

2.) Write following into strings.xml:

 

<resources>

<string name="app_name">ExternalAppLauncher</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_external_app_launcher">ExternalAppLauncherActivity</string>
<string name="app_desc">Example 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>

 

 

3.) Write following into res/layout/appview.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>

 

4.) Write following into 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>

 

 

5.) Create and write following into src/AppLauncher.java:

 

package com.example.externalapplauncher;

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

 

 

6.) Create and Write following into res/menu/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>

 

7.) Run for output.

Steps:

1.) Create a project named ExternalAppLauncher and set the information as stated in the image.

Build Target: Android 4.0
Application Name: ExternalAppLauncher
Package Name: com. example. ExternalAppLauncher
Activity Name: ExternalAppLauncherActivity
Min SDK Version: 2.2

2.) Open ExternalAppLauncher.java file and write following code there:

 

package com.example.externalapplauncher;

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 ExternalAppLauncherActivity 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(ExternalAppLauncherActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
}
catch (Exception e)
{
Toast.makeText(ExternalAppLauncherActivity.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(ExternalAppLauncherActivity.this, R.layout.appview, 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(
ExternalAppLauncherActivity.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)ExternalAppLauncherActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.appview, 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;
}
};
}

 

 

3.) Compile and build the project.

Note: – For better results check on device.

Output