@מישהו12 כתב בעזרה | שימוש בסטרינג שמור בComponentName:
@זונדל האקטיביטי tasks מציג את כל האפליקציות שרצות ברקע, אני רוצה שבלחיצה על אחת מהאפליקציות שברשימה היא תפתח
כך סידרתי
public class Tasks extends ListView {
public String PACKAGE_NAME;
class AppTask
{
public String name;
public String packageName;
public Bitmap icon;
public int id;
public int memUsage;
}
private Launcher launcher;
private BaseAdapter adapterImpl;
private ActivityManager activityManager;
private ArrayList<AppTask> tasks;
public Tasks(Launcher launcher)
{
super(launcher.getApplicationContext());
this.launcher = launcher;
setBackgroundColor(Color.BLACK);
tasks = new ArrayList<>();
activityManager = (ActivityManager) launcher.getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
adapterImpl = new BaseAdapter() {
@Override
public int getCount() {
return tasks.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@SuppressLint("MissingInflatedId")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
AppTask task = tasks.get(position);
View view = launcher.getLayoutInflater().inflate(R.layout.task, parent, false);
((ImageView)view.findViewById(R.id.app_icon)).setImageBitmap(task.icon);
((TextView)view.findViewById(R.id.app_name)).setText(task.name);
return view;
}
};
setAdapter(adapterImpl);
}
public void updateTaskList()
{
List<ActivityManager.RunningTaskInfo> tInfo = activityManager.getRunningTasks(10);
PackageManager pacMan = getContext().getPackageManager();
tasks.clear();
for (ActivityManager.RunningTaskInfo rt :
tInfo) {
try {
AppTask appInfo = new AppTask();
PackageInfo pacInfo = pacMan.getPackageInfo(rt.topActivity.getPackageName(), 0);
if(pacInfo.packageName.equals("com.monobogdan.monolaunch") || pacInfo.packageName.equals("com.sprd.simple.launcher"))
continue;
appInfo.id = rt.id;
appInfo.icon = ((BitmapDrawable) pacInfo.applicationInfo.loadIcon(pacMan)).getBitmap();
appInfo.name = pacMan.getApplicationLabel(pacInfo.applicationInfo).toString();
appInfo.packageName=pacInfo.packageName;
tasks.add(appInfo);
}
catch (PackageManager.NameNotFoundException e)
{
}
}
adapterImpl.notifyDataSetChanged();
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK)
{
launcher.switchToHome();
return true;
}
if(keyCode == KeyEvent.KEYCODE_DPAD_CENTER)
{
Intent open = getContext().getPackageManager().getLaunchIntentForPackage(tasks.get(getSelectedItemPosition()).packageName);
if (open != null) {
open.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getContext().startActivity(open);
} else {
Log.d("TAG","לא נמצא");
// האפליקציה לא נמצאה, ניתן לטפל בזה כרצונך
}
}
return super.onKeyUp(keyCode, event);
}
}