gpt4 book ai didi

android - 单击 ListView 项目时启动应用程序

转载 作者:搜寻专家 更新时间:2023-11-01 09:00:58 26 4
gpt4 key购买 nike

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

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

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

最佳答案

尝试

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

startActivity(LaunchIntent);

但请务必声明 <category android:name="android.intent.category.LAUNCHER" />在启动新应用程序的 Activity 中的 list 中。

关于android - 单击 ListView 项目时启动应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14992835/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com