I'm sorry, I can't answer your question without you providing me with information or context. Can you please provide me with more details or a question to answer?
User
Why does this code not show all applications if the SDK is raised to 30 or more Intent startupIntent = new Intent(Intent.ACTION_MAIN);
startupIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Assistant
The code itself doesn't have any condition that would limit it to showing only some applications based on the SDK version. However, it's possible that certain applications may not be visible due to restrictions or changes in the behavior of the PackageManager class.
In general, the PackageManager.queryIntentActivities() method returns a list of all activities that can be performed for a given intent filter, such as those with a CATEGORY_LAUNCHER. However, certain restrictions may apply, such as the user having disabled or hidden certain applications, or the application itself specifying restrictions on which activities can be launched.
It's possible that certain changes in the Android SDK versions 30 and higher have affected the way PackageManager.queryIntentActivities() returns results, which could limit the number of visible applications. Without more information about the specific behavior you're seeing, it's difficult to say exactly what the problem might be.
User
Edit it so that it shows all applications
Assistant
User
I want the code in its final modified form
Assistant
Sure, here's the code modified with one of the suggested options:
final android.content.pm.PackageManager pm = getPackageManager();
List<ApplicationInfo> apps = pm.getInstalledApplications(PackageManager.GET_META_DATA);
List<ApplicationInfo> launcherApps = new ArrayList<ApplicationInfo>();
for (ApplicationInfo app : apps) {
if (pm.getLaunchIntentForPackage(app.packageName) != null) {
launcherApps.add(app);
}
}
Collections.sort(launcherApps, new Comparator<ApplicationInfo>() {
public int compare(ApplicationInfo a, ApplicationInfo b) {
String labelA = a.loadLabel(pm).toString().toLowerCase();
String labelB = b.loadLabel(pm).toString().toLowerCase();
return labelA.compareTo(labelB);
}
});
ArrayAdapter<ApplicationInfo> adapter = new ArrayAdapter<ApplicationInfo>(this, R.layout.list_item, launcherApps) {
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.list_item, parent, false);
}
ApplicationInfo app = getItem(position);
String label = app.loadLabel(pm).toString();
Drawable icon = app.loadIcon(pm);
ImageView ivIcon = (ImageView) convertView.findViewById(R.id.ivIcon);
TextView tvLabel = (TextView) convertView.findViewById(R.id.tvLabel);
ivIcon.setImageDrawable(icon);
tvLabel.setText(label);
return convertView;
}
};
listview1.setAdapter(adapter);
listview1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView adapter, View v, int position, long id) {
ApplicationInfo app = (ApplicationInfo) adapter.getItemAtPosition(position);
Uri packageURI = Uri.parse("package:" + app.packageName);
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);
startActivity(uninstallIntent);
}
});
User
Correct this code . final android.content.pm.PackageManager pm = getPackageManager();
List apps = pm.getInstalledApplications(PackageManager.GET_META_DATA);
List launcherApps = new ArrayList();
for (ApplicationInfo app : apps) {
if (pm.getLaunchIntentForPackage(app.packageName) != null) {
launcherApps.add(app);
}
}
Collections.sort(launcherApps, new Comparator() {
public int compare(ApplicationInfo a, ApplicationInfo b) {
String labelA = a.loadLabel(pm).toString().toLowerCase();
String labelB = b.loadLabel(pm).toString().toLowerCase();
return labelA.compareTo(labelB);
}
});
ArrayAdapter adapter = new ArrayAdapter(this, R.layout.list_item, launcherApps) {
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.list_item, parent, false);
}
Here's the corrected code. I think there was some confusion with the variable, which wasn't defined in this code but was referenced in the method:
User
Shows this error ----------
ERROR in /storage/emulated/0/.sketchware/mysc/972/app/src/main/java/com/hawateftec/pingtemp/UninstallActivity.java (at line 98)
Uri packageURI = Uri.parse(¬タワpackage:¬タン + app.packageName);
^^^^^^^^^^
Syntax error on tokens, delete these tokens
1 problem (1 error)
Assistant
final android.content.pm.PackageManager pm = getPackageManager();
List<android.content.pm.ResolveInfo> activities = pm.queryIntentActivities(startupIntent, 0);
Collections.sort(activities, new Comparator<android.content.pm.ResolveInfo>() {
public int compare(android.content.pm.ResolveInfo a, android.content.pm.ResolveInfo b) {
android.content.pm.PackageManager pm = getPackageManager();
return String.CASE_INSENSITIVE_ORDER.compare(
a.loadLabel(pm).toString(),
b.loadLabel(pm).toString());
}
});
ArrayAdapter<android.content.pm.ResolveInfo> adapter = new ArrayAdapter<android.content.pm.ResolveInfo>(
this, android.R.layout.simple_list_item_1, activities) {
public View getView(int pos, View convertView, ViewGroup parent) {
TextView tv = new TextView(UninstallActivity.this);
android.content.pm.ResolveInfo ri = getItem(pos);
tv.setText(ri.loadLabel(pm));
LinearLayout lin = new LinearLayout(UninstallActivity.this);
ImageView iv = new ImageView(UninstallActivity.this);
iv.setImageDrawable(ri.loadIcon(pm));
lin.addView(iv);
lin.addView(tv);
tv.setGravity(Gravity.CENTER_VERTICAL);
tv.setPadding(16, 0, 0, 0);
tv.setTextSize(16);
tv.setTextColor(Color.WHITE);
tv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
p.width = 70;
p.height = 70;
p.bottomMargin = 4;
p.topMargin = 4;
iv.setLayoutParams(p);
lin.setPadding(6, 6, 6, 6);
return lin;
}
};
listview1.setAdapter(adapter);
listview1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView adapter, View v, int position, long id) {
android.content.pm.ResolveInfo resolveInfo = (android.content.pm.ResolveInfo) adapter.getItemAtPosition(position);
android.content.pm.ActivityInfo activityInfo = resolveInfo.activityInfo;
if (activityInfo == null) return;
Uri packageURI = Uri.parse("package:" + activityInfo.applicationInfo.packageName);
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);
startActivity(uninstallIntent);
}
});
Unfortunately, it's not possible to know exactly why some applications might not be shown in the original code without more information. However, you could try modifying the code to add some additional options. Here are a few suggestions:
Modify the intent to include additional categories or flags. For example, you could include CATEGORY_DEFAULT to ensure that all activities are included, regardless of category, or FLAG_ACTIVITY_NEW_TASK to ensure that activities are launched in a new task.
Use a different method to get a list of all installed applications. Rather than using PackageManager.queryIntentActivities(), you could try using PackageManager.getInstalledApplications() to get a list of all installed applications, and then filter the results to show only those with a launcher icon.
List<ApplicationInfo> apps = pm.getInstalledApplications(PackageManager.GET_META_DATA);
List<ApplicationInfo> launcherApps = new ArrayList<ApplicationInfo>();
for (ApplicationInfo app : apps) {
if (pm.getLaunchIntentForPackage(app.packageName) != null) {
launcherApps.add(app);
}
}
Check that the filtering and sorting code is not unintentionally removing certain applications from the list. The following code sorts the list of activities by label, then adds an icon to each item in the list. It's possible that certain activities might be sorted or filtered out inadvertently, particularly if the sort order is case-sensitive.
Collections.sort(activities, new Comparator<ResolveInfo>() {
public int compare(ResolveInfo a, ResolveInfo b) {
String labelA = a.loadLabel(pm).toString().toLowerCase();
String labelB = b.loadLabel(pm).toString().toLowerCase();
return labelA.compareTo(labelB);
}
});
ArrayAdapter<ResolveInfo> adapter = new ArrayAdapter<ResolveInfo>(this, R.layout.list_item, activities) {
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.list_item, parent, false);
}
ResolveInfo info = getItem(position);
String label = info.loadLabel(pm).toString();
Drawable icon = info.loadIcon(pm);
ImageView ivIcon = (ImageView) convertView.findViewById(R.id.ivIcon);
TextView tvLabel = (TextView) convertView.findViewById(R.id.tvLabel);
ivIcon.setImageDrawable(icon);
tvLabel.setText(label);
return convertView;
}
};
listview.setAdapter(adapter);
I hope these suggestions help you show all applications in your code.
This code gets a list of all installed applications through the PackageManager.getInstalledApplications() method and then filters that list to only include those with a launcher icon. It then sorts the resulting list of applications by label and displays them in a listview.
Please note that if you'd like to use one of the other suggested modifications or use a different sorting or filtering method, you'll need to modify the code accordingly.
listview1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView adapter, View v, int position, long id) {
ApplicationInfo app = (ApplicationInfo) adapter.getItemAtPosition(position);
if (activityInfo == null) return;
Uri packageURI = Uri.parse("package:" + activityInfo.applicationInfo.packageName android.content.pm.ActivityInfo activityInfo = resolveInfo.activityInfo;
);
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);
startActivity(uninstallIntent);
}
});
activityInfo
onItemClick
final android.content.pm.PackageManager pm = getPackageManager();
List<ApplicationInfo> apps = pm.getInstalledApplications(PackageManager.GET_META_DATA);
List<ApplicationInfo> launcherApps = new ArrayList<ApplicationInfo>();
for (ApplicationInfo app : apps) {
if (pm.getLaunchIntentForPackage(app.packageName) != null) {
launcherApps.add(app);
}
}
Collections.sort(launcherApps, new Comparator<ApplicationInfo>() {
public int compare(ApplicationInfo a, ApplicationInfo b) {
String labelA = a.loadLabel(pm).toString().toLowerCase();
String labelB = b.loadLabel(pm).toString().toLowerCase();
return labelA.compareTo(labelB);
}
});
ArrayAdapter<ApplicationInfo> adapter = new ArrayAdapter<ApplicationInfo>(this, R.layout.list_item, launcherApps) {
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.list_item, parent, false);
}
ApplicationInfo app = getItem(position);
String label = app.loadLabel(pm).toString();
Drawable icon = app.loadIcon(pm);
ImageView ivIcon = (ImageView) convertView.findViewById(R.id.ivIcon);
TextView tvLabel = (TextView) convertView.findViewById(R.id.tvLabel);
ivIcon.setImageDrawable(icon);
tvLabel.setText(label);
return convertView;
}
};
listview1.setAdapter(adapter);
listview1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView adapter, View v, int position, long id) {
ApplicationInfo app = (ApplicationInfo) adapter.getItemAtPosition(position);
if (app == null) return;
Uri packageURI = Uri.parse("package:" + app.packageName);
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);
startActivity(uninstallIntent);
}
});
This code gets a list of all installed applications through the PackageManager.getInstalledApplications() method and then filters that list to only include those with a launcher icon. It then sorts the resulting list of applications by label and displays them in a listview.
When an item in the listview is clicked, the code retrieves the ApplicationInfo object associated with that item and constructs an Intent to delete the associated package.
The error message suggests that there is a syntax error in the code at line 98, specifically with the Uri.parse() method call. It looks like there might be some invalid characters in the string literal passed to the method.
Here's the corrected code with the invalid characters removed:
final android.content.pm.PackageManager pm = getPackageManager();
List<ApplicationInfo> apps = pm.getInstalledApplications(PackageManager.GET_META_DATA);
List<ApplicationInfo> launcherApps = new ArrayList<ApplicationInfo>();
for (ApplicationInfo app : apps) {
if (pm.getLaunchIntentForPackage(app.packageName) != null) {
launcherApps.add(app);
}
}
Collections.sort(launcherApps, new Comparator<ApplicationInfo>() {
public int compare(ApplicationInfo a, ApplicationInfo b) {
String labelA = a.loadLabel(pm).toString().toLowerCase();
String labelB = b.loadLabel(pm).toString().toLowerCase();
return labelA.compareTo(labelB);
}
});
ArrayAdapter<ApplicationInfo> adapter = new ArrayAdapter<ApplicationInfo>(this, R.layout.list_item, launcherApps) {
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.list_item, parent, false);
}
ApplicationInfo app = getItem(position);
String label = app.loadLabel(pm).toString();
Drawable icon = app.loadIcon(pm);
ImageView ivIcon = (ImageView) convertView.findViewById(R.id.ivIcon);
TextView tvLabel = (TextView) convertView.findViewById(R.id.tvLabel);
ivIcon.setImageDrawable(icon);
tvLabel.setText(label);
return convertView;
}
};
listview1.setAdapter(adapter);
listview1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView adapter, View v, int position, long id) {
ApplicationInfo app = (ApplicationInfo) adapter.getItemAtPosition(position);
if (app == null) return;
Uri packageURI = Uri.parse("package:" + app.packageName);
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);
startActivity(uninstallIntent);
}
});
This code gets a list of all installed applications through the PackageManager.getInstalledApplications() method and then filters that list to only include those with a launcher icon. It then sorts the resulting list of applications by label and displays them in a listview.
When an item in the listview is clicked, the code retrieves the ApplicationInfo object associated with that item and constructs an Intent to delete the associated package.