Skip to content

Using Android Intents Tutorial

A Little Manifest Destiny

I’ve re-written the createAdapter method in our main activity to automatically populate the list with the other activities.

protected ListAdapter createAdapter()
    {
 
    	List<String> tempList = new ArrayList<String>();
 
    	Intent listIntent = new Intent("com.learnandroid.intents.VIEW_SAMPLE_CODE");
        listIntent.addCategory("com.learnandroid.intents.SAMPLE_CODE");
 
        PackageManager pm = getPackageManager();
        List<ResolveInfo> activityList = pm.queryIntentActivities(listIntent, 0);
 
        for (int i = 0; i < activityList.size(); i++) {
        	ResolveInfo info = activityList.get(i);
        	CharSequence labelChars = info.loadLabel(pm);
        	String displayName = labelChars != null ? labelChars.toString() : info.activityInfo.name;
        	tempList.add(displayName);
        }
 
    	String[] listValues = tempList.toArray(new String[0]);
 
    	// Create a simple array adapter (of type string) with the test values
    	ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1
    			, listValues);
 
    	return adapter;
    }

First I create an Intent using the action we specified. Next I add a category to the Intent. So at this point the intent will only use activities which have both that action and that category in their intent-filter.

Next I get an instance of PackageManager. We use this to call queryIntentActivities. This handy method will return a list of all of the activities that could be activated by the intent you pass in as a parameter. Now that we have this list, we loop through the list and get the label for each activity. I went ahead and put in some logic to get the activity name if the label isn’t present, since the label is optional. Finally, we save the results to a string array and viola! We have a dynamic list.

Dynamic List

You might have noticed that this list is shorter than the previous one. That’s because we have one more activity to create. Let’s go ahead and create a class named FinalActivity and put an entry into the Android Manifest for it.

        <activity android:name="FinalActivity" android:label="When You Just Don't Know">
            <intent-filter >
                <action android:name="com.learnandroid.intents.VIEW_SAMPLE_CODE" />
                <category android:name="com.learnandroid.intents.SAMPLE_CODE"/>                
            </intent-filter>
        </activity>

Now when you run you should see the same menu as before.

Final Dynamic List

Finally, we need to rewrite our onListItemClick method to also be dynamic. I am just going to use the same method used to list our activities.

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
    	Intent listIntent = new Intent("com.learnandroid.intents.VIEW_SAMPLE_CODE");
        listIntent.addCategory("com.learnandroid.intents.SAMPLE_CODE");
 
        PackageManager pm = getPackageManager();
        List<ResolveInfo> activityList = pm.queryIntentActivities(listIntent, 0);
 
        ResolveInfo info = activityList.get(position);
        Intent dynamicIntent = new Intent();
        dynamicIntent.setClassName(info.activityInfo.applicationInfo.packageName
            , info.activityInfo.name);
        startActivityForResult(dynamicIntent, 1700 + position);
    }

Instead of looping through all of the activities, we just use the position integer to get the selected activity. We create an empty Intent, and then use the setClassName that accepts String values to identify the class. We are just going to call all of the activities using startActivityForResult. Since we are checking for the result code RESULT_OK an activity that isn’t setup to return anything should just fail this if statement and skip our popup code.

This should be enough to get your started with a foundation for working with Intents. If you want, you can download the source code for this entire project here.

As always, please leave comments with any questions you have on the subject, or anything you would like to see elaborated on in a future article. Thank you for reading.

{ 6 } Comments

  1. shub | May 30, 2012 at 5:07 pm | Permalink

    i am new to android can you please explain what is onclicklistner (not what is does) and why we use View.onclicklistner and what is view there

  2. krishna | August 10, 2012 at 2:19 am | Permalink

    very useful, thaks..

  3. dinesh | February 10, 2014 at 3:24 am | Permalink

    wow…amazing…you give simple and easy look to android layout…..

    thank you very much

  4. fixit | April 11, 2014 at 11:56 am | Permalink

    great post, very useful for beginers

  5. Alex Butler | September 27, 2014 at 3:59 am | Permalink

    Will you accept guest post content?

  6. Sheridan | September 27, 2014 at 7:28 am | Permalink

    Sure, I would be willing to accept a guest post. Basically, my criteria would be that I should be able to open Eclipse, go through your tutorial step by step, and have it work without needed to figure out any extra stuff.

    Basically, I started this website with a friend (he wrote the lots of lists post). I was very gung ho about it… and then I found myself going through a divorce. I’ve kicked around the idea of starting to do tutorials again, and would be glad to have new content on here.

Post a Comment

Your email is never published nor shared. Required fields are marked *