如何使用ActivityManager和PackageManager在listview中显示正在运行的应用程序图标?

时间:2013-02-15 08:32:15

标签: java android android-package-managers activity-manager

我正在尝试制作一款应用... - 目前正在listview中运行应用程序图标 列表视图上的单击项目,显示正在运行的应用程序图标以进行切换

但我不知道怎么做这些,我已经google了很多 但我发现我应该使用ActivityManager.RunningTaskinfo和Packagemanager来做这些

云你帮我做这些吗? 如何在listview中显示正在运行的应用程序图标,并给listview点击事件(例如OnItemClick)切换到点击列表视图的应用程序?

[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.content.pm.PackageInfo;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

/**
 * 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);

List<PackageInfo> packs = getPackageManager().getInstalledPackages(10);
Drawable[] images = new Drawable[packs.size()];
for(int i=0;i<packs.size();i++) {
    PackageInfo p = packs.get(i);
    images[i]= p.applicationInfo.loadIcon(getPackageManager());
 }

rowItems = new ArrayList<PanelItemDetail>();
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) {
Toast toast = Toast.makeText(getApplicationContext(),
    "Item " + (position + 1) + ": " + rowItems.get(position),
    Toast.LENGTH_SHORT);
toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
}
}

[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;
    }
}

[panelrow.xml]

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

<ImageView
    android:id="@+id/appicon"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_margin="10dp" />

    </RelativeLayout>

[activity_left_side_panel.xml]

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".LeftSidePanel" >

    <ImageView android:id="@+id/transparentbackground"
        android:src="@drawable/detector"
        android:adjustViewBounds="true"
        android:gravity="center_vertical"
        android:scaleType="fitXY"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_x="0dp" 
        android:layout_y="0dp" 
        android:layout_marginLeft="100dp"/>

    <ImageView
        android:id="@+id/panelbackground"
        android:layout_width="100dp"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:scaleType="fitXY"
        android:src="@drawable/panelbg" />

    <ListView
        android:id="@+id/panelcontents"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignRight="@+id/panelbackground"
        android:layout_centerVertical="true"
        android:listSelector="@drawable/panel_item_bg"
        android:divider="#000000" >

    </ListView>

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

尝试阅读本教程可能会对您有所帮助 here