Challange 2 Solution


#1

anybody have challange 2 solution?
if you willing please share your solution
thanks

here is my solution

[code]public class NerdTaskManager extends ListFragment {
private static final String TAG = “NerdTaskManager”;

@TargetApi(Build.VERSION_CODES.HONEYCOMB) // because moveTaskToFront need api 11
@Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

	Intent startupIntent = new Intent(Intent.ACTION_MAIN);
	startupIntent.addCategory(Intent.CATEGORY_LAUNCHER);
	
	//final PackageManager pm = getActivity().getPackageManager();
	
	//List<ResolveInfo> activities = pm.queryIntentActivities(startupIntent, 0);
	
	ActivityManager am = (ActivityManager) getActivity().getSystemService(getActivity().ACTIVITY_SERVICE);
	List< ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(5);
	Log.i("running", "I've found " + taskInfo.size() + " activites.");
	//Log.i(TAG, "I've found " + activities.size() + " activites.");
	
	//Collections.sort(activities, new Comparator<ResolveInfo>() {
		//public int compare(ResolveInfo a, ResolveInfo b) {
			//PackageManager pm = getActivity().getPackageManager();
			//return String.CASE_INSENSITIVE_ORDER.compare(
				//	a.loadLabel(pm).toString(),
					//b.loadLabel(pm).toString());
		//}
	//});
	
	ArrayAdapter<ActivityManager.RunningTaskInfo> adapter = new ArrayAdapter<ActivityManager.RunningTaskInfo>(
			getActivity(), android.R.layout.simple_list_item_1, taskInfo) {
		public View getView(int pos, View convertView, ViewGroup parent) {
			if (null == convertView) {
			convertView = getActivity().getLayoutInflater()
					.inflate(R.layout.list_app, null);
			}
			ActivityManager.RunningTaskInfo ri = getItem(pos);
			//View v = super.getView(pos, convertView, parent);
			// Documentation says that simple_list_item_1 is a TextView,
			// so cast it so that you can set its text value
			
			TextView applicationName = (TextView)convertView.findViewById(R.id.app_list_text);
			applicationName.setText(ri.baseActivity.getPackageName().toString());
			//ImageButton ib =(ImageButton)v;
			//ImageView iconApp= (ImageView)convertView.findViewById(R.id.app_list_icon);
			//ib.setImageDrawable(ri.loadIcon(pm));
			//iconApp.setImageDrawable(ri.loadIcon(am));
			
			return convertView;
		}
	};	
	setListAdapter(adapter);
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
ActivityManager.RunningTaskInfo ri = (ActivityManager.RunningTaskInfo)l.getAdapter().getItem(position);
ActivityManager am = (ActivityManager) getActivity().getSystemService(getActivity().ACTIVITY_SERVICE);
am.moveTaskToFront(ri.id, 0);
}
[/code]
permission added to AndroidManifest.xml

<uses-permission android:name="android.permission.GET_TASKS" /> <uses-permission android:name="android.permission.REORDER_TASKS" />

in my near desperation
finally i finish this challange
thank god :slight_smile: