Untitled
unknown
plain_text
2 years ago
5.6 kB
17
Indexable
public class BackgroundService extends Service {
private static final String CHANNEL_ID = "your_channel_id";
private static final String TAG = "MyForegroundService";
private final Handler handler = new Handler();
private final Runnable runnable = new Runnable() {
@Override
public void run() {
int[] a = {1, 2, 3, 4, 56, 7, 8, 9, 10};
for(int i = 0; i < 101 ; i++){
if(i%100 == 0){
send(getApplicationContext());
}
}
handler.postDelayed(this, 10000); // Lặp lại sau mỗi 10 giây
}
};
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Bắt đầu service và đặt nó thành foreground
startForeground(1234, NotificationHelper.createCustomNotification(this));
// Bắt đầu chạy runnable để log "Running" sau mỗi 10 giây
handler.postDelayed(runnable, 10000);
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "Destroy");
handler.removeCallbacks(runnable);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
public static void send(Context context) {
NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "Foreground Service", NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
// Tạo thông báo với giao diện người dùng tùy chỉnh
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle("My Foreground Service")
.setContentText("abc") // Sử dụng giao diện người dùng tùy chỉnh
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
notificationManager.notify(1, builder.build());
Log.d(TAG, "send");
}
}
public class RemoteViewsHelper {
public static RemoteViews createCustomNotificationView(Context context) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.custom_push);
// Thực hiện bất kỳ cài đặt tùy chỉnh nào cho giao diện người dùng ở đây, ví dụ:
// remoteViews.setTextViewText(R.id.notification_message, "Custom message");
return remoteViews;
}
}
public class NotificationHelper {
private static final String CHANNEL_ID = "your_channel_id";
public static Notification createCustomNotification(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "Foreground Service", NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
// Tạo thông báo với giao diện người dùng tùy chỉnh
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle("My Foreground Service")
.setContentText("Running")
.setCustomContentView(RemoteViewsHelper.createCustomNotificationView(context)) // Sử dụng giao diện người dùng tùy chỉnh
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
Log.d("abc", "run");
return builder.build();
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="1dp">
<!-- ImageView sẽ hiển thị -->
<ImageView
android:id="@+id/notification_icon"
android:layout_width="1dp"
android:layout_height="1dp"
android:src="@drawable/ic_launcher_background"
android:layout_centerVertical="true"
android:layout_alignParentStart="true"
android:visibility="gone"/>
<!-- TextView này có thuộc tính visibility="gone" nên sẽ ẩn -->
<TextView
android:id="@+id/notification_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@id/notification_icon"
android:layout_centerVertical="true"
android:text="My Foreground Service"
android:textSize="16sp"
android:textStyle="bold"
android:visibility="gone"/>
<!-- TextView này sẽ hiển thị -->
<TextView
android:id="@+id/notification_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/notification_title"
android:layout_toEndOf="@id/notification_icon"
android:text="Running"
android:textSize="14sp"
android:visibility="gone"/>
</RelativeLayout>
Editor is loading...