Logcat

This tutorial assumes that you have either the Android SDK or AI Extras installed with access to the adb.exe command.

If you are trying to debug your application or get more info about something happening on the device, using logcat is a great way to go about doing this. A fairly common use is to identify various elements of apps that are external to yours that you would like your app to interact with. There is some good info about this on the official site at the following URL.
http://appinventor.googlelabs.com/learn/reference/other/activitystarter.html

One thing you will notice is a reference to ACTION. The depth of this topic is way out of scope for what I want to discuss here, but you can get greater detail if you follow this link:
http://developer.android.com/guide/topics/intents/intents-filters.html

When you hear mention of INTENT, that is for the most part interchangeable with ACTION. This part of the activity tells the device what you want to do and it’s usually combined with one or more elements to form the complete set of instructions. I started a project for tracking this information that includes an App Inventor app with examples, a project on Google Code and a tracking spreadsheet. All the info is on this site. Just follow the link in the top navigation "Activity Starter & Intents".

So lets take an example of identifying how to open Gmail with the To:, Subject: and Message body fields filled in.

  • Start by connecting your device and run "adb devices" from the command line.
    • If it’s not in the system path you’ll need to change directory to where it’s located
  • If your devices is detected you will see something like this
    • HT05WHL06711    device

Now you could simply run logcat but even for the smallest of tasks you could run out of buffer space and find that the info you need has already scrolled off screen. Even if that doesn’t happen you’ll find it’s not very comfortable trying to locate what you need. So the way we get around this is to pipe the output to a text file. The command line syntax to pipe output is the greater than sign. Here is the entire syntax.

  • SomeProgram.exe > filename.txt

This will create a filename.txt file in the same directory you run that command. You can use a path with the file name (i.e. c:\folder\filename.txt) to save the file eslewhere. The best way is to get your adb.exe path into the system path and this way you can run the command from any directory.

Logcat is part of the adb.exe program. The same way that devices, in adb devices is as well. All you do is run the command and pipe it to a text file.

  • adb logcat > logcat.txt

You will notice that the cursor does not return to a prompt and it just sits on the next line. If you look at the directory you will see a new file created with the name you gave it. If you open this file you will see everything that has been going on, on your device since you started capturing. It will continue capturing until you close that command prompt window or press CTRL+C to exit the last instruction. At which point the capture will end. Depending on what you’re doing and how long you need to capture, for example if you start getting into debugging you might want to watch this file. The best way to do this, is find a utility that tails a file. What this does is give you a live update of all the changes to that file. It will be like watching in the command prompt, except these tools usually have some cool features like line numbers, highlighting, filters, etc. OK, back to the topic. Once you’ve started the capture, pick up your device and run the program you need info on. In this case I’m going to start Gmail, and send an email to myself. After doing that, I hit CTRL+C and open the file. There will be a lot of information, but you can quickly find what you need by searching for "cmp". You’ll notice that there are multiple entries. In many cases you can tell which one you need based on the name of the action, package and class.

Long story short, this one is a bit tricky and if you are new to this, there is nothing wrong with a little trial an error. You’ll find that the .MAIN intent does not do what you need, but if you keep searching you’ll come across the following.

Starting activity: Intent { act=android.intent.action.VIEW dat=mailto:someone@address.com?subject=The Subject&body=The message body.

Here is a break down:

  • act= is the Action (Intent)
  • dat= is the DataUri

As it happens this actions doesn’t require a package or activityclass. In fact everything you need is right there. Check this site http://www.ianr.unl.edu/internet/mailto.html for reference and you’ll see that the instructions for generating various types of emails matches what you see in dat=. So from this all we need to build out the Activity Starter for this is a button to set the components and start the activity. The following block is a very basic example to demonstrate how it works.

SendMail

If you’re building an app you can make it dynamic by combining the use of one or more textboxes to collect what you need and use the make text block to build the DataUri. You can also use a listpicker or contact picker to pull data from the address book and use it for the To: portion, as an example. When you click the button your device will prompt you, asking which mail program to use, if you have more than one. This actually does not specifically use Gmail, so it should work on any device regardless of email client. The API automatically knows that mailto: needs to open a mail client and apply the given data to the appropriate fields.

App Inventor does not currently have a function for attaching files this way.

In many cases you will also have an ActivityPackage and ActivityClass. Here is an example of opening YouTube.

Starting activity: Intent { act=android.intent.action.MAIN cmp=com.google.android.youtube/.HomeActivity }

  • ACTION: act=
    • android.intent.action.MAIN
  • PACKAGE: cmp= up to, but not including the forward slash "/"
    • com.google.android.youtube
  • CLASS: cmp= the entire cmp string, without the forward slash. Combine the entire string
    • com.google.android.youtube.HomeActivity

Sometimes you’ll see the cmp= string is already combined after the "/", so there is no need to combine it. Just grab the part after the "/" for the CLASS. It would like like this.

  • cmp=com.google.android.youtube/com.google.android.youtube.HomeActivity

That’s all there is to it. This tutorial is probably as basic as it gets since we are not building apps that can take user input and act on results from the activity. The goal is to help you get an understanding of how the Activity Starter works and how to find what you need to interact with various apps on your device.

Play around with it and see what you can build.

You may also like...