Вы находитесь на странице: 1из 28

Intents & Phone Dialer

What is an Intent?
An Intent is Androids method for
relaying certain information from one
Activity to another.
An Intent, in simpler terms,
expresses to Android your intent to
do something.
You can think of an Intent as a
message passed between Activities.
For Example
Assume that you have an Activity that needs to open
a web browser and display a page on your Android
device.
Your Activity would send an intent to open x page in
the web browser, known as a WEB_SEARCH_ACTION
Intent, to the Android Intent Resolver.
The Intent Resolver parses through a list of Activities
and chooses the one that would best match your
Intent; in this case, the Web Browser Activity.
The Intent Resolver then passes your page to the
web browser and starts the Web Browser Activity.
Categories of Intent (1) Activity
Action Intents
Intents used to call Activities outside
of your application. Only one Activity
can handle the Intent. For example,
for a web browser, you need to open
the Web Browser Activity to display a
page.
Activity Action Intents
Categories of Intent (2)Broadcast
Intents
Intents that are sent out for multiple
Activities to handle. An example of a
Broadcast Intent would be a message
sent out by Android about the current
battery level. Any Activity can
process this Intent and react
accordinglyfor example, cancel an
Activity if the battery level is below a
certain point.
Broadcast Intent
Some of these Broadcast Intents are sent out
quite often, such as TIME_TICK_ACTION and
SIGNAL_STRENGTH_CHANGED_ACTION. Be
careful how you use them. You should try not
to receive such broadcasts if at all possible.
Intent is 1/3 of the picture
An Intent cannot actually do anything
by itself. You need Intent Filters and
Intent Receivers to listen for, and
interpret, the Intents.
Intent Receiver
An Intent Receiver is like the mailbox of an
Activity. The Intent Receiver is used to allow
an Activity to receive the specified Intent.
Using the previous web browser example, the
Web Browser Activity is set up to receive web
browser Intents. A system like this allows
unrelated Activities to ignore Intents that they
would not be able to process. It also allows
Activities that need the assistance of another
Activity to utilize that Activity without needing
to know how to call it.
Intents + Intent Receivers
With Intents and Intent Receivers,
one Activity can send out an Intent
and another can receive it.
However, there needs to be
something that governs the type of
information that can be sent between
the two Activities.
This is where Intent Filters come in.
Intent Filters
Intent Filters are used by Activities to
describe the types of Intents they
want to receive. More importantly,
they outline the type of data that
should be passed with the Intent.
Therefore, in our example scenario,
we want the web browser to open a
web page. The Intent Filter would
state that the data passed with the
WEB_SEARCH_ACTION Intent should
Using the dialer
How to use the DIAL_ACTION Intent
to open the phone dialer..?
You will pass a telephone number
with your Intent. If your application
works correctly, you should see
displayed in the dialer the number
you pass with your Intent.
MainActivity.java
public void onDialClick(View view) {
/** Create our Intent to call the Dialer */
/** Pass the Dialer the number 5551212 */
EditText tNo = (EditText)
this.findViewById(R.id.TxtNumber);
Intent DialIntent = new
Intent(Intent.ACTION_DIAL,Uri.parse("tel:"+tNo.getText()));
/** Use NEW_TASK_LAUNCH to launch the Dialer Activity */
DialIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
/** Finally start the Activity */
startActivity(DialIntent);

}
Main.xml
<Button
android:id="@+id/btnDial"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btnViewText"
android:layout_below="@+id/btnViewText"
android:onClick="onDialClick"
android:layout_marginTop="18dp"
android:text="Dial" />

<EditText
android:id="@+id/TxtNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btnDial"
android:layout_alignBottom="@+id/btnDial"
android:layout_alignParentLeft="true"
android:ems="10"
android:inputType="number" />
Button Click
Button btnCall = (Button)
this.findViewById(R.id.btnCall);
btnCall.setOnClickListener(new
Button.OnClickListener() {
public void onClick(View v){
}
}
Making a Call
Intent callIntent = new

Intent(Intent.ACTION_CALL,Uri.parse(
"tel:5551212"));

callIntent.setFlags(Intent.FLAG_ACTIV
ITY_NEW_TASK );
startActivity(callIntent);
Activity Permissions
Calling Permissions
You should find that most of the Intents match up
with a corresponding permission. The CALL_ACTION
Intent is no exception.
You need to assign your Activity the CALL_PHONE
permission to be able to execute your Intent. To
assign your Activity the correct permission, you first
need to know what permission you need to assign.
The current example is using the Dialer Activity.
Access to the Dialer Activity is governed by the
CALL_PHONE permission. By assigning this
permission to your Activity, Android will let your
Intent launch the Dialer Activity.
Adding Permissions
How do you add permissions to the
Activity?
You need to edit the Activitys
Manifest.
If you are using Eclipse, double-click
AndroidManifest.xml.
This opens the Android Manifest
Overview window, shown in the
following illustration.
To edit the Activitys permissions,
click the Permission link.
Menifest.xml
The line of most interest is at the end of the file:

<uses-permission
android:name="android.permission.CALL_PHONE"/>

This line of code was added by the Android plugin for Eclipse.
If you wanted to, you could have edited AndroidManifest.xml
directly to assign the permission. However, if there are times
when you are not sure what permission you need to add, or
the syntax with which to add it, you can use the Manifests
wizard. Now that the permission is in place, recompile and run
your Activity. Your Emulator should now be making a phone
call
Sending Message
public void onSendNoClick(View view) {
/** Create our Intent to call the Dialer */
/** Pass the Dialer the number 5551212 */
EditText tNo = (EditText)
this.findViewById(R.id.TxtNumber);
Intent intent = new Intent(this,
NewActivity.class );
intent.putExtra("msg","value");
startActivity(intent);

}
Get Message
String data =
getIntent().getExtras().getString("key
Name");

Вам также может понравиться