Professional Documents
Culture Documents
Chuong 6 - Intent - Intent Filter - New
Chuong 6 - Intent - Intent Filter - New
5) Kết nối các thành phần trong các ứng dụng khác nhau và giữa HĐH
với ứng dụng
2. Phân loại Intent
a. Explicit Intent:
Hay còn gọi là Intent công khai. Với loại Intent này tên của
Application Componnet đích phải được chỉ định và nêu rõ tên,
Ví dụ:
Intent myIntent=new Intent(MainActivity.this, ChildActivity.class);
b. Implicit Intent: hay còn gọi là Intent ẩn, Với loại Intent này
tên của Application Componnet đích không được chỉ định,
chúng ta cần thiết lập thông tin về hành động (Action), Dữ liệu
(Data) hay phân loại (category)
Ví dụ:
Intent myIntent=new Intent(Intent.ACTION_CALL, Uri.parse(“tel:55
5-2368”));
2. Phân loại Intent
3. Intent và Activity
Explicit Intent
- Dùng Intent để mở Activity khác
Explicit Intent
Explicit Intent
Button btnOpen=(Button) findViewById(R.id.btnOpenChildActivity);
btnOpen.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
doOpenChildActivity();
}
});
}
public void doOpenChildActivity()
{
Intent myIntent=new Intent(this, ChildActivity.class);
startActivity(myIntent);
}
Intent myIntent=new Intent(this, ChildActivity.class);
Đối số 1 là context hiện tại, nếu bạn muốn chắc ăn ở đâu nó cũng
hiểu thì bạn gán tên class Activity như thế này:
Intent myIntent=new Intent(MainActivity.this, ChildActivity.class);
Explicit Intent
myIntent.putExtra("MyPackage", bundle);
yourintent.getBundleExtra("MyPackage");
Explicit Intent
Đưa dữ liệu trực tiếp vào Intent để truyền
myintent.putExtra("soa",a);
myintent.putExtra("sob",b);
int a = myintent.getIntExtra("soa",1);
int b = myintent.getIntExtra("sob",1);
Explicit Intent
Explicit Intent
Intent myIntent=new Intent(MainActivity.this, ResultActivity.class);
//Khai báo Bundle
Bundle mybundle=new Bundle();
int a=Integer.parseInt(txta.getText().toString());
int b=Integer.parseInt(txtb.getText().toString());
//đưa dữ liệu riêng lẻ vào Bundle
mybundle.putInt("soa", a);
mybundle.putInt("sob", b);
//Đưa Bundle vào Intent
myIntent.putExtra("MyPackage", mybundle);
//Mở Activity ResultActivity
startActivity(myIntent);
Explicit Intent
//lấy intent gọi Activity này
Intent yourintent = getIntent();
//có intent rồi thì lấy Bundle dựa vào MyPackage
Bundle yourbundle =
yourintent.getBundleExtra("MyPackage");
//Có Bundle rồi thì lấy các thông số dựa vào key “soa”, “sob”
int a= yourbundle.getInt("soa");
int b= yourbundle.getInt("sob");
Explicit Intent
- Truyền dữ liệu và tự động lắng nghe kết quả trả về giữa các Activity
Màn hình A Màn hình B
Nhập a:
113
113 và 100
Nhập b: qua màn hình
B
100
Gửi và nhận Nhận 113 và 100, tính
kết quả Tổng của 113 và 100
Nhận lại Tổng
Kết quả ở đây
Explicit Intent
- Truyền dữ liệu và tự động lắng nghe kết quả trả về giữa các Activity`
Màn hình MainActivity Màn hình ManHinhXuLyActivity
Bước 1: Bước 2:
startActivityForResult(intent,99); intent=getIntent();
Bước 3
//Thay đổi thông tin và gán vào cho intent
//nên sử dụng lại intent (ko cần tạo mới)
intent.putExtra(“ketqua",sum);
Bước 4:
Đánh dấu kết quả trả về:
setResult(33,intent);
Bước 6 Bước 5
Nhận kết quả trong onActivityResult phải đóng màn hình này lại:
//để màn hình MainActivity trở thành
Foreground lifetime
(Chỉ nhận trong vòng đời foregroundlifetime)
//vì nó chỉ tự động nhận được kết quả trả về ở
trong Foreground lifetime
finish();
Implicit Intent
• Implicit Intent hay còn gọi là Intent ẩn, Với loại Intent này
tên của Application Componnet đích không được chỉ định,
chúng ta cần thiết lập thông tin về hành động (Action), Dữ
liệu (Data) hay phân loại (category)
Implicit Intent
• Ví dụ 1:
Intent myIntent=new Intent(Intent.ACTION_CALL, Uri.parse(“tel:5
55-2368”));
• Trong ví dụ trên, Intent chứa Action là ACTION_CALL và
Data tương ứng là số điện thoại 555-2368, hệ thống Android
sẽ tìm một ứng dụng có sẵn trong thư viện SDK để thực hiện
yêu cầu.
• Hoặc:
Intent myintent =new Intent(Intent.ACTION_CALL);
myintent.setData(Uri.parse(“tel:555-2368”));
• Ví dụ 2:
Intent myIntent=new Intent(Intent.ACTION_VIEW, Uri.parse
(“www.google.com”)
Implicit Intent
Một số ACTION thường sử dụng trong Intent:
ACTION_CALL - mở 1 Phone Dialer và ngay lập tức thực hiện cuộc gọi dựa
vào thông tin trong data URI
ACTION_DIAL - mở 1 Phone Dialer (mặc định là PD của Android) và điền
thông tin lấy từ địa chỉ chứa trong data URI
ACTION_SEND - mở 1 Activity cho phép gửi dữ liệu lấy từ data URI, kiểu của
dữ liệu xác định trong thuộc tính type
ACTION_SENDTO - mở 1 Activity cho phép gửi thông điệp (Message) tới địa
chỉ lấy từ data URI
ACTION_VIEW - action thông dụng nhất, khởi chạy activity thích hợp để hiển
thị dữ liệu trong data URI (hình ảnh, web, map...)
ACTION_IMAGE_CAPTURE/ACTION_VIDEO_CAPTURE – Mở ứng dụng cho
phép chụp ảnh/ quay video
ACTION_MAIN - sử dụng để khởi chạy 1 Activity
Demo: Action_Call & Action Sendto
Demo Implicit Intent
Bước 1: Tạo một Project mới có tên My_Contact
Bước 2: Xây dựng 3 cửa sổ giao diện
– activity_main.xml
– call_phone.xml
– send_sms.xml
Bước 3: Xây dựng Class MainActivity.java
Button Call Phone:
btnCall.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent1 =new
Intent(MainActivity.this,CallPhoneActivity.class);
startActivity(intent1); }});
Demo Implicit Intent
Button Send_SMS:
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent2 =new
Intent(MainActivity.this,SendSMSActivity.class);
startActivity(intent2); }});
Demo Implicit Intent
Bước 4: Tạo thêm Activity CallPhoneActivity
Thiết lập sự kiện cho Button Call Phone:
btnCallPhone.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
Intent callintent =new Intent(Intent.ACTION_CALL);
callintent.setData(Uri.parse("tel:"+txtNumber.getText().toStr
ing()));
startActivity(callintent); } });
Demo Implicit Intent
Bước 5: Tạo Activity SendSMSActivity:
Thiết lập sự kiện cho Button Send SMS
btnsendsms.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent smsintent = new Intent(Intent.ACTION_SENDTO,
Uri.parse("smsto:"+edtsms.getText().toString()));
startActivity(smsintent);
}
});
btnback2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
Demo Implicit Intent
• Khai báo các Activity và các quyền trong tập tin
AndroidManifest.xml
Demo Implicit Intent
• Action Image_Capture
Demo Implicit Intent
Action Image_Capture
Demo Implicit Intent
Action Image_Capture
Demo Action_View
4. Intent Filter
• Với Explicit Intent: chúng ta chỉ cần cung cấp đối
tượng nguồn và đối tượng đích cụ thể
• Còn với Implicit Intent: Android phải tìm Application
Component phù hợp để thực hiện Action chuyển phát
thông điệp (Broadcast).
• Để làm được điều này, Android sử dụng Intent Filter
như những bộ lọc để cho biết một Application
Component có thể thực hiện được những Action nào
4. Intent Filter
Review Implicit Intent
• Ví dụ 1:
Intent myIntent=new Intent(Intent.ACTION_CALL, Uri.parse(“tel:555-
2368”));
• Trong ví dụ trên, Intent chứa Action là ACTION_CALL và Data
tương ứng là số điện thoại 555-2368, hệ thống Android sẽ tìm
một ứng dụng có sẵn trong thư viện SDK để thực hiện yêu cầu.
• Hoặc:
Intent myintent =new Intent(Intent.ACTION_CALL);
myintent.setData(Uri.parse(“tel:555-2368”));
• Ví dụ 2:
Intent myIntent=new Intent(Intent.ACTION_VIEW, Uri.parse
(“www.google.com”)
4. Intent Filter
• Ví dụ: khi bạn mở một file Image hệ thống android sẽ đưa
ra một thông báo với tên các ứng dụng có thể mở file này
như là Gallery, Photos, QuikPic,….Vậy các ứng dụng đó
đã được cài đặt Intent Filter để báo cho Android rằng “tôi
có thể thực hiện công việc này”
4. Intent Filter
• Mấu chốt của vấn đề làm sao cài đặt nó? Ta chỉ cần khai
báo trong AndroidMaintifest.xml là xong!
• Ví dụ : SMS
Demo Intent Filter
Demo Intent Filter
• Đăng ký trong AndroidManifest: Trở thành 1 dịch vụ chạy ngầm, cho dù tắt phần mềm nó vẫn luôn lắng nghe bộ
lọc
IntentFilter filter = new IntentFilter(“Tên bộ lọc khai báo ở đây"
MySmsReceive receiver = new MySmsReceive();
registerReceiver(receiver,filter);
<receiver
android:name=".MySmsReceive">
<intent-filter>
<action android:name=“Tên bộ lọc khai báo tại đây"/>
</intent-filter>
</receiver>
Hủy BroadcastReceiver
• Muốn hủy BroadcastReceiver ta dùng lệnh
unregisterReceiver trong onPause
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(MySmsReceive);
}