Reputation: 1755
i develope such a apps in that,
Activity Display all the installed application with their name using gridview, as following method :
private ArrayList<ImageItem> getData() {
final PackageManager pm = getPackageManager();
List<ApplicationInfo> packages = pm
.getInstalledApplications(PackageManager.GET_META_DATA);
final ArrayList<ImageItem> imageItems = new ArrayList<ImageItem>();
for (ApplicationInfo packageInfo : packages)
{
if (pm.getLaunchIntentForPackage(packageInfo.packageName) != null &&
!pm.getLaunchIntentForPackage(packageInfo.packageName).equals(""))
{
try {
System.out.println("Application Label :"
+ pm.getApplicationIcon(packageInfo.packageName)
.toString());
Drawable bitmap = pm
.getApplicationIcon(packageInfo.packageName);
imageItems.add(new ImageItem(bitmap, (String) pm
.getApplicationLabel(packageInfo)));
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return imageItems;
}
it diplsay all installed application well in custom gridview, but i want to when i click on perticular gridview application icon like click on facebook , -> launch that perticular app,
how to get that specific package launcher in onItemClick of gridview to launch specific application ?
how to get specific app package name to launch apps or how to what to do in gridview to get specific app laucher package name to launch application :
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Toast.makeText(GridCameraPreviewTestActivity.this,
position + "Selected", Toast.LENGTH_SHORT).show();
}
});
Upvotes: 1
Views: 879
Reputation: 1092
Entire Code For List All Installed Application on Gridview and Launch perticular on onItemClick :
public class MainActivity extends Activity implements OnItemClickListener {
private GridView gridview;
private List<ApplicationInfo> applist = null;
private PackageManager packageManager = null;
private ApplicationAdapter listadaptor = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridview = (GridView) findViewById(R.id.grid_view);
packageManager = getPackageManager();
new LoadApplications().execute();
}
private class LoadApplications extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
applist = checkForLaunchIntent(packageManager.getInstalledApplications(PackageManager.GET_META_DATA));
Log.d("list size", String.valueOf(applist.size()));
listadaptor = new ApplicationAdapter(MainActivity.this, R.layout.list_item, applist);
System.out.println("adapter="+listadaptor);
return null;
}
@Override
protected void onCancelled() {
super.onCancelled();
}
@Override
protected void onPostExecute(Void result) {
gridview.setAdapter(listadaptor);
gridview.setOnItemClickListener(MainActivity.this);
super.onPostExecute(result);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
ApplicationInfo app = applist.get(position);
try {
Intent intent = packageManager.getLaunchIntentForPackage(app.packageName);
if (null != intent) {
startActivity(intent);
}
} catch (ActivityNotFoundException e) {
Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
private List<ApplicationInfo> checkForLaunchIntent(List<ApplicationInfo> list) {
ArrayList<ApplicationInfo> applist = new ArrayList<ApplicationInfo>();
for (ApplicationInfo info : list) {
try {
if (null != packageManager.getLaunchIntentForPackage(info.packageName)) {
applist.add(info);
}
} catch (Exception e) {
e.printStackTrace();
}
}
return applist;
}
}
ApplicationAdapter :
public class ApplicationAdapter extends ArrayAdapter<ApplicationInfo> {
private List<ApplicationInfo> appsList = null;
private Context context;
private PackageManager packageManager;
public ApplicationAdapter(Context context, int textViewResourceId,
List<ApplicationInfo> appsList) {
super(context, textViewResourceId, appsList);
this.context = context;
this.appsList = appsList;
packageManager = context.getPackageManager();
}
@Override
public int getCount() {
return ((null != appsList) ? appsList.size() : 0);
}
@Override
public ApplicationInfo getItem(int position) {
return ((null != appsList) ? appsList.get(position) : null);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (null == view) {
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.list_item, null);
}
ApplicationInfo data = appsList.get(position);
if (null != data) {
TextView appName = (TextView) view.findViewById(R.id.title);
//TextView packageName = (TextView) view.findViewById(R.id.app_paackage);
ImageView iconview = (ImageView) view.findViewById(R.id.icon);
appName.setText(data.loadLabel(packageManager));
//packageName.setText(data.packageName);
iconview.setImageDrawable(data.loadIcon(packageManager));
}
return view;
}
}
activity_main.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<GridView
android:id="@+id/grid_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
</RelativeLayout>
Inflate xml file : list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:orientation="vertical"
android:padding="5dp" >
<ImageView
android:id="@+id/icon"
android:layout_width="30dp"
android:layout_height="30dp" >
</ImageView>
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="left"
android:textColor="@android:color/white"
android:textSize="12sp" >
</TextView>
</LinearLayout>
Its Entire code that i Successfully run. Hope these helpful to other person.
Upvotes: 1
Reputation: 6612
Simple as that :
Intent intent= getPackageManager().getLaunchIntentForPackage("com.example.package");
startActivity(intent);
Upvotes: 0