Feb 9, 2013

startActivity from ResolveInfo details

Last example describe how to "Get application details via ResolveInfo". According to the retrieved ResolveInfo, we can start activity specified in Intent.

    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    intent.setClassName(packageName, name);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | 
      Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
    startActivity(intent);


startActivity from ResolveInfo details
startActivity from ResolveInfo details


Full code:
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.DialogInterface;
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
  final String name = activityInfo.name;
  final String packageName = activityInfo.applicationInfo.packageName;
  final 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.setNegativeButton("Cancel", null);
     appInfoDialog.setPositiveButton("Start App", 
       new DialogInterface.OnClickListener() {
   
   @Override
   public void onClick(DialogInterface dialog, int which) {
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    intent.setClassName(packageName, name);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | 
      Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
    startActivity(intent);
    
   }
  });
     appInfoDialog.show();
  
 }
}


No comments:

Post a Comment

Infolinks In Text Ads