2

我正在开发一个显示当前在 ListView 中运行的图标的应用程序。我已经编写了在 ListView 中显示正在运行的应用程序图标的代码,并且运行良好。

但是,我不知道将单击事件(单击以切换到相应的应用程序)附加到 ListView 项目。单击 ListView 上的项目时如何启动相应的应用程序?我正在尝试,但切换效果不佳。我怎样才能做到这一点?到目前为止,这是我的代码:

[左侧面板.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));
    }
}

[面板阵列适配器.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;
    }
}
4

3 回答 3

2

尝试

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

startActivity(LaunchIntent);

但请确保<category android:name="android.intent.category.LAUNCHER" /> 在启动新应用程序的活动中在清单中声明。

于 2013-02-21T01:34:39.057 回答
1

在您onItemClick使用您的适配器来获取列表视图中通过使用单击的项目,adapter.get(position)或者您可以使用getListView().getItem(position)它将返回您的自定义类型,以便您可以获取选择了哪个活动的信息,然后按照@user1762507 的说明启动该意图

于 2013-02-21T01:36:40.830 回答
0
 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();
        }
    });
于 2017-02-11T14:44:48.387 回答