简体   繁体   English

Android Preference应用程序列表

[英]Android Preference application list

Is there a way to show the list of installed Application in a preference ? 有没有一种方法可以显示首选项中已安装的应用程序列表?

I'm making an application that starts others application via Intent : 我正在制作一个通过Intent启动其他应用程序的应用程序:

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

I need a way to get the name of the package from the ListPreference , the best would be a ListPreference containing the name of all installed applications with icons. 我需要一种从ListPreference获取软件包名称的ListPreference ,最好是一个包含所有带有图标的已安装应用程序名称的ListPreference

How is it possible? 这怎么可能?

Nevermind, i made it working using a custom adaptator, thank's to some sample code on the internet. 没关系,我使它使用自定义适配器工作,感谢Internet上的一些示例代码。 (http://blog.isys-labs.com/creating-a-custom-listpreference/) (http://blog.isys-labs.com/creating-a-custom-listpreference/)

Here is the code : 这是代码:

Custom List preference : 自定义列表首选项:

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;
    }
}

and in the preference.xml : 并在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" />

I hope it will help u 希望对您有帮助

regards 问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM