network

Is it on? An internet connection checker snippet

SDK Version: 
M3

Most of our apps use the internet in some way, so we made an util class, that we use regularly. To save some bandwidth, I only use it after a connection related exception(ioexception etc) was thrown, but of course checking ahead is a good idea sometimes.

Gzipping files on the fly part 1.

SDK Version: 
M3

Today we wanted to test ways, to optimize the download of files to android phones.
The first way we tried, is the most obvious method of using a little less of everything (bandwith, battery, cpu, backlight, time? etc), compression.

Image source.

But what about speed? Is it worth to sacrifice the app's speed for using less bandwith? How much slower is compressing/decompressing files on a phone anyway?
Read on to find out.

Simple connection example part II - TCP communication

SDK Version: 
M3
Last time I wrote about UDP connection. I got some comments about the problem, that UDP packets are not guaranteed to be delivered.
This time I’m going to show you the safer option, the TCP connection.
TCP is probably the most commonly used protocol, simply because it is used for so many applications such as HTTP, POP, SMTP, etc. TCP is a protocol which guarantees that the receiver will receive exactly what the sender sent - there will be no errors, it will be in the correct order, everything will work just fine.

TCP communication time diagram.

  1. try {
  2.         Socket s = new Socket("http://helloandroid.com",80);
  3. } catch (UnknownHostException e) {
  4.         // TODO Auto-generated catch block
  5.         e.printStackTrace();
  6. } catch (IOException e) {

Simple UDP communication example

SDK Version: 
M3

Today I’m going to show you how to create a really easy and simple UDP message sender and receiver.


Udp communication time diagram.

The User Datagram Protocol (UDP) is one of the core members of the Internet Protocol Suite, the set of network protocols used for the Internet. With UDP, computer applications can send messages, in this case referred to as datagrams, to other hosts on an Internet Protocol (IP) network without requiring prior communications to set up special transmission channels or data paths. /wiki/

First of all let’s see the server side:

  1. String messageStr="Hello Android!";
  2. int server_port = 12345;
  3. InetAddress local = InetAddress.getByName("192.168.1.102");
  4. int msg_length=messageStr.length();
  5. byte[] message = messageStr.getBytes();

Xml remote procedure calls on android

SDK Version: 
M3

Using web services on android phones, is pretty simple. For most popular services, there is a usable library available.
Here is a little snippet for using an android library, called android-xmlrpc.

How to make a phone call from your application

SDK Version: 
M3

To enable your application to initiate a phone call, you must set permissions in the manifest file:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.       package="com.bubudsadasdas"
  4.       android:versionCode="1"
  5.       android:versionName="1.0">
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">
  7.         <activity android:name=".phonecalls"
  8.                   android:label="@string/app_name">
  9.             <intent-filter>
  10.                 <action android:name="android.intent.action.MAIN" />
  11.                 <category android:name="android.intent.category.LAUNCHER" />
  12.             </intent-filter>
  13.         </activity>
  14.  
  15.     </application>
  16.     <uses-sdk android:minSdkVersion="3" />
  17. <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
  18. </manifest>

Now create a new activity with a call method. It should look like this:
 

WebView with zoom controls, image from SDCARD

SDK Version: 
M3

The Problem:
When I'm trying to load an image (tried both gif and png) from my assets directory, into a webview.
In my assets folder, I have an image called myImage.gif
Here's my wrong code:

  1. WebView data = (WebView) findViewById(R.id.data)
  2. data.loadData("<IMG HEIGHT=\"42px\" WIDTH=\"42px\" SRC=\"file:///android_assets/myImage.gif\" />", "text/html",  "UTF-8");

All that happens when I try it, is that I get a blank screen with a
empty 42px/42px box.
In logcat, I see the message:
03-30 00:21:14.398: DEBUG/WebCore(214): Console: Not allowed to load
local resource: file:///android_assets/myImage.gif line: 0 source:

The Solution:
Even though, I'm going to load the image file from the filesystem (SDCARD or Phone Storage), instead of Assets folder, I've found a solution for this, and you don't even have to care about ContentProviders... (Which is also a good solution, but this is much easier & quicker in this case.)

Using ksoap2 for android, and parsing output data

SDK Version: 
M3

So the other day, I was asked to check out how we could use soap on Android, preferably with ksoap2 for android, and a public SOAP Web Service. For the latter the TopGoalScorers web service was chosen.

How to send email from your application

SDK Version: 
M3

Today we'll create an easy email sender application.
First of all we need to create a layout to set the address, the subject and email body box.

  1. <?xml version="1.0" encoding="utf-8"?><LinearLayout android:id="@
  2.  
  3. +id/LinearLayout01" android:layout_width="fill_parent"
  4.  
  5. android:layout_height="fill_parent"
  6.  
  7. xmlns:android="http://schemas.android.com/apk/res/android"
  8.  
  9. android:orientation="vertical"><LinearLayout android:id="@+id/LinearLayout02";
  10.  
  11. android:layout_width="wrap_content" android:layout_height="wrap_content"
  12.  
  13. android:orientation="horizontal"><EditText android:layout_width="wrap_content"
  14.  
  15. android:layout_height="wrap_content" android:width="170dip" android:id="@
  16.  
  17. +id/emailaddress"></EditText><TextView android:layout_width="wrap_content"
  18.  
  19. android:layout_height="wrap_content" android:id="@+id/emailaddress"
  20.  
  21. android:text="Email address"></TextView>
  22. </LinearLayout>
  23.  
  24.  
  25. <LinearLayout android:id="@+id/LinearLayout03";

How to download file/image from url to your device

SDK Version: 
M3

We wrote a code for download image from a website to the phone. You can work with images (and files) in your applications, but this article may be useful for java developers too.

Syndicate content