Hshshs

 avatar
hacker98
java
5 months ago
3.3 kB
2
Indexable
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class MyForegroundService extends Service {

    private static final String CHANNEL_ID = "ForegroundServiceChannel";

    @Override
    public void onCreate() {
        super.onCreate();
        // Khởi tạo các tài nguyên nếu cần
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // Kiểm tra trạng thái kết nối Wi-Fi
        if (!isWifiConnected()) {
            Log.i("Socket", "Không có kết nối Wi-Fi, dừng service.");
            stopSelf(); // Dừng service nếu không có Wi-Fi
            return START_NOT_STICKY;
        }

        // Tạo Notification Channel (dành cho Android 8.0+)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(
                    CHANNEL_ID,
                    "Foreground Service Channel",
                    NotificationManager.IMPORTANCE_DEFAULT
            );
            NotificationManager manager = getSystemService(NotificationManager.class);
            if (manager != null) {
                manager.createNotificationChannel(channel);
            }
        }

        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE);

        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("App chạy ngầm")
                .setContentText("Ứng dụng của bạn đang chạy ngầm")
                .setSmallIcon(R.drawable.ic_notification)
                .setContentIntent(pendingIntent)
                .build();

        startForeground(1, notification);

        // Thực hiện các tác vụ nền
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (isWifiConnected()) {
                    // Thực hiện công việc nền chỉ khi có kết nối Wi-Fi
                    Log.i("Socket", "Foregroun đang chạy với Wi-Fi.");
                    try {
                        Thread.sleep(3000); // Chờ 5 giây
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                Log.i("Socket", "Mất kết nối Wi-Fi, dừng service.");
                stopSelf(); // Dừng service nếu mất kết nối Wi-Fi
            }
        }).start();

        return START_STICKY;
    }

    private boolean isWifiConnected() {
        ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        return networkInfo != null && networkInfo.isConnected();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null; // Không cần bind trong trường hợp này
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        // Hủy bỏ các tài nguyên khi service bị tắt
    }
}
Editor is loading...
Leave a Comment