Android Preference应用程序列表

时间:2012-02-13 19:06:21

标签: java android android-preferences

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

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

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

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

怎么可能?

1 个答案:

答案 0 :(得分:5)

没关系,我使用自定义调整器使其工作,感谢互联网上的一些示例代码。 (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);
    }


}

Custom Adaptator:

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" />

我希望它能帮助你

问候