Reputation: 251
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 :
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.
How is it possible?
Upvotes: 1
Views: 1059
Reputation: 251
Nevermind, i made it working using a custom adaptator, thank's to some sample code on the internet. (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 :
<YOURPACKAGE.ApplicationSelector
android:defaultValue="Rien"
android:dependency="cbmain2"
android:entries="@array/listeChoix"
android:entryValues="@array/listeChoix"
android:key="pref22"
android:summary="Selection de l\'action à effectuer"
android:title="Application" />
I hope it will help u
regards
Upvotes: 6