Feb 8, 2013

Get application details via ResolveInfo

Via ResolveInfo return from PackageManager.queryIntentActivities(), we can get details of the application. This example demonstarte how to get Label, Icon, name, packageName and className.

Get application details via ResolveInfo


package com.example.androidlistapps;

import java.util.List;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;

public class MainActivity extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
        
        Intent intent = new Intent(Intent.ACTION_MAIN, null);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        List<ResolveInfo> intentList = getPackageManager().queryIntentActivities(intent, 0);
        
        setListAdapter(new ArrayAdapter<ResolveInfo>(
          this, 
          android.R.layout.simple_list_item_1, 
          intentList));
        
        Toast.makeText(getApplicationContext(), 
          "no of activities: " + intentList.size(), 
          Toast.LENGTH_LONG).show();
    }

 @Override
 protected void onListItemClick(ListView l, View v, int position, long id) {
  
  PackageManager packageManager = getPackageManager();
  ResolveInfo resolveInfo = (ResolveInfo)l.getItemAtPosition(position);
  ActivityInfo activityInfo = resolveInfo.activityInfo;

  AlertDialog.Builder appInfoDialog = new AlertDialog.Builder(MainActivity.this);
  
  CharSequence label = resolveInfo.loadLabel(packageManager);
  appInfoDialog.setTitle(label);
  
  //load icon
  ImageView icon = new ImageView(getApplicationContext());
     icon.setImageDrawable(resolveInfo.loadIcon(packageManager));
     LayoutParams iconLayoutParams = new LayoutParams(
       LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
     icon.setLayoutParams(iconLayoutParams);
     
     //load package and class name
  String name = activityInfo.name;
  String packageName = activityInfo.applicationInfo.packageName;
  String className = activityInfo.applicationInfo.className;
  TextView textName = new TextView(getApplicationContext());
  textName.setText("Name: " + name);
  TextView textPackageName = new TextView(getApplicationContext());
  textPackageName.setText("PackageName: " + packageName);
  TextView textClassName = new TextView(getApplicationContext());
  textClassName.setText("ClassName: " + className);
     
     LinearLayout dialogLayout = new LinearLayout(getApplicationContext());
     LayoutParams dialogLayoutParams = new LayoutParams(
       LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
     dialogLayout.setLayoutParams(dialogLayoutParams);
     
     dialogLayout.setOrientation(LinearLayout.VERTICAL);
     dialogLayout.addView(icon);
     dialogLayout.addView(textName);
     dialogLayout.addView(textPackageName);
     dialogLayout.addView(textClassName);
     
     ScrollView scrollView = new ScrollView(getApplicationContext());
     LayoutParams scrollViewLayoutParams = new LayoutParams(
       LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
     scrollView.setLayoutParams(scrollViewLayoutParams);
     scrollView.addView(dialogLayout);
     
     appInfoDialog.setView(scrollView);
         
     appInfoDialog.setPositiveButton("OK", null);
     appInfoDialog.show();
  
 }
}


Next: startActivity from ResolveInfo details


No comments:

Post a Comment

Infolinks In Text Ads