Tutorial

kingfishblacksmithMobile - Wireless

Dec 14, 2013 (3 years and 4 days ago)

82 views

Page
1


Android
Network Tutorial




Check the internet connection state by using class:
ConnectivityManager



Sending email by using class:
Intent



UDP communication by using class
DatagramSocket



TCP communication by using class
Socket


Introduction

Following the wide usage of 3G and WIFI, more and more Android Applications
need to call the internet resources
.

Checking the internet connection state is
becoming a must requirement.

Android provides the class
ConnectivityManager

for
checking internet connection state. We will use a simple example
checkNetworkInfo() to indicate how to acquire the internet connection state for
Android phone.

For
user’s
convenience,
we can directly jump
to Internet setting from
out application by the function
startActivity

if the internet is not connected
.

Android provides simple function for developers to send or receive emails. After
making sure that the internet connection is on, we can build a simple
function to
send emails

by simply using class
Intent
.
An
intent

is an abstract description of an
operation to be performed.

Intent provides a facility for performing late runtime
binding between the code in different applications. Its most significant use
is in the
launching of activities, where it can be thought of as the glue between activities. It is
basically a passive data structure holding an abstract description of an action to be
performed.


Software Requirement



Eclipse IDE



Java JDK
, JRE



Android SDK



A
A
n
n
d
d
r
r
o
o
i
i
d
d
:
:




N
N
e
e
t
t
w
w
o
o
r
r
k
k


T
T
u
u
t
t
o
o
r
r
i
i
a
a
l
l


Page
2

Android Network Tutorial



Tutorial

1.

Check Internet Connection State

Open Eclipse, click:
File
-
> New
-
> Project
. Choose
Android Project
-
> Next
.

Enter and set the following configuration in the new window

Project Name: Network

Target Name: Android 2.3.3

Package Name:
android.network

Set others

information

as default
.

Click finish.


Page
3


Android
Network Tutorial

Go to Network
-
>res
-
>layout
-
>main.xml


Since we will use Android 2.3.3, change the version of android to Android 2.3.3


Click

the Graphical Layout

tab
, drag and drop the following componen
ts into the android
phone window
.

Here are 5 components, set their properties, change the text of two textView and the button
as follows
(Mobile, WIFI, Test Connection)


The layout will be like this:

Page
4

Android Network Tutorial



Then

change the button1’s

properties
, set

on click


t
o checkNetworkInfo.

Go to
Network
-
> src
-
> android.network
-
> NetworkActivity.java

Copy and paste the following code:

package

android.network;


import

android.app.Activity;

import

android.content.Context;

import

android.net.ConnectivityManager;

import

android.net.NetworkInfo.State;

import

android.os.Bundle;

import

android.view.View;

import

android.widget.EditText;


public

class

NetworkActivity
extends

Activity {


/** Called when the activity is first created. */


private

EditText
text1
;

Page
5


Android
Network Tutorial


@Override


public

void

onCreate(Bundle savedInstanceState) {


super
.onCreate(savedInstanceState);


setContentView(R.layout.
main
);


}


public

void

checkNetworkInfo(View view)


{


view.getId();


ConnectivityManager conMan =
(ConnectivityManager)getSystemService(Context.
CONNECTIVITY_SERVICE
);


State mobile =
conMan.getNetworkInfo(ConnectivityManager.
TYPE_MOBILE
).getState();


text1

= (EditText)findViewById(R.id.
editText1
);



text1
.setText(mobile.toString());


State wifi =
conMan.getNetworkInfo(ConnectivityManager.
TYPE_WIFI
).getState();


text1

= (EditText)findViewById(R.id.
editText2
);


text1
.setText(wifi.toString());


}


}

To use the class
ConnectivityManager
, we need to add a permission in the file

“AndroidManifest.xml”.

Page
6

Android Network Tutorial


Go to Permissions tab and add a new Uses Permission.

Then set the name of Uses Permission to
android.permission.ACCESS_NETWORK_STATE

from the drop box
, save the file.



Page
7


Android
Network Tutorial

Run the project as Android Application,
you will see the result in the emulator:


We can also go to the wireless or wifi setting if neither
Mobile nor WIFI has
connected.


Just add the following code in the end of the function
checkNetworkInfo
.


if
(mobile==State.
CONNECTED
||mobile==State.
CONNECTING
)
return
;


if
(wifi==State.
CONNECTED
||wifi==State.
CONNECTING
)
return
;


startActivity(
new

Intent(Settings.
ACTION_WIRELESS_SETTINGS
));


Also remember to add the import statement for class Intent:


import

android.content.Intent;

Run the project again. You can simply press “F8” to
turn the

mobile network

on or off
. This small
image

on the top of the phone shows the internet is
on.


When
the mobile network is off, press the button
, it will jump to the wireless
setting window
:

Page
8

Android Network Tutorial



It can also jump to WIFI Setting window if you change the code

F
rom

startActivity(
new

Intent(Settings.
ACTION_WIRELESS_SETTINGS
));

To

startActivity(
new

Intent(Setti
ngs.
ACTION_WIFI_SETTINGS
));

Here is the WIFI setting window:




Page
9


Android
Network Tutorial


2.

Send email by using class Intent

Add a tableLayout
and add three more textView,
three more editText
, and one
more button
. Set the new textView to

Email

,

Subject


and

Content

, set the
button’s text to

Send

.
Set the new button’s “on click” property to
“sendEmail”.
To adjust the size of editText, right click on the editText, click
Layout Width
-
> Other.

E
nter the value you need (
for example:
200dp).

The
new layout looks like th
is:





Go to NetworkActivity.java

Add the sendEmail function:


public

void

sendEmail(View view)


{



view.getId();



Intent send=
new

Intent(Intent.
ACTION_SEND
);



text1
=(EditText)findViewById(R.id.
editText3
);



String
email=
text1
.getText().toString();



text1
=(EditText)findViewById(R.id.
editText4
);



String subject=
text1
.getText().toString();



text1
=(EditText)findViewById(R.id.
editText5
);

Page
10

Android Network Tutorial




String content=
text1
.getText().toString();



send.putExtra(Inten
t.
EXTRA_EMAIL
, email);



send.putExtra(Intent.
EXTRA_SUBJECT
, subject);



send.putExtra(Intent.
EXTRA_TEXT
, content);



send.setType(
"message/rfc822"
);



startActivity(Intent.
createChooser
(send,
"Email sent"
));


}

Save the project and run
it. We need to first create an email account in the
android phone. When the emulator starts, first click the button

to exit
from the application. Then click the button
, choose “Email” as follows:


Set up your own email account and password, after doing

that, we can go to
our application “Network” to test the email sending function
, check the
Page
11


Android
Network Tutorial

destination email

inbox
, you will receive the sent email.