Download Working with Intents - Dei-Isep

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Departamento de Engenharia Informática
Minds-On
Working with Intents
Paulo Baltarejo Sousa
[email protected]
2016
1
Intents
An intent is an abstract description of an operation to be performed. It is
also a messaging object. Although intents facilitate communication between
components in several ways, there are three fundamental use-cases:
• To start an activity:
– You can start a new instance of an Activity by passing an Intent
to startActivity. The Intent describes the activity to start and
carries any necessary data.
– If you want to receive a result from the activity when it finishes,
call startActivityForResult. Your activity receives the result
as a separate Intent object in your activity’s onActivityResult
callback.
• To start a service:
– A Service is a component that performs operations in the background without a user interface. You can start a service to perform
a one-time operation (such as download a file) by passing an Intent to startService. The Intent describes the service to start
and carries any necessary data.
– If the service is designed with a client-server interface, you can
bind to the service from another component by passing an Intent
to bindService.
• To deliver a broadcast:
– A broadcast is a message that any app can receive. The system
delivers various broadcasts for system events, such as when the
system boots up or the device starts charging. You can deliver a
broadcast to other apps by passing an Intent to sendBroadcast.
1.1
Intent Types
There are two types of intents:
• Explicit intents specify the component to start by name (the fullyqualified class name). You’ll typically use an explicit intent to start a
component in your own app, because you know the class name of the
activity or service you want to start. For example, start a new activity
in response to a user action or start a service to download a file in the
background.
1
• Implicit intents do not name a specific component, but instead declare a general action to perform, which allows a component from another app to handle it. For example, if you want to show the user
a location on a map, you can use an implicit intent to request that
another capable app show a specified location on a map.
1.2
Building an Intent
An Intent object carries information that the Android system uses to determine which component to start (such as the exact component name or
component category that should receive the intent), plus information that
the recipient component uses in order to properly perform the action (such
as the action to take and the data to act upon).
The primary information contained in an Intent is the following:
• Component name: The name of the component to start.
• Action: A string that specifies the generic action to perform (such as
view or pick).
• Data: The URI (a Uri object) that references the data to be acted
on and/or the MIME type of that data. The type of data supplied is
generally dictated by the intent’s action.
• Category: A string containing additional information about the kind
of component that should handle the intent.
• Extras: Key-value pairs that carry additional information required to
accomplish the requested action.
1.3
Intent examples
An explicit intent is one that you use to launch a specific app component,
such as a particular activity or service in your app. To create an explicit
intent, define the component name for the Intent object?all other intent
properties are optional.
For example, if you built a service in your app, named DownloadService,
designed to download a file from the web, you can start it with the following
code:
// Executed in an Activity , so ’ this ’ is the Context
// The fileUrl is a string URL , such as " http :// www . example . com / image . png "
Intent down loadIn tent = new Intent ( this , D ow n lo ad S er v ic e . class ) ;
dow nloadI ntent . setData ( Uri . parse ( fileUrl ) ) ;
startService ( d ownloa dInten t ) ;
2
The Intent(Context, Class) constructor supplies the app Context and
the component a Class object. As such, this intent explicitly starts the
DownloadService class in the app. A Context is a handle to the system.
Context is a interface to global information about an application environment. Context is an abstract class whose implementation is provided by
the Android system. It allows access to application-specific resources and
classes, as well as up-calls for application-level operations such as launching
activities, broadcasting and receiving intents, etc.
An implicit intent specifies an action that can invoke any app on the
device able to perform the action. Using an implicit intent is useful when
your app cannot perform the action, but other apps probably can and you’d
like the user to pick which app to use.
For example, if you have content you want the user to share with other
people, create an intent with the ACTION_SEND action and add extras that
specify the content to share. When you call startActivity with that intent,
the user can pick an app through which to share the content.
// Create the text message with a string
Intent sendIntent = new Intent () ;
sendIntent . setAction ( Intent . ACTION_SEND ) ;
sendIntent . putExtra ( Intent . EXTRA_TEXT , textMessage ) ;
sendIntent . setType ( " text / plain " ) ;
// Verify that the intent will resolve to an activity
if ( sendIntent . re so l ve Ac t iv i ty ( g e t P a c k a g e M a n a g e r () ) != null ) {
startActivity ( sendIntent ) ;
}
1.4
IntentFilter component
An IntentFilter is a component that specifies the type of intents that the
component would like to receive. For example, by declaring an intent filter
for an activity, you make it possible for other apps to directly start your
activity with a certain kind of intent. If you do not declare any intent filters
for an activity, then it can be started only with an explicit intent. IntentFilter
component is only used for implicit intents.
Its purpose is to advertise which implicit intents your app can receive,
declare one or more intent filters for each of your app components with an
<intent-filter> element in your manifest file.
Each intent filter specifies the type of intents it accepts based on the
3
intent’s action, data, and category. The system will deliver an implicit
intent to your app component only if the intent can pass through one of your
intent filters.
< activity android : name = " MainActivity " >
<! - - This activity is the main entry , should appear in app launcher -->
< intent - filter >
< action android : name = " android . intent . action . MAIN " / >
< category android : name = " android . intent . category . LAUNCHER " / >
</ intent - filter >
</ activity >
< activity android : name = " ShareActivity " >
<! - - This activity handles " SEND " actions with text data -->
< intent - filter >
< action android : name = " android . intent . action . SEND " / >
< category android : name = " android . intent . category . DEFAULT " / >
< data android : mimeType = " text / plain " / >
</ intent - filter >
<! - - This activity also handles " SEND " and " SEND_MULTIPLE " with media
data -->
< intent - filter >
< action android : name = " android . intent . action . SEND " / >
< action android : name = " android . intent . action . SEND_MULTIPLE " / >
< category android : name = " android . intent . category . DEFAULT " / >
< data android : mimeType = " application / vnd . google . panorama360 + jpg " / >
< data android : mimeType = " image /* " / >
< data android : mimeType = " video /* " / >
</ intent - filter >
</ activity >
The first activity, MainActivity, is the app’s main entry point?the activity that opens when the user initially launches the app with the launcher
icon:
• The ACTION_MAIN action indicates this is the main entry point and does
not expect any intent data.
• The CATEGORY_LAUNCHER category indicates that this activity’s icon
should be placed in the system’s app launcher. If the <activity>
element does not specify an icon with icon, then the system uses the
icon from the <application> element.
These two must be paired together in order for the activity to appear in
the app launcher.
The second activity, ShareActivity, is intended to facilitate sharing text
and media content. Although users might enter this activity by navigating
to it from MainActivity, they can also enter ShareActivity directly from
another app that issues an implicit intent matching one of the two intent
filters.
4
2
Implementing Intent test app
2.1
Create a project
Start a new Android project.
1. Select File > New > New Project....
2. Fill in the project details with the following values:
• New Project: Application name: Intent test
• Target Android Devices: Phone and Tablet
• Target Android Devices: Minimum SDK: API10
• Add an Activity to Mobile: Empty Activity
• ...
2.2
Creating Activities
Create three more activities:Main2Activity, Main3Activity and Main4Activity.
5
In the end, the project structure is like this:
2.3
Changing MainActivity UI layout
From Android studio, open activity_main.xml file and change it to:
6
<? xml version = " 1.0 " encoding = " utf -8 " ? >
< Relati veLayo ut xmlns : android = " http :// schemas . android . com / apk / res / android "
xmlns : tools = " http :// schemas . android . com / tools "
android : layout_width = " match_parent "
android : layout_height = " match_parent "
android : paddingBottom = " @dimen / a c t i v i t y _ v e r t i c a l _ m a r g i n "
android : paddingLeft = " @dimen / a c t i v i t y _ h o r i z o n t a l _ m a r g i n "
android : paddingRight = " @dimen / a c t i v i t y _ h o r i z o n t a l _ m a r g i n "
android : paddingTop = " @dimen / a c t i v i t y _ v e r t i c a l _ m a r g i n "
tools : context = " pt . androiddoc . intenttest . MainActivity " >
< TextView
android : layout_width = " wrap_content "
android : layout_height = " wrap_content "
android : text = " New Text "
android : id = " @ + id / textView "
android : l a y o u t _ a l i g n P a r e n t T o p = " true "
android : l a y o u t _ a l i g n P a r e n t L e f t = " true "
android : l a y o u t _ a l i g n P a r e n t S t a r t = " true " / >
< Button
android : layout_width = " match_parent "
android : layout_height = " wrap_content "
android : text = " Open Activity ( passing data ) "
android : id = " @ + id / button "
android : layout_below = " @ + id / textView "
android : l a y o u t _ a l i g n P a r e n t L e f t = " true "
android : l a y o u t _ a l i g n P a r e n t S t a r t = " true "
android : l a y o u t _ m a r g i nT o p = " 40 dp " / >
< Button
android : layout_width = " match_parent "
android : layout_height = " wrap_content "
android : text = " Open Activity ( receiving data ( int ) ) "
android : id = " @ + id / button2 "
android : layout_below = " @ + id / button "
android : l a y o u t _ a l i g n P a r e n t L e f t = " true "
android : l a y o u t _ a l i g n P a r e n t S t a r t = " true "
android : l a y o u t _ m a r g i nT o p = " 80 dp " / >
< Button
android : layout_width = " match_parent "
android : layout_height = " wrap_content "
android : text = " Open Activity ( receiving data ( str ) ) "
android : id = " @ + id / button3 "
android : layout_below = " @ + id / button2 "
android : l a y o u t _ a l i g n P a r e n t L e f t = " true "
android : l a y o u t _ a l i g n P a r e n t S t a r t = " true "
android : l a y o u t _ m a r g i nT o p = " 90 dp " / >
< Button
android : layout_width = " match_parent "
android : layout_height = " wrap_content "
android : text = " Open Activity ( from other app ) "
android : id = " @ + id / button4 "
android : l a y o u t _ a l i g n P a r e n t B o t t o m = " true "
android : l a y o u t _ a l i g n P a r e n t L e f t = " true "
android : l a y o u t _ a l i g n P a r e n t S t a r t = " true "
android : l a y o u t _ m a r g i n B o t t o m = " 120 dp " / >
7
</ RelativeLayout >
The MainActivity UI layout is composed by one TextView element and
four Button elements.
2.4
Passing data from MainActivity to Main2Activity
Here, it is described the procedure for passing data from the active activity
(in foreground), MainActivity, to newly created activity, Main2Activity.
2.4.1
Changing Main2Activity UI layout
From Android studio, open activity_main2.xml file and change it to:
8
<? xml version = " 1.0 " encoding = " utf -8 " ? >
< Relati veLayo ut xmlns : android = " http :// schemas . android . com / apk / res / android "
xmlns : tools = " http :// schemas . android . com / tools "
android : layout_width = " match_parent "
android : layout_height = " match_parent "
android : paddingBottom = " @dimen / a c t i v i t y _ v e r t i c a l _ m a r g i n "
android : paddingLeft = " @dimen / a c t i v i t y _ h o r i z o n t a l _ m a r g i n "
android : paddingRight = " @dimen / a c t i v i t y _ h o r i z o n t a l _ m a r g i n "
android : paddingTop = " @dimen / a c t i v i t y _ v e r t i c a l _ m a r g i n "
tools : context = " pt . androiddoc . intenttest . Main2Activity " >
< TextView
android : layout_width = " wrap_content "
android : layout_height = " wrap_content "
android : text = " New Text "
android : id = " @ + id / textView2 "
android : l a y o u t _ a l i g n P a r e n t T o p = " true "
android : l a y o u t _ a l i g n P a r e n t L e f t = " true "
android : l a y o u t _ a l i g n P a r e n t S t a r t = " true " / >
</ RelativeLayout >
This UI layout is composed by one TextView element. This element is used
to show the data received from Main2Activity.
2.4.2
Passing data to Main2Activity
From Android studio, open MainActivity.java file and change it to:
public class MainActivity extends A p p C o m p a t A c t i v i t y {
@Override
protected void onCreate ( Bundle s a v e d I n s t a n c e S t a t e ) {
super . onCreate ( s a v e d I n s t a n c e S t a t e ) ;
set Conten tView ( R . layout . activity_main ) ;
Button bt = ( Button ) findViewById ( R . id . button ) ;
bt . s e t O n C l i c k L i s t e n e r ( new View . On Cl i ck L is te n er () {
@Override
public void onClick ( View v ) {
Intent intent = new Intent ( MainActivity . this , Main2Activity .
class ) ;
intent . putExtra ( " EXTRA_INT " , 1234) ;
intent . putExtra ( " EXTRA_STR " , " Hello Android !!! " ) ;
startActivity ( intent ) ;
}
}) ;
}
}
Intent has been created to open Main2Activity. putExtra method is used
to pass an integer and a string data parameter. It is done using key-value
pair. First parameter is key(name), the second (value). The key is always a
9
String. As value it can be used the primitive data types int, float, char
and etc. It can also pass Parceable and Serializable objects.
2.4.3
Main2Activity receiving data
From Android studio, open MainActivity.java file and change it to:
public class Main2Activity extends A p p C o m p a t A c t i v i t y {
@Override
protected void onCreate ( Bundle s a v e d I n s t a n c e S t a t e ) {
super . onCreate ( s a v e d I n s t a n c e S t a t e ) ;
setC ontent View ( R . layout . acti vity_m ain2 ) ;
Intent intent = getIntent () ;
int x = intent . getIntExtra ( " EXTRA_INT " , 0) ;
String str = intent . g etStri ngExtr a ( " EXTRA_STR " ) ;
TextView tv = ( TextView ) findViewById ( R . id . textView2 ) ;
tv . setText ( " Data received : " + x + " : " + str ) ;
}
}
Every Activity has an Intent. getIntent method is used to get Intent used
to start Main2Activity and methods getIntExtra and getStringExtra
to retrieve the data parameters that this intent is carrying on. To retrieve
Serializable and Parcelable object using getSerializableExtra and
getParcelableExtra methods, respectively.
2.4.4
Running
1. To run the app from Android Studio: Click Run from the toolbar.
2. Click on Open Activity (passing data) button.
10
2.5
Receiving data from Main3Activity and Main4Activity
Here, it is described the procedure to receive data from a foreground activity.
That is, when the foreground activity finishes, it returns data to the activity
that has launched it.
11
2.5.1
Launching Main3Activity or Main4Activity
From Android studio, open MainActivity.java file and change it to:
12
public class MainActivity extends A p p C o m p a t A c t i v i t y {
static final int R E Q U E S T _ C O D E _ M A I N 3 A C T I V I T Y = 1;
static final int R E Q U E S T _ C O D E _ M A I N 4 A C T I V I T Y = 2;
// The request code
// The request code
@Override
protected void onCreate ( Bundle s a v e d I n s t a n c e S t a t e ) {
super . onCreate ( s a v e d I n s t a n c e S t a t e ) ;
set Conten tView ( R . layout . activity_main ) ;
...
Button bt2 = ( Button ) findViewById ( R . id . button2 ) ;
bt2 . s e t O n C l i c k L i s t e n e r ( new View . O nC li c kL is t en e r () {
@Override
public void onClick ( View v ) {
Intent intent = new Intent ( MainActivity . this , Main3Activity .
class ) ;
s t a r t A c t i v i t y F o r R e s u l t ( intent , R E Q U E S T _ C O D E _ M A I N 3 A C T I V I T Y ) ;
}
}) ;
Button bt3 = ( Button ) findViewById ( R . id . button3 ) ;
bt3 . s e t O n C l i c k L i s t e n e r ( new View . O nC li c kL is t en e r () {
@Override
public void onClick ( View v ) {
Intent intent = new Intent ( MainActivity . this , Main4Activity .
class ) ;
s t a r t A c t i v i t y F o r R e s u l t ( intent , R E Q U E S T _ C O D E _ M A I N 4 A C T I V I T Y ) ;
}
}) ;
}
}
Regarding to the Intent objects, there is nothing special about them.
However, instead of using startActivity method, you have to use the
startActivityForResult method. Theis method has two arguments, the
Intent object and an integer. In this case, the REQUEST_CODE_MAIN3ACTIVITY
and REQUEST_CODE_MAIN4ACTIVITY with values 1 and 2, respectively. The
integer request code passed as the second argument identifies the request,
which activity you started. When the MainActivity receive the result Intent from the Main3Activity or Main4Activity back, the same request code
is also sent back so that it can be properly identify the result and decide how
to handle it.
2.5.2
Changing Main3Activity and Main4Activity UI layouts
From Android studio, open activity_main3.xml file and change it to:
13
<? xml version = " 1.0 " encoding = " utf -8 " ? >
< Relati veLayo ut xmlns : android = " http :// schemas . android . com / apk / res / android "
xmlns : tools = " http :// schemas . android . com / tools "
android : layout_width = " match_parent "
android : layout_height = " match_parent "
android : paddingBottom = " @dimen / a c t i v i t y _ v e r t i c a l _ m a r g i n "
android : paddingLeft = " @dimen / a c t i v i t y _ h o r i z o n t a l _ m a r g i n "
android : paddingRight = " @dimen / a c t i v i t y _ h o r i z o n t a l _ m a r g i n "
android : paddingTop = " @dimen / a c t i v i t y _ v e r t i c a l _ m a r g i n "
tools : context = " pt . androiddoc . intenttest . Main3Activity " >
< Button
android : layout_width = " match_parent "
android : layout_height = " wrap_content "
android : text = " Send an int "
android : id = " @ + id / button5 "
android : l a y o u t _ a l i g n P a r e n t T o p = " true "
android : l a y o u t _ a l i g n P a r e n t L e f t = " true "
android : l a y o u t _ a l i g n P a r e n t S t a r t = " true " / >
</ RelativeLayout >
Open activity_main4.xml file and change it to:
14
<? xml version = " 1.0 " encoding = " utf -8 " ? >
< Relati veLayo ut xmlns : android = " http :// schemas . android . com / apk / res / android "
xmlns : tools = " http :// schemas . android . com / tools "
android : layout_width = " match_parent "
android : layout_height = " match_parent "
android : paddingBottom = " @dimen / a c t i v i t y _ v e r t i c a l _ m a r g i n "
android : paddingLeft = " @dimen / a c t i v i t y _ h o r i z o n t a l _ m a r g i n "
android : paddingRight = " @dimen / a c t i v i t y _ h o r i z o n t a l _ m a r g i n "
android : paddingTop = " @dimen / a c t i v i t y _ v e r t i c a l _ m a r g i n "
tools : context = " pt . androiddoc . intenttest . Main4Activity " >
< EditText
android : layout_width = " match_parent "
android : layout_height = " wrap_content "
android : id = " @ + id / editText "
android : l a y o u t _ a l i g n P a r e n t T o p = " true "
android : l a y o u t _ a l i g n P a r e n t L e f t = " true "
android : l a y o u t _ a l i g n P a r e n t S t a r t = " true "
/>
< Button
android : layout_width = " match_parent "
android : layout_height = " wrap_content "
android : text = " Send a message "
android : id = " @ + id / button6 "
android : layout_below = " @ + id / editText "
android : l a y o u t _ a l i g n P a r e n t L e f t = " true "
android : l a y o u t _ a l i g n P a r e n t S t a r t = " true "
android : l a y o u t _ m a r g i nT o p = " 20 dp " / >
< Button
android : layout_width = " match_parent "
android : layout_height = " wrap_content "
android : text = " Do not send "
android : id = " @ + id / button7 "
android : layout_below = " @ + id / button6 "
android : l a y o u t _ a l i g n P a r e n t L e f t = " true "
android : l a y o u t _ a l i g n P a r e n t S t a r t = " true "
android : l a y o u t _ m a r g i nT o p = " 30 dp " / >
</ RelativeLayout >
2.5.3
Main3Activity and Main4Activity returning data to MainActivity
From Android studio, open Main3Activity.java file and change it to:
15
public class Main3Activity extends A p p C o m p a t A c t i v i t y {
@Override
protected void onCreate ( Bundle s a v e d I n s t a n c e S t a t e ) {
super . onCreate ( s a v e d I n s t a n c e S t a t e ) ;
set Conten tView ( R . layout . acti vity_m ain3 ) ;
Button bt = ( Button ) findViewById ( R . id . button5 ) ;
bt . s e t O n C l i c k L i s t e n e r ( new View . On Cl i ck L is te n er () {
@Override
public void onClick ( View v ) {
Intent intent = new Intent () ;
intent . putExtra ( " EXTRA_INT " ,325) ;
setResult ( Activity . RESULT_OK , intent ) ;
finish () ;
}
}) ;
}
}
In order to return back data to the MainActivity, it has to create an Intent
object with no arguments. Put the data into it. Set the result code. Typically, the result codes RESULT_OK and RESULT_CANCELLED basically means
operation succeeded and cancelled respectively. However, the response code
can be some other user defined custom value too, not just these. The finish
method close the activity. Its behaviour is similar to the press of Back
Button.
Open Main4Activity.java file and change it to:
16
public class Main4Activity extends A p p C o m p a t A c t i v i t y {
EditText et ;
@Override
protected void onCreate ( Bundle s a v e d I n s t a n c e S t a t e ) {
super . onCreate ( s a v e d I n s t a n c e S t a t e ) ;
set Conten tView ( R . layout . act ivity_ main4 ) ;
et = ( EditText ) findViewById ( R . id . editText ) ;
et . setText ( " Write a message " ) ;
Button bt = ( Button ) findViewById ( R . id . button6 ) ;
bt . s e t O n C l i c k L i s t e n e r ( new View . On Cl i ck Li s te ne r () {
@Override
public void onClick ( View v ) {
String str = et . getText () . toString () ;
Intent intent = new Intent () ;
intent . putExtra ( " EXTRA_STR " , str ) ;
setResult ( Activity . RESULT_OK , intent ) ;
finish () ;
}
}) ;
Button bt2 = ( Button ) findViewById ( R . id . button7 ) ;
bt2 . s e t O n C l i c k L i s t e n e r ( new View . O nC li c kL is t en e r () {
@Override
public void onClick ( View v ) {
Intent intent = new Intent () ;
setResult ( Activity . RESULT_CANCELED , intent ) ;
finish () ;
}
}) ;
}
}
2.5.4
MainActivity receiving data from Main3Activity and Main4Activity
From Android studio, open MainActivity.java file and change it to:
17
public class MainActivity extends A p p C o m p a t A c t i v i t y {
static final int R E Q U E S T _ C O D E _ M A I N 3 A C T I V I T Y = 1;
static final int R E Q U E S T _ C O D E _ M A I N 4 A C T I V I T Y = 2;
// The request code
// The request code
@Override
protected void onCreate ( Bundle s a v e d I n s t a n c e S t a t e ) {
super . onCreate ( s a v e d I n s t a n c e S t a t e ) ;
set Conten tView ( R . layout . activity_main ) ;
...
Button bt2 = ( Button ) findViewById ( R . id . button2 ) ;
bt2 . s e t O n C l i c k L i s t e n e r ( new View . O nC li c kL is t en e r () {
@Override
public void onClick ( View v ) {
Intent intent = new Intent ( MainActivity . this , Main3Activity .
class ) ;
s t a r t A c t i v i t y F o r R e s u l t ( intent , R E Q U E S T _ C O D E _ M A I N 3 A C T I V I T Y ) ;
}
}) ;
Button bt3 = ( Button ) findViewById ( R . id . button3 ) ;
bt3 . s e t O n C l i c k L i s t e n e r ( new View . O nC li c kL is t en e r () {
@Override
public void onClick ( View v ) {
Intent intent = new Intent ( MainActivity . this , Main4Activity .
class ) ;
s t a r t A c t i v i t y F o r R e s u l t ( intent , R E Q U E S T _ C O D E _ M A I N 4 A C T I V I T Y ) ;
}
}) ;
}
@Override
protected void o n A c t i v i t y R e s u l t ( int requestCode , int resultCode , Intent
data )
{
super . o n A c t i v i t y R e s u l t ( requestCode , resultCode , data ) ;
TextView tv = ( TextView ) findViewById ( R . id . textView ) ;
switch ( requestCode ) {
case R E Q U E S T _ C O D E _ M A I N 3 A C T I V I T Y :
if ( resultCode == Activity . RESULT_OK ) {
int x = data . getIntExtra ( " EXTRA_INT " ,0) ;
tv . setText ( " Data received from Main3Activity : " + x ) ;
}
break ;
case R E Q U E S T _ C O D E _ M A I N 4 A C T I V I T Y :
switch ( resultCode ) {
case Activity . RESULT_OK :
String str = data . getSt ringEx tra ( " EXTRA_STR " ) ;
tv . setText ( " Data received from Main4Activity : " + str
);
break ;
case Activity . R ES U LT _C A NC E LE D :
tv . setText ( " No data received from Main4Activity " ) ;
break ;
}
break ;
default :
}
18
}
}
Main3Activity and Main4Activity are launched using startActivityFor
Result method. Therefore, to receive data from them, in the MainActivity,
the onActivityResult method has to be override.
onActivityResult has three arguments: int requestCode, int resultCode,
Intent data
In the startActivityForResult method call you can specify a request
code (in this case REQUEST_CODE_MAIN3ACTIVITY and REQUEST_CODE_MAIN4ACTIVITY)
to determine which activity has been started. This request code is returned
to the MainActivity in the argument requestCode of onActivityResult
method. The started activity set a result code (in this case RESULT_OK or
RESULT_CANCELLED) which the MainActivity can use to determine if the activity was cancelled or not. This result code is instantiated in the argument
resultCode of onActivityResult method. The returned data is carried on
the Intent object, the data argument of the onActivityResult method.
2.5.5
Running
1. To run the app from Android Studio: Click Run from the toolbar.
2. Click on Open Activity (receiving data (int) button.
3. Click on Send an int button.
4. To run the app from Android Studio: Click Run from the toolbar.
5. Click on Open Activity (receiving data (str) button.
19
6. Write a message
7. Click on Send a message button.
2.6
Opening phone app
2.6.1
Requesting an action
From Android studio, open MainActivity.java file and change it to:
public class MainActivity extends A p p C o m p a t A c t i v i t y {
...
@Override
protected void onCreate ( Bundle s a v e d I n s t a n c e S t a t e ) {
super . onCreate ( s a v e d I n s t a n c e S t a t e ) ;
setC ontent View ( R . layout . activity_main ) ;
...
Button bt4 = ( Button ) findViewById ( R . id . button4 ) ;
bt4 . s e t O n C l i c k L i s t e n e r ( new View . O nC li c kL i st en e r () {
@Override
public void onClick ( View v ) {
Intent intent = new Intent ( Intent . ACTION_DIAL ) ;
intent . setData ( Uri . parse ( " tel :987654321 " ) ) ;
startActivity ( intent ) ;
}
}) ;
}
...
}
20
Implicit intents are used to specify an action and data. Android system
searches for all the registered components (through intent filters) for the specific action and data. If only one component is found, that is triggered but
when several of them are identified a dialog is presented to the user from
which he can make a suitable selection.
2.6.2
Running
1. To run the app from Android Studio: Click Run from the toolbar.
2. Click on Open Activity (from other app) button.
21