Giới thiệu
Ví dụ này minh họa cách cập nhật UI Activity khi IntentService hoàn thành công việc, sử dụng LocalBroadcastManager để giao tiếp an toàn trong cùng process.
Bước 1: Tạo dự án mới
Android Studio → File → New Project.
Bước 2: Giao diện (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Khởi động Service"
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Bước 3: MainActivity với BroadcastReceiver (src/MainActivity.java)
package com.example.andy.myapplication;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView text;
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String value = intent.getStringExtra("someName");
text.setText(value); // Cập nhật UI trên Main Thread
}
};
@Override
protected void onStart() {
super.onStart();
IntentFilter filter = new IntentFilter();
filter.addAction("com.example.andy.myapplication");
LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver, filter);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = findViewById(R.id.text);
text.setOnClickListener(v -> startService(new Intent(MainActivity.this, RefreshService.class)));
}
@Override
protected void onStop() {
super.onStop();
LocalBroadcastManager.getInstance(this).unregisterReceiver(broadcastReceiver);
}
}
Bước 4: IntentService gửi broadcast (RefreshService.java)
package com.example.andy.myapplication;
import android.app.IntentService;
import android.content.Intent;
import android.os.IBinder;
import android.support.v4.content.LocalBroadcastManager;
public class RefreshService extends IntentService {
public static volatile boolean shouldStop = false;
public RefreshService() {
super(RefreshService.class.getSimpleName());
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
protected void onHandleIntent(Intent intent) {
Intent resultIntent = new Intent("com.example.andy.myapplication");
resultIntent.putExtra("someName", "Tutorialspoint.com");
LocalBroadcastManager.getInstance(this).sendBroadcast(resultIntent);
if (shouldStop) {
stopSelf();
return;
}
}
}
Bước 5: Manifest
<service android:name=".RefreshService"/>
Giải thích LocalBroadcastManager
- Giao tiếp trong cùng process (an toàn, hiệu năng cao, không cần permission)
registerReceivertrongonStart(),unregisterReceivertrongonStop()tránh leak- Action string phải khớp giữa sender và receiver
💡 Hiện đại hơn: Dùng
LiveData + ViewModel + WorkManager thay vì IntentService + BroadcastReceiver.
Chạy ứng dụng
Chạy app → Nhấn "Khởi động Service" → Service chạy xong → TextView cập nhật thành "Tutorialspoint.com".