Android LogoOver the last few days, I’ve been sharing with you articles about how to create your own Android apps (hence the name of the articles :-)). On day one, I taught you how to download and install the Java Development Kit (JDK), Eclipse, and the Android SDK. One day two, I taught you how to configure Eclipse to work with the Android SDK and how to create your first Android app. Day three was spent teaching you how to create a simple layout and add some code to make your app actually do something. Today, I will teach you how to run your application on the Android emulator for testing and debugging.

Before we can jump right into running our application on the Android emulator, there is one more thing we will need to do before our app will run. To prevent applications from doing things they’re not suppose to do, Google has including a feature called “permissions” that must be set in the application and authorized by the user of the app. For standard “hello world” type apps, you usually won’t require any specific functionality that requires the user to grant permissions before your app will work. However, since I am creating an app that will need to talk to servers across the internet, I will first need to ask the user if this is ok. Plus, since we will be placing ads in our app in tomorrow’s article, our ads will also require internet access too. So, if you plan on monetizing your app by placing ads in it using the methods I will be teaching tomorrow, you will need to follow this step too.

Before Android will allow you to install an application on any device, you are presented with a list of non-standard functions the app requires. If you don’t have any objections with the application accessing the features it has listed, you can go forward with installing the app. There are several different permissions that can be implemented in an Android app. But, for now, we will only be concerned with the “android.permission.INTERNET” permission which will allow us to conduct business over the internet. To do that, open your AndroidManifest.xml file and click on the last tab labeled with the same name.

Android Manifest Tab

Somewhere between the <manifest> and </manifest> elements, you will need to add a new element for “<uses-permission android:name=“android.permission.INTERNET”></uses-permission>“. Here is what my entire AndroidManifest.xml looks like now that I have added my permission request.

Android Manifest Example

CLICK TO ENLARGE

After you save your AndroidManifest.xml, you are now ready to launch your app using the Android emulator. To do that, right click on your project in the Project Explorer panel and select Run As > Android Application. Since you already setup your AVD (Android Virtual Device) on day one, the Android emulator should launch with the settings you specified for your AVD. If you followed everything accordingly, you should also see your application running in the emulator. Here is what my app looks like running in the emulator.

Android Emulator Test

CLICK TO ENLARGE

Pretty cool, huh? If you do not see your application or it closes unexpectedly, go back to Eclipse and click on Window on the menu bar and go to Show View > Other. Expand the Android tree node, select LogCat, and click the OK button. This will open a view where you can see what happened with your app. Note: The LogCat view sometimes opens on its own when you first launch the emulator from inside Eclipse. It will be up to you to debug and fix your open apps.

Open LogCat View

CLICK TO ENLARGE

 With our app running in the Android emulator, the first thing we will want to test is our menu. To do that, click the software menu key in the upper-right corner of the screen. It’s the 3 vertical blocks just below the clock.

Android Emulator Menu Test

CLICK TO ENLARGE

This will drop down the menu you created earlier. If you followed my instructions and added an Exit menu, go ahead and click the Exit menu to close your app. To reopen your app, click the round circle at the bottom of the screen with the six squares in it. This will open your applications window where you can locate and click your app to reopen it. If you do not see your app in the list, it’s probably because it’s on another page. Just like on your cellphone, you will need to click and drag to the left to flip through the application pages. Since I have other apps loaded onto my emulator, my UDP Tester app is on the second page. So, I swipe my screen to the left one time and voila, there’s my app!

Android Emulator Apps Button

CLICK TO ENLARGE

After reopening my app from the applications window, the next thing I will need to do is test my “Send” button. Before I can do that, I will need a UDP server to test with. Even though I have live servers on the net that I can test with, I’d rather not give out their information. But, for those of you that are also creating a UDP client and need a server to test with, you are in luck. I have written articles showing how to write a UDP client and server with C# and with Python. Click here for the C# UDP server or click here for the Python UDP server. The Android UDP client will work with both the C# and the Python UDP servers. However, since we went through the trouble of adding a field in our layout for incoming UDP data, I would recommend using the Python version since it already sends a reply. If you prefer the C# version, you will have to add the missing code that will send a reply.

Before you can test your UDP client, you will need to change the destination IP to match the IP address of the computer that the UDP server is listening on. If you leave the default “127.0.0.1″ that I set in mine, the client will attempt to call a UDP server on the emulator. Since we don’t have a UDP server running on the emulator, the test will fail. If you are running your UDP server on the same computer that you are running the emulator on (and I’m sure most of you will be), you can get the IP address of your host system by holding down the Window key (it’s the key between Ctrl and Alt to the left of your spacebar) and pressing the “R” key. This will popup a “Run” dialog window. In it, type “cmd” (without the quotes) and click the “OK” button. This will launch a command shell. With that open, type “ifconfig” (again, without the quotes) and press enter. This will give you a list of the network adapters in your computer. You will need to locate the IP address that you are using to access the internet, your router, or your wireless access point. Since I am using my laptop connected to my wireless access point, I had to get the IPv4 address listed under “Wireless LAN adapter Wireless Network Connection”. It just so happens that my IP address is “192.168.0.126″. So, I type that same number into the “Dest IP” field in my Android UDP app.

Once you have entered the IP address of your host system and have entered it into the destination IP field in your app, go ahead and fire up the UDP server. Then, click the “Send” button to test that the UDP client worked successfully. If it did, you will see a new message in the “Incoming UDP Data” field. You will also see the message “Hello from client” in the output of your UDP server. Here is the output of my Python UDP server. I am also including the Python code for the UDP server so that you can see how it behaves and why I got the output I did.

UDP Server Output

CLICK TO ENLARGE

After running the UDP test in the Android emulator, here is what I saw after my Python UDP server sent its reply. You will notice that the text in my Incoming UDP Data changed to “Hello from server” to reflect what my UDP server sent. You can change the outgoing message in your UDP client and click the “Send” button again to make sure that the UDP server can see the new message.

Android UDP Client Test Success

CLICK TO ENLARGE

 Hopefully you followed everything correctly and your Android app is working as expected. Tomorrow I will teach you how to monetize your Android app by placing ads in it.

 

Create Your Own Android Apps for Fun or Profit
Part 1: Downloading and Installing Java, Eclipse, and the Android SDK
Part 2: Configuring Eclipse and Creating Your First Android App
Part 3: Creating a Layout and Adding Some Code
Part 4: Testing Your App with the Android Emulator
Part 5: Monetizing Your Android App with Google AdMob
Part 6: Running Your Android App on Cellphones and Tablets
Part 7: Publishing Your Android App to the Google Play Store

If you enjoyed this post, please consider making a donation.

Related Posts

Tagged with:  

Leave a Reply