Perfect Solution For Challenge 2


#1

step 1: i created a listfragment, inflated a layout with imageView and TextView for the icon and application name respectively
step 2: get the running task using ActivityManager.getRunningTaskInfo this returns a list of running task or null; surround with try catch
step 3:create instance of ApplicationInfo and pass in the running task packagename and a flag to get the metadata
step 4: use the ActivityManager.movetaskToFront() and pass in the id of the runningTask and a flag to bring the task with home

see the code below

package com.example.nerdlauncher;

import java.util.List;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RecentTaskInfo;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo;
import android.drm.DrmStore.Action;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class RunningTask extends ListFragment{
private List mRunningApps;
private ActivityManager activityManager;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
activityManager=(ActivityManager) (getActivity().getSystemService(Activity.ACTIVITY_SERVICE));

	try {
		mRunningApps=activityManager.getRunningTasks(Integer.MAX_VALUE);
	} catch (Exception e) {
		Toast.makeText(getActivity(), "no running processes", Toast.LENGTH_LONG).show();
		e.printStackTrace();
	}
	
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
	if(mRunningApps !=null)setListAdapter(new cMyAdapter(mRunningApps));
	else  Toast.makeText(getActivity(), "no running processes", Toast.LENGTH_LONG).show();

	return super.onCreateView(inflater, container, savedInstanceState);
}
private class cMyAdapter extends ArrayAdapter<RunningTaskInfo>{

	public cMyAdapter( List<RunningTaskInfo> objects) {
		super(getActivity(),0, objects);
		// TODO Auto-generated constructor stub
	}
	@Override
	public View getView(int position, View view, ViewGroup parent) {
		view=getActivity().getLayoutInflater().inflate(R.layout.launcher_with_icon, null);
		ListAdapter adapter=getListAdapter();
		RunningTaskInfo running=(RunningTaskInfo) adapter.getItem(position);
		PackageManager pm=getActivity().getPackageManager();
		ApplicationInfo appInfo;
		try {
			appInfo = pm.getApplicationInfo(running.baseActivity.getPackageName(), PackageManager.GET_META_DATA);
			//ApplicationInfo appInfo=(ApplicationInfo)running;
 			TextView textView=(TextView)view.findViewById(R.id.app_name);
 			ImageView imageView=(ImageView)view.findViewById(R.id.app_icon);
			imageView.setImageBitmap(Utils.scaleImageDrawable(getActivity(), appInfo.loadIcon(pm)));
 			textView.setText(appInfo.loadLabel(pm));
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	
		return view;
	}
	
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
	ListAdapter adapter=getListAdapter();
	RunningTaskInfo running=(RunningTaskInfo) adapter.getItem(position);
	PackageManager pm=getActivity().getPackageManager();
	 ApplicationInfo appInfo;
	try {
		appInfo = pm.getApplicationInfo(running.baseActivity.getPackageName(), PackageManager.GET_META_DATA);
	 activityManager.moveTaskToFront(running.id, ActivityManager.MOVE_TASK_WITH_HOME);
	
	}catch(Exception ex){}
}

}

so simple and interesting