5. INTENT
Intents allow sending or receiving data
from and to other activities or services. Intents are objects of type "android.content.Intent"
and are used to send asynchronous messages within your application or between
applications.Intents can also be used to
broadcast to the Android system that a certain event has occurred. Other
components in Android can register to this event and will get notified.
Intents
are a powerful concept as they allow the creation of loosely coupled
applications. Intents can be used to communicate between any installed
application component on the device.
An Intent object can
contain information for the receiving component. For example if your
application calls via an Intent a browser it may send the URL to the browser
component. An Intent also contain information for the Android system so that
the Android system can determine which component should handle the request.
5.1 IMPLICIT vs. EXPLICIT
Android
supports explicit intents and implicit intents.
Implicit
intents asked the system to perform a service without telling the system which
Java class should do this service. In constructing an implicit Intent you
specify the action which should be performed and optionally an URI which should
be used for this action. For example you could tell the system that you want to
view (action) a webpage (URI). By starting an intent for this data the system
would try to find an application which is registered for this event, e.g. a
browser.
Intent intent = new Intent(Intent.ACTION_DIAL);
|
Explicit
intent names the component, e.g. the Java class which should be called. i.e, in
explicit intent we define from and to, from which activity , to which activity-
here the second activity to which we navigate is given as .class. We should add the second java page in manifest file while
using explicit intent.
Intent intent = new Intent(FirstActivity.this, second.class
);
|
5.2 STARTING ACTIVITIES
For an Intent to work i.e., to perform the action the above given code is
insufficient. We need to call a method startActivity(intent) along with above code. startActivity(intent) is
called only when you don’t expect a value from called activity.
If
suppose you have a return value from called activity then instead of startActivity(intent) we use
startActivityForResult(intent) . After the called activity is finished the
program returns to a sub activity called onActivityResult()
which is implemented while programming.
In
some cases you might want to pass data from activity to other. This can be done
by using method putExtras(). This
method contains two parts/argument we could say. One is key value the second is
string value/data to be passed. To get the information fin the called activity
use getIntent() along with getExtras(). By using getString(key) we could extract the data. Also you can make use of Bundle for passing data. When using Bundle we put data in Bundle using putExtras() and Bundle in
Intent.
5.3 IMPLICIT INTENT
Example-
xml page
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/bttn1"
android:text=" call
"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/bttn2"
android:text=" sms
"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/bttn3"
android:text=" internet
"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
|
First activity page
package a.b.c;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import
android.view.View.OnClickListener;
import android.widget.Button;
public class
ImplicitintentActivity extends Activity {
Button b1,b2,b3;
/** Called when the activity is first created. */
@Override
public void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b1 = (Button)findViewById(R.id.bttn1);
b1.setOnClickListener(new
OnClickListener() {
public void
onClick(View arg0) {
// TODO Auto-generated method stub
Intent i
= new Intent(Intent.ACTION_CALL, Uri.parse("tel:5556"));
startActivity(i);
}
});
b2 = (Button)findViewById(R.id.bttn2);
b2.setOnClickListener(new
OnClickListener() {
public void
onClick(View arg0) {
// TODO Auto-generated method stub
Intent i
= new Intent(Intent.ACTION_SENDTO, Uri.parse("sms:5556"));
startActivity(i);
}
});
b3 = (Button)findViewById(R.id.bttn3);
b3.setOnClickListener(new
OnClickListener() {
public void
onClick(View arg0) {
// TODO Auto-generated method stub
Intent i
= new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(i);
}
});
}
}
|
5.4 EXPLICIT INTENT
Example 1- first activity
package a.b.c;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import
android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class
ExplicitintentActivity extends Activity {
Button b1;
EditText e1;
String s1;
/** Called when the activity is first created. */
@Override
public void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
e1 = (EditText)findViewById(R.id.ed1);
b1 = (Button)findViewById(R.id.bttn1);
b1.setOnClickListener(new
OnClickListener() {
public void
onClick(View v) {
// TODO Auto-generated method stub
s1 = e1.getText().toString();
Intent i
= new Intent(ExplicitintentActivity.this, secondPage.class);
Bundle b
= new Bundle();
b.putString("k", s1);
i.putExtras(b);
startActivity(i);
}
});
}
}
|
First xml page
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<EditText
android:id="@+id/ed1"
android:layout_width="120dip"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/bttn1"
android:text=" Go to
second page "
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
|
Second activity
page
package a.b.c;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class
secondPage extends Activity {
TextView t1;
/** Called when the activity is first created. */
@Override
public void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
t1 = (TextView)findViewById(R.id.tv1);
Intent in = getIntent();
Bundle bu =
in.getExtras();
String s = bu.getString("k");
t1.setText(s);
Toast.makeText(getApplicationContext(),
s, Toast.LENGTH_SHORT).show();
}
}
|
Second xml page
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
|
Manifest
file- In this we should add the second java page/activity as shown below. In
manifest we should add all activities which we are using in application
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="a.b.c"
android:versionCode="1"
android:versionName="1.0"
>
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
>
<activity
android:label="@string/app_name"
android:name=".ExplicitintentActivity"
>
<intent-filter >
<action android:name="android.intent.action.MAIN"
/>
<category android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
<activity android:name=".secondPage"></activity>
</application>
</manifest>
|
5.5 STARTAVTICITYFORRESULT
Example 2 – using method startActivityFor Result()
First activity page
package a.b.c;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import
android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class
Startactivity4rsltActivity extends Activity {
Button b1;
/** Called when the activity is first created. */
@Override
public void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b1 = (Button)findViewById(R.id.bttn1);
b1.setOnClickListener(new
OnClickListener() {
@Override
public void
onClick(View arg0) {
// TODO Auto-generated method stub
Intent i
= new Intent(Startactivity4rsltActivity.this, second.class);
startActivityForResult(i,
0);
}
});
}
@Override
protected void
onActivityResult(int requestCode, int resultCode, Intent data)
{
// TODO Auto-generated method
stub
super.onActivityResult(requestCode, resultCode, data);
Bundle bundle =
data.getExtras();
String s = bundle.getString("k");
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
}
}
|
Main xml page
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="START
ACTIVITY FOR RESULT"
android:textSize="20dip"
/>
<Button
android:id="@+id/bttn1"
android:text=" GO
"
android:textSize="20dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
|
Second activity page
package a.b.c;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import
android.view.View.OnClickListener;
import android.widget.Button;
public class second
extends Activity {
Button b2;
/** Called when the activity is first created. */
@Override
public void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
b2 = (Button)findViewById(R.id.bttn2);
b2.setOnClickListener(new
OnClickListener() {
@Override
public void
onClick(View v) {
// TODO Auto-generated method stub
Intent
intent = new Intent();
Bundle b
= new Bundle();
b.putString("k", "DONE");
intent.putExtras(b);
setResult(RESULT_OK, intent);
finish();
}
});
}
}
|
Main1 xml page
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:text="Enter you
name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="35dip"/>
<EditText
android:layout_width="140dip"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"/>
<Button
android:id="@+id/bttn2"
android:text=" Ok
"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
|
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="a.b.c"
android:versionCode="1"
android:versionName="1.0"
>
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
>
<activity
android:label="@string/app_name"
android:name=".Startactivity4rsltActivity"
>
<intent-filter >
<action android:name="android.intent.action.MAIN"
/>
<category android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
<activity android:name=".second"></activity>
</application>
</manifest>
|
Remember that whenever we add an Activity to our application
it should be added in manifest file.
No comments:
Post a Comment