Android Development

tibburfrogtownMobile - Wireless

Dec 14, 2013 (3 years and 8 months ago)

98 views



Android Development
Mikko Ahonen
mikko.j.ahonen@student.jyu.fi
Skype: mikko.emicode


Intents

Intents are asyncronous messages

Allow Andoid components to request functionality from other
components of Android system

Allow to combine components to perform tasks

Intents can be used to signal events for Android system

Intents can contain data

Android supports explicit and implicit intents


Explicit/Implicit Intents

Explicit intent defines which class will be called
Intent i =
new
Intent(
this
, ActivityTwo.
class
);
i.putExtra(
"Value1"
,
"This value one for ActivityTwo "
);
i.putExtra(
"Value2"
,
"This value two ActivityTwo"
);


Implicit intent do not spcify which class should be called

Specify the action which should be performed
Intent
intent
=
new
Intent(Intent.
ACTION_VIEW
,
Uri.
parse
(
"http://www.google.com"
));


This intent is received by every component that is
registered to get Intent.ACTION_VIEW and web urls


Intent data

Intents can carry extra data with them

To add data in intent use intent.putExtra()

These are key/value pairs and key is always string

Values can be primitive data types, Strings, Bundle, Parceable,
Serializable

To get data from intent

Implicit intent

getAction() and getData()

Both intents

getExtra()


Intent data

This example creates intent that triggers all components that have
registeret to send data
Intent intent =
new
Intent(Intent.
ACTION_SEND
);
intent.setType(
"text/plain"
);
intent.putExtra(android.content.Intent.
EXTRA_TEXT
,
"News for
you!"
);
startActivity(intent);


Intent data

Component that receives intent can get data:
Bundle extras = getIntent().getExtras();
if
(extras ==
null
) {
return
;
}
// Get data via the key
String value1 = extras.getString(Intent.
EXTRA_TEXT
);
if
(value1 !=
null
) {
// Do something with the data
}


Calling activities

To call activities via intents you have to define which activity will receive
intent
Intent i =
new
Intent(
this
, AnotherActivity.
class
);
startActivity
(i);

To add data:
i.putExtra(
"DataForIntent"
,
"Data from button open activity"
);

To get data back from called activity
i.putExtra(
"DataForIntent"
,
"Data from button Open Activity with
data"
);
startActivityForResult(i,
REQUEST_CODE_DATA
);

REQUEST_CODE_DATA is just some integer you choose


Calling activities

To get result from activity

Called activity adds data in intent

SetResult() adds results in intent

Caller activity gets data in onActivityResult method

Check result and REQUEST_CODE

If everything is ok do actions and get extras


Calling activities

Called activity
@Override
public

void
finish() {
Intent data =
new
Intent();
data.putExtra(
"returnValue"
,
"Retuning data"
);
setResult(
RESULT_OK
, data);
super
.finish();
}


Calling activities

Caller activity

@Override
protected

void
onActivityResult(
int
requestCode,
int
resultCode, Intent data) {

if
(requestCode ==
REQUEST_CODE_DATA
&& resultCode ==
RESULT_OK
)

{


if
(data.hasExtra(
"returnValue"
)) {


TextView textViewReceive = (TextView) findViewById(R.id.
textReceived
);


textViewReceive.setText(data.getExtras().getString(
"returnValue"
));


}

}

}


Exercise

Create main window with 3 button, 1 TextView and 1 ImageView

1. button opens activity

2. button opens activity and sends data

3. button opens activity, sends data and receives data

TextView shows returning data if available

ImageView for opening image from gallery (Do this imageview as last task if
you have spare time after completing other activities )

Second activity add textview, that shows data if main activity sends
something and button to close activity