如何查找以特定字母开头的应用程序? - Android 启动器
How to find apps that start with a specific alphabet? - Android Launcher
我正在为 android 制作一个启动器,它具有用户选择字母表的功能,然后将显示以该字母表开头的应用程序。
我有一个 class 存储应用程序的名称、图标等,并可以使用 Gridiew 成功显示它。但我似乎无法弄清楚如何只显示以所选字母开头的所选应用程序。
MainActivity/Dashboard.java
public class Dashboard extends Activity {
DrawerAdapter drawerAdapterObject;
GridView drawerGrid;
class Pac {
Drawable icon;
String name;
String label;
}
Pac[] pacs;
PackageManager pm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
drawerGrid = (GridView) findViewById(R.id.content);
pm = getPackageManager();
set_pacs();
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_PACKAGE_ADDED);
filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
filter.addDataScheme("package");
registerReceiver(new PacReciever(), filter);
}
public void set_pacs() {
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
mainIntent.hasExtra("facebok");
List<ResolveInfo> pacsList = pm.queryIntentActivities(mainIntent, 0);
pacs = new Pac[pacsList.size()];
for(int i = 0; i < pacsList.size(); i++) {
pacs[i] = new Pac();
pacs[i].icon = pacsList.get(i).loadIcon(pm);
pacs[i].name = pacsList.get(i).activityInfo.packageName;
pacs[i].label = pacsList.get(i).loadLabel(pm).toString();
}
new SortApps().exchage_sort(pacs);
drawerAdapterObject = new DrawerAdapter(this, pacs);
drawerGrid.setAdapter(drawerAdapterObject);
drawerGrid.setOnItemClickListener(new DrawerClickListner(this, pacs, pm));
}
public class PacReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
set_pacs();
}
}
public void test(){
Intent test = new Intent(this,MainActivity.class);
startActivity(test);
}
}
activity_dashboard.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_dashboard"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.zyconut.socio.Dashboard">
<SlidingDrawer
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:handle="@+id/handle"
android:content="@+id/content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<ImageView
android:id="@+id/handle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@mipmap/ic_launcher"/>
<GridView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimarytrans"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="50dp"
android:horizontalSpacing="50dp"/>
</SlidingDrawer>
</RelativeLayout>
DrawerAdapter.java
public class DrawerAdapter extends BaseAdapter {
Context mContext;
Dashboard.Pac[] pacsForAdapter;
public DrawerAdapter(Context c, Dashboard.Pac pacs[]) {
mContext = c;
pacsForAdapter = pacs;
}
@Override
public int getCount() {
return pacsForAdapter.length;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
static class ViewHolder {
TextView text;
ImageView icon;
}
@Override
public View getView(int pos, View convertView, ViewGroup viewGroup) {
ViewHolder viewHolder;
LayoutInflater li = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(convertView == null) {
convertView = li.inflate(R.layout.drawer_item, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) convertView.findViewById(R.id.icon_text);
viewHolder.icon = (ImageView) convertView.findViewById(R.id.icon_image);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.icon.setImageDrawable(pacsForAdapter[pos].icon);
viewHolder.text.setText(pacsForAdapter[pos].label);
return convertView;
}
}
DrawerClickListner.java
public class DrawerClickListner implements OnItemClickListener {
Context mContext;
Dashboard.Pac[] pacsForAdapter;
PackageManager pmForListner;
public DrawerClickListner(Context c, Dashboard.Pac[] pacs, PackageManager pm) {
mContext = c;
pacsForAdapter = pacs;
pmForListner = pm;
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
Intent launchIntent = pmForListner.getLaunchIntentForPackage(pacsForAdapter[pos].name);
mContext.startActivity(launchIntent);
}
}
SortApps.java
public class SortApps {
public void exchage_sort(Dashboard.Pac[] pacs){
int i,j;
Dashboard.Pac temp;
for(i = 0; i < pacs.length; i++) {
for(j = i + 1; j < pacs.length; j++) {
if(pacs[i].label.compareToIgnoreCase(pacs[j].label) > 0) {
temp = pacs[i];
pacs[i] = pacs[j];
pacs[j] = temp;
}
}
}
}
}
drawer_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<ImageView
android:id="@+id/icon_image"
android:layout_width="65dp"
android:layout_height="65dp"
android:padding="3dp"/>
<TextView
android:id="@+id/icon_text"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:maxLines="2"
android:gravity="center_horizontal"/>
</LinearLayout>
第一次post,如有错误请多多包涵
我只需要有人告诉我如何显示仅以 'A' 开头的应用程序。休息我也许能弄明白。
提前致谢
您可以使用以下代码段获取已安装应用程序的列表。
List<String> installedApps = new ArrayList<String>();
List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);
for(int i=0;i<packs.size();i++) {
PackageInfo p = packs.get(i);
String appName = p.applicationInfo.loadLabel(getPackageManager()).toString();
installedApps.add(appName);
}
然后您可以使用此列表根据起始字母过滤应用程序。
我正在为 android 制作一个启动器,它具有用户选择字母表的功能,然后将显示以该字母表开头的应用程序。 我有一个 class 存储应用程序的名称、图标等,并可以使用 Gridiew 成功显示它。但我似乎无法弄清楚如何只显示以所选字母开头的所选应用程序。
MainActivity/Dashboard.java
public class Dashboard extends Activity {
DrawerAdapter drawerAdapterObject;
GridView drawerGrid;
class Pac {
Drawable icon;
String name;
String label;
}
Pac[] pacs;
PackageManager pm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
drawerGrid = (GridView) findViewById(R.id.content);
pm = getPackageManager();
set_pacs();
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_PACKAGE_ADDED);
filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
filter.addDataScheme("package");
registerReceiver(new PacReciever(), filter);
}
public void set_pacs() {
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
mainIntent.hasExtra("facebok");
List<ResolveInfo> pacsList = pm.queryIntentActivities(mainIntent, 0);
pacs = new Pac[pacsList.size()];
for(int i = 0; i < pacsList.size(); i++) {
pacs[i] = new Pac();
pacs[i].icon = pacsList.get(i).loadIcon(pm);
pacs[i].name = pacsList.get(i).activityInfo.packageName;
pacs[i].label = pacsList.get(i).loadLabel(pm).toString();
}
new SortApps().exchage_sort(pacs);
drawerAdapterObject = new DrawerAdapter(this, pacs);
drawerGrid.setAdapter(drawerAdapterObject);
drawerGrid.setOnItemClickListener(new DrawerClickListner(this, pacs, pm));
}
public class PacReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
set_pacs();
}
}
public void test(){
Intent test = new Intent(this,MainActivity.class);
startActivity(test);
}
}
activity_dashboard.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_dashboard"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.zyconut.socio.Dashboard">
<SlidingDrawer
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:handle="@+id/handle"
android:content="@+id/content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<ImageView
android:id="@+id/handle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@mipmap/ic_launcher"/>
<GridView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimarytrans"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="50dp"
android:horizontalSpacing="50dp"/>
</SlidingDrawer>
</RelativeLayout>
DrawerAdapter.java
public class DrawerAdapter extends BaseAdapter {
Context mContext;
Dashboard.Pac[] pacsForAdapter;
public DrawerAdapter(Context c, Dashboard.Pac pacs[]) {
mContext = c;
pacsForAdapter = pacs;
}
@Override
public int getCount() {
return pacsForAdapter.length;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
static class ViewHolder {
TextView text;
ImageView icon;
}
@Override
public View getView(int pos, View convertView, ViewGroup viewGroup) {
ViewHolder viewHolder;
LayoutInflater li = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(convertView == null) {
convertView = li.inflate(R.layout.drawer_item, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) convertView.findViewById(R.id.icon_text);
viewHolder.icon = (ImageView) convertView.findViewById(R.id.icon_image);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.icon.setImageDrawable(pacsForAdapter[pos].icon);
viewHolder.text.setText(pacsForAdapter[pos].label);
return convertView;
}
}
DrawerClickListner.java
public class DrawerClickListner implements OnItemClickListener {
Context mContext;
Dashboard.Pac[] pacsForAdapter;
PackageManager pmForListner;
public DrawerClickListner(Context c, Dashboard.Pac[] pacs, PackageManager pm) {
mContext = c;
pacsForAdapter = pacs;
pmForListner = pm;
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
Intent launchIntent = pmForListner.getLaunchIntentForPackage(pacsForAdapter[pos].name);
mContext.startActivity(launchIntent);
}
}
SortApps.java
public class SortApps {
public void exchage_sort(Dashboard.Pac[] pacs){
int i,j;
Dashboard.Pac temp;
for(i = 0; i < pacs.length; i++) {
for(j = i + 1; j < pacs.length; j++) {
if(pacs[i].label.compareToIgnoreCase(pacs[j].label) > 0) {
temp = pacs[i];
pacs[i] = pacs[j];
pacs[j] = temp;
}
}
}
}
}
drawer_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<ImageView
android:id="@+id/icon_image"
android:layout_width="65dp"
android:layout_height="65dp"
android:padding="3dp"/>
<TextView
android:id="@+id/icon_text"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:maxLines="2"
android:gravity="center_horizontal"/>
</LinearLayout>
第一次post,如有错误请多多包涵
我只需要有人告诉我如何显示仅以 'A' 开头的应用程序。休息我也许能弄明白。
提前致谢
您可以使用以下代码段获取已安装应用程序的列表。
List<String> installedApps = new ArrayList<String>();
List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);
for(int i=0;i<packs.size();i++) {
PackageInfo p = packs.get(i);
String appName = p.applicationInfo.loadLabel(getPackageManager()).toString();
installedApps.add(appName);
}
然后您可以使用此列表根据起始字母过滤应用程序。