I want to open an app chooser that will list of the installed apps on the phone. When clicking on an app in the chooser I don't need to open it but get it's package name.
I know that I can create a specific intent for example with Intent.ACTION_SEND and the
Intent.createChooser()
I also know that I can list the installed packages using the package manager.
Is there a way to combine the 2 methods and create an app chooser with all of the installed apps?
Chooser.java
public class Chooser extends ListActivity {
AppAdapter adapter=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chooser_layout);
PackageManager pm=getPackageManager();
Intent main=new Intent(Intent.ACTION_MAIN, null);
main.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> launchables=pm.queryIntentActivities(main, 0);
Collections.sort(launchables,
new ResolveInfo.DisplayNameComparator(pm));
adapter=new AppAdapter(pm, launchables);
setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v,
int position, long id) {
ResolveInfo launchable=adapter.getItem(position);
ActivityInfo activity=launchable.activityInfo;
ComponentName name=new ComponentName(activity.applicationInfo.packageName,
activity.name);
/* SetResult
String pack_name = name.getPackageName();
Intent intentMessage=new Intent();
intentMessage.putExtra("MESSAGE_package_name", pack_name);
setResult(1,intentMessage);
finish();
*/
}
class AppAdapter extends ArrayAdapter<ResolveInfo> {
private PackageManager pm=null;
AppAdapter(PackageManager pm, List<ResolveInfo> apps) {
super(Chooser.this, R.layout.row, apps);
this.pm=pm;
}
@Override
public View getView(int position, View convertView,
ViewGroup parent) {
if (convertView==null) {
convertView=newView(parent);
}
bindView(position, convertView);
return(convertView);
}
private View newView(ViewGroup parent) {
return(getLayoutInflater().inflate(R.layout.row, parent, false));
}
private void bindView(int position, View row) {
TextView label=(TextView)row.findViewById(R.id.label);
label.setText(getItem(position).loadLabel(pm));
ImageView icon=(ImageView)row.findViewById(R.id.icon);
icon.setImageDrawable(getItem(position).loadIcon(pm));
}
}
}
chooser_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageView android:id="@+id/icon"
android:drawable="@drawable/ic_launcher"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentLeft="true"
android:paddingLeft="2px"
android:paddingTop="3px"
android:paddingBottom="3px"
android:paddingRight="3px"
/>
<TextView
android:id="@+id/label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/icon"
android:textSize="18sp"
android:text="Test App will here bla bla"
android:paddingTop="2px"
android:paddingBottom="2px"
/>
</RelativeLayout>
You can choose app like this:
Intent appIntent;
...
appIntent=new Intent(this,Chooser.class);
startActivityForResult(appIntent, 1);
...
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(null!=data){
if(requestCode==1){
//Do something
String message=data.getStringExtra("MESSAGE_package_name");
package_Name.SetText(message);
}
}
This will work for you.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments