Background Service cơ bản
Service nền đơn giản nhất: không Foreground, không START_STICKY phức tạp, chỉ chạy tác vụ khi được start và dừng khi gọi stopService().
Các bước tạo
Bước 1: Tạo dự án
Android Studio → New Project → Empty Activity.
Bước 2: Layout (activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="Start Service"
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Bước 3: MainActivity.java
package com.example.andy.myapplication;
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView text = findViewById(R.id.text);
text.setOnClickListener(v -> {
if ("Started".equals(text.getText().toString())) {
text.setText("Stopped");
stopService(new Intent(this, SimpleService.class));
} else {
text.setText("Started");
startService(new Intent(this, SimpleService.class));
}
});
}
}
Bước 4: SimpleService.java
package com.example.andy.myapplication;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class SimpleService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "Service started by user", Toast.LENGTH_LONG).show();
// Thực hiện công việc nền ở đây
// Ví dụ: tải file, sync data, xử lý dữ liệu...
return START_STICKY; // Vẫn dùng STICKY để an toàn
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service destroyed by user", Toast.LENGTH_LONG).show();
}
}
Bước 5: AndroidManifest.xml
<service android:name=".SimpleService" />
Đặc điểm và hạn chế
| Đặc điểm | Mô tả |
|---|---|
| Đơn giản | Code tối thiểu, dễ hiểu |
| Không Notification | Không hiển thị trên thanh trạng thái |
| Dễ bị kill | Hệ thống ưu tiên kill khi cần RAM |
| Giới hạn Android 8+ | Chỉ chạy được vài phút khi app ở background |
Khi nào dùng?
- Tác vụ ngắn, hoàn thành nhanh (vài giây)
- Không cần chạy liên tục khi app đóng
- Học tập, prototype
Khuyến nghị: Production app nên dùng Foreground Service (có Notification) hoặc WorkManager cho tác vụ định kỳ/đảm bảo thực thi.