Question

I'm working on an app that shows the icons of the currently running in a ListView. I already have written code that shows the running app icons in the ListView, and it works well.

However, I don't know about attaching click events (click to switch to corresponding app) to ListView items. How do I launch the corresponding app when I click an item on the ListView? I'm trying, but switching doesn't work well. How can I do this? Here is my code so far:

[LeftSidePanel.java]

package kr.hybdms.sidepanel;

import java.util.ArrayList;
import java.util.List;
import kr.hybdms.sidepanel.PanelArrayAdapter;
import kr.hybdms.sidepanel.R;
import kr.hybdms.sidepanel.util.SystemUiHider;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 * 
 * @see SystemUiHider
  */

public class LeftSidePanel extends Activity implements OnItemClickListener {
    ListView listView;
    List<PanelItemDetail> rowItems;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_left_side_panel);
        listView = (ListView) findViewById(R.id.panelcontents);
        ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(30);
        rowItems = new ArrayList<PanelItemDetail>();
        PackageManager pacMgr = getPackageManager();
        for (ActivityManager.RunningTaskInfo runningTask: tasks) {
            try {
                rowItems.add(new PanelItemDetail(pacMgr.getApplicationIcon(
                        runningTask.topActivity.getPackageName())));
            } catch (NameNotFoundException e) {
                e.printStackTrace();
            }
        }
        listView = (ListView) findViewById(R.id.panelcontents);
        PanelArrayAdapter adapter = new PanelArrayAdapter(this,
        R.layout.panelrow, rowItems);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);
    }
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
        String packageName = am.getRunningTasks(30).get(0).topActivity.getPackageName();
        String className = am.getRunningTasks(30).get(0).topActivity.getClassName();
        startActivity(new Intent().setClassName(packageName,
                className).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
    }
}

[PanelArrayAdapter.java]

package kr.hybdms.sidepanel;

import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;

public class PanelArrayAdapter extends ArrayAdapter<PanelItemDetail> {
    Context context;

    public PanelArrayAdapter(Context context, int resourceId, List<PanelItemDetail> items) {
        super(context, resourceId, items);
        this.context = context;
   }

   /*private view holder class*/
   private class ViewHolder {
       ImageView imageView;
   }

   public View getView(int position, View convertView, ViewGroup parent) {
       ViewHolder holder = null;
       PanelItemDetail rowItem = getItem(position);
       LayoutInflater mInflater = (LayoutInflater)
               context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
       if (convertView == null) {
           convertView = mInflater.inflate(R.layout.panelrow, null);
           holder = new ViewHolder();
           holder.imageView = (ImageView) convertView.findViewById(R.id.appicon);
           convertView.setTag(holder);
       } else
           holder = (ViewHolder) convertView.getTag();
       holder.imageView.setImageDrawable(rowItem.getImageId());
       return convertView;
   }

}


[PanelItemDetail.java]

package kr.hybdms.sidepanel;

import android.graphics.drawable.Drawable;

public class PanelItemDetail {
    private Drawable imageId;

    public PanelItemDetail(Drawable images) {
        this.imageId = images;
    }

    public Drawable getImageId() {
        return imageId;
    }

    public void setImageId(Drawable imageId) {
        this.imageId = imageId;
    }
}
Was it helpful?

Solution 2

In your onItemClick use your adapter to get the item in the list view that was clicked by using adapter.get(position) or you can use getListView().getItem(position) which will return your custom type so you can get the information for which activity was selected then follow @user1762507's instructions for launching that intent

OTHER TIPS

Try

Intent LaunchIntent = getPackageManager().
getLaunchIntentForPackage("com.package.address");

startActivity(LaunchIntent);

but make sure you declare <category android:name="android.intent.category.LAUNCHER" /> in your manifest in the activity that launches the new app.

 private ArrayList results = new ArrayList();





PackageManager pm = this.getPackageManager();

    Intent intent = new Intent(Intent.ACTION_MAIN, null);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);

    final List<ResolveInfo> list = pm.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED);
    for (ResolveInfo rInfo : list) {
     results.add(rInfo.activityInfo.applicationInfo.loadLabel(pm).toString());
     Log.w("Installed Applications", rInfo.activityInfo.applicationInfo.loadLabel(pm).toString());
    } 

   final ArrayAdapter<String> adapter1;
    adapter1 =new ArrayAdapter(this, android.R.layout.simple_list_item_1, results);

    lView.setAdapter(adapter1);


    lView.setOnItemClickListener(new OnItemClickListener() {


        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {

            final String packageName = list.get(position).activityInfo.packageName;

            Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage(packageName);
            startActivity( LaunchIntent );


            Toast.makeText(getApplicationContext(), packageName, Toast.LENGTH_LONG).show();
        }
    });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top