gpt4 book ai didi

java - Android 首选项应用程序列表

转载 作者:行者123 更新时间:2023-12-01 17:32:57 24 4
gpt4 key购买 nike

有没有办法在首选项中显示已安装应用程序的列表?

我正在制作一个通过 Intent 启动其他应用程序的应用程序:

 PackageManager pm = getApplicationContext().getPackageManager();
Intent appStartIntent = pm.getLaunchIntentForPackage("NAME OF THE PACKAGE");
if (null != appStartIntent) {
getApplicationContext().startActivity(appStartIntent);
}

我需要一种方法从 ListPreference 获取包的名称,最好是包含所有已安装应用程序名称和图标的 ListPreference

这怎么可能?

最佳答案

没关系,我使用自定义适配器使其工作,这要感谢互联网上的一些示例代码。 (http://blog.isys-labs.com/creating-a-custom-listpreference/)

这是代码:

自定义列表首选项:

import java.util.List;

import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.preference.ListPreference;
import android.util.AttributeSet;

public class ApplicationSelector extends ListPreference
{
private Context contexte;

public ApplicationSelector(Context context, AttributeSet attrs) {
super(context, attrs);
contexte=context;
}

@Override
protected void onPrepareDialogBuilder(Builder builder) {
int index = findIndexOfValue(getSharedPreferences().getString(
getKey(), "1"));

AppliAdaptateur adapter = new AppliAdaptateur(contexte, this.getInstalledApplication(contexte), contexte.getPackageManager());

builder.setAdapter(adapter, this);
super.onPrepareDialogBuilder(builder);
}


public static List<ApplicationInfo> getInstalledApplication(Context c) {
return c.getPackageManager().getInstalledApplications(PackageManager.GET_META_DATA);
}


}

自定义适配器:

import java.util.List;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.plop.R;

public class AppliAdaptateur extends BaseAdapter {
private Context mContext;
private List mListAppInfo;
private PackageManager mPackManager;

public AppliAdaptateur(Context c, List list, PackageManager pm) {
mContext = c;
mListAppInfo = list;
mPackManager = pm;
}

@Override
public int getCount() {
return mListAppInfo.size();
}

@Override
public Object getItem(int position) {
return mListAppInfo.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// get the selected entry
ApplicationInfo entry = (ApplicationInfo) mListAppInfo.get(position);

// reference to convertView
View v = convertView;

// inflate new layout if null
if(v == null) {
LayoutInflater inflater = LayoutInflater.from(mContext);
v = inflater.inflate(R.layout.appliligne, null);
}

// load controls from layout resources
ImageView ivAppIcon = (ImageView)v.findViewById(R.id.ivIcon);
TextView tvAppName = (TextView)v.findViewById(R.id.tvName);
TextView tvPkgName = (TextView)v.findViewById(R.id.tvPack);

// set data to display
ivAppIcon.setImageDrawable(entry.loadIcon(mPackManager));
tvAppName.setText(entry.loadLabel(mPackManager));
tvPkgName.setText(entry.packageName);

// return view
return v;
}
}

并在preference.xml中:

<YOURPACKAGE.ApplicationSelector
android:defaultValue="Rien"
android:dependency="cbmain2"
android:entries="@array/listeChoix"
android:entryValues="@array/listeChoix"
android:key="pref22"
android:summary="Selection de l\&apos;action à effectuer"
android:title="Application" />

希望对你有帮助

问候

关于java - Android 首选项应用程序列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9266373/

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