我正在开发一个应用程序,显示当前在ListView中运行的图标。我已经编写了代码来显示ListView中正在运行的应用程序图标,它运行良好。
但是,我不知道将点击事件(点击切换到相应的应用程序)附加到ListView项目。单击ListView上的项目时如何启动相应的应用程序?我正在尝试,但切换效果不佳。我怎样才能做到这一点?到目前为止,这是我的代码:
[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;
}
}
答案 0 :(得分:2)
尝试
Intent LaunchIntent = getPackageManager().
getLaunchIntentForPackage("com.package.address");
startActivity(LaunchIntent);
但请务必声明<category android:name="android.intent.category.LAUNCHER" />
在您启动新应用的活动的清单中。
答案 1 :(得分:1)
在onItemClick
使用您的适配器获取使用adapter.get(position)
点击的列表视图中的项目,或者您可以使用getListView().getItem(position)
来返回您的自定义类型,这样您就可以获得选择了哪个活动的信息,然后按照@ user1762507的说明启动该意图
答案 2 :(得分: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();
}
});