开发者

OnItemClickListener that passes the Package Name of the app selected to a method... Help

开发者 https://www.devze.com 2023-03-06 00:31 出处:网络
UPDATE: Now looking for Package Name as opposed to PID I have an app that diplays a listview of all the installed apps on the device. I want to create an OnItemClickListener that passes the Package N

UPDATE: Now looking for Package Name as opposed to PID

I have an app that diplays a listview of all the installed apps on the device. I want to create an OnItemClickListener that passes the Package Name of the app selected to a method.

Here is my code:

public class Main extends ListActivity {

    private static final int ACTIVITY_CREATE=0;
    public static final int Menu1 = Menu.FIRST;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        final PackageManager pm = this.getPackageManager();

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

        final ArrayList<ResolveInfo> list =
                (ArrayList<ResolveInfo>) pm.queryIntentActivities(intent, 
                        PackageManager.PERMISSION_GRANTED);
        for (ResolveInfo rInfo : list)
        {
            Log.i("Installed Applications " + rInfo.activityInfo.
                    applicationInfo.loadLabel(pm).toString(), null);
        }

        final ArrayAdapter<ResolveInfo> adapter = 
            new开发者_运维知识库 ArrayAdapter<ResolveInfo>(this, R.layout.list_item, list)
            {
            @Override
            public View getView(int position, View convertView, ViewGroup parent)
            {
                if (convertView == null)
                    convertView = LayoutInflater.from(parent.getContext()).
                        inflate(R.layout.list_item, parent, false);

                final String text = list.get(position).activityInfo.
                    applicationInfo.loadLabel(pm).toString();
                ((TextView)convertView.findViewById(R.id.text)).setText(text);

                final Drawable drawable = list.get(position).activityInfo.applicationInfo.loadIcon(pm);
                ((ImageView)convertView.findViewById(R.id.image)).setImageDrawable(drawable);

                return convertView;
            }

            };
        setListAdapter(adapter);

        ListView lv = getListView();
        lv.setTextFilterEnabled(true);
        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

                // On Item Click Activity

                // This is where I would like to pass the package name to a method
            }
          });
    }

Any help would be greatly appreciated!


You can retrieve the package name for the activityInfo member of the ResolveInfo instance whose item you just clicked on:

lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
        final String packageName = list.get(position).activityInfo.packageName;
        Log.i("PACKAGE", packageName);
        //TODO: Pass this packageName to a method
    }
});


It is fundamentally wrong to try to associate pids with apps. One app can run in multiple pids, one pid can run multiple apps. Whatever you are actually trying to do (you don't say so it is hard to give useful help), this is not the way.


You might have more than one PID in the application. Take a look at the ActivityManager documentation. http://developer.android.com/reference/android/app/ActivityManager.html

Use this as a base:

ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> activities = am.getRunningAppProcesses();
for (int i=0; i< activities.size(); i++)
    Log.d ("PID INFO", "Activity " + i + " - PID: " + activities.get(i).pid);
}

And, BTW, don't post so much code next time. It is not relevant for your question :)

0

精彩评论

暂无评论...
验证码 换一张
取 消