Hai bài trước các bạn đã đi hết các cách cơ sở để viết một ứng dụng nhận Broadcast từ hệ thống. Bài hôm nay mình giới thiệu tới các bạn cách gửi một Broadcast và nhận một Broadcast không phải từ hệ thống mà từ ứng dụng này sang ứng dụng khác.
Theo hình trên Project 1 mình sẽ gửi một Broadcast, Project 2 sẽ nhận Broadcast đó và hiển thị một Toast lên màn hình.
Viết một ứng dụng nhận một Broadcast các bạn đã biết ở bài trước, nó chỉ khác duy nhất action trong thẻ <action> không phải là các action cố định mà hệ thống đã định nghĩa trước đó mà action này do ta định nghĩ
1
2
3
|
< intent-filter > < action android:name = "clbtinhoc.ictu.custom_intent" /> </ intent-filter > |
Ta sẽ đi luôn vào viết ứng dụng gửi broadcast trước, bạn tạo một project trong đó file main_activity.xml (file giao diện) có nội dung là một Button
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
android:orientation
=
"vertical"
>
<
TextView
android:id
=
"@+id/textView1"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:text
=
"Broadcast Receiver custom"
android:textAppearance
=
"?android:attr/textAppearanceLarge"
android:layout_gravity
=
"center_horizontal"
/>
<
Button
android:id
=
"@+id/button1"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:text
=
"Send Broadcast"
android:layout_gravity
=
"center_horizontal"
/>
</
LinearLayout
>
import
clbtinhoc.ictu.broadcastreceivercustom.R;
import
android.app.Activity;
import
android.content.Intent;
import
android.os.Bundle;
import
android.view.View;
import
android.widget.Button;
public
class
ActivityMain
extends
Activity{
@Override
public
void
onCreate(Bundle bundle){
super
.onCreate(bundle);
setContentView(R.layout.main_activity);
final
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View v) {
// intent kèm theo hành động có tên clbtinhoc.ictu.custom_intent
Intent intent =
new
Intent(
"clbtinhoc.ictu.custom_intent"
);
Bundle bundle =
new
Bundle();
bundle.putString(
"xau"
,
"Xin chào!"
);
intent.putExtras(bundle);
// gửi Broadcast
sendBroadcast(intent);
}
});
}
}
Đoạn code khá đơn giản nên mình chỉ chú thích duy nhất hàm gửi broadcast thôi 🙂
Tiếp theo bạn tạo một Project mới dùng để nhận broadcast. Project này khi tạo không có file Activity.java. Bạn tạo một class có tên MyProject.java sau đó extends từ BroadcastReceiver
import
android.content.BroadcastReceiver;
import
android.content.Context;
import
android.content.Intent;
import
android.os.Bundle;
import
android.widget.Toast;
public
class
MyBroadcast
extends
BroadcastReceiver{
@Override
public
void
onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
String text = bundle.getString(
"xau"
);
Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
}
}
package
=
"clbtinhoc.ictu.broadcastreceivercustom"
android:versionCode
=
"1"
android:versionName
=
"1.0"
>
<
uses-sdk
android:minSdkVersion
=
"8"
android:targetSdkVersion
=
"8"
/>
<
application
android:allowBackup
=
"true"
android:icon
=
"@drawable/ic_launcher"
android:label
=
"@string/app_name"
android:theme
=
"@style/AppTheme"
>
<
receiver
android:name
=
"clbtinhoc.ictu.MyBroadcast"
>
<
intent-filter
>
<
action
android:name
=
"clbtinhoc.ictu.custom_intent"
/>
</
intent-filter
>
</
receiver
>
</
application
>
</
manifest
>
Code không có gì mới chỉ có phần thẻ action với nội dung là “clbtinhoc.ictu.custom_intent”
Các bạn build project số hai vào máy trước sau đó build và chạy project số 1 sau đó ấn button sẽ thấy hiện một Toast.
Ở project mẫu mình sẽ hợp luôn hai project làm một project các bạn có thể download ở đây:****