Android前台服务 - 在设置的时间后触发操作
创始人
2024-10-09 14:35:35
0

以下是一个示例代码,演示如何在Android前台服务中设置一个定时器,在指定的时间触发操作:

首先,在AndroidManifest.xml文件中,确保已添加必要的权限:



接下来,在MainActivity.java中,创建一个前台服务类ForegroundService.java:

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.os.SystemClock;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;

import java.util.Timer;
import java.util.TimerTask;

public class ForegroundService extends Service {

    private static final int NOTIFICATION_ID = 1;
    private static final long TRIGGER_TIME = 5000; // 5 seconds

    private Timer timer;

    @Override
    public void onCreate() {
        super.onCreate();
        startForeground(NOTIFICATION_ID, buildNotification());
        startTimer();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        stopTimer();
    }

    private Notification buildNotification() {
        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "channel_id")
                .setContentTitle("Foreground Service")
                .setContentText("Running...")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentIntent(pendingIntent)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder.setChannelId("channel_id");
        }

        return builder.build();
    }

    private void startTimer() {
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                // 在指定的时间后执行操作
                doSomething();
            }
        }, TRIGGER_TIME);
    }

    private void stopTimer() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
    }

    private void doSomething() {
        // 在这里执行你的操作
        Toast.makeText(this, "操作已触发", Toast.LENGTH_SHORT).show();
    }
}

最后,在MainActivity.java的onCreate()方法中,启动前台服务:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent serviceIntent = new Intent(this, ForegroundService.class);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        startForegroundService(serviceIntent);
    } else {
        startService(serviceIntent);
    }
}

这样,当应用启动后,前台服务将在5秒后触发操作,并显示一个通知栏来表示服务正在运行。

相关内容

热门资讯

透视辅助(微扑克辅助器ios)... 一、简介了解软件请加微:136704302是一款在线扑克游戏平台,玩家可以在平台上进行多人在线扑克游...
wepoke确实有挂!we-p... wepoke确实有挂!we-poke有挂吗,(WpK)原本是真的有挂(详细辅助2025新版)准备好在...
透视规律(aa扑克辅助)aa扑... 1、透视规律(aa扑克辅助)aa扑克有外挂吗(详细辅助实用技巧)先前是真的有挂;代表性(透视辅助软件...
wepoke ai辅助!wpk... wepoke ai辅助!wpk微扑克模拟器,(德州aa扑克)一贯是真的有挂(详细辅助细节方法)1、完...
wepoke确实有挂!德州ai... wepoke确实有挂!德州ai智能辅助,(微扑克)切实是有挂(详细辅助黑科技教程);《WPK辅助透视...
透视挂透视(智星德州菠萝有挂吗... 透视挂透视(智星德州菠萝有挂吗)pokermaster有德州吗(详细辅助攻略方法)真是是有挂;辅助插...
wepower有外挂!德扑之星... wepower有外挂!德扑之星app发牌规律,(德州wpk)一向是有挂(详细辅助教你攻略);实战中需...
透视透视(智星德州菠萝有挂吗)... 透视透视(智星德州菠萝有挂吗)epoker外挂(详细辅助2025版教程)本来真的有挂1、用户打开应用...
wepoke辅助机器人!微扑克... wepoke辅助机器人!微扑克有保险吗,(WepokE)固有是真的有挂(详细透视德州论坛) 科技详细...
透视线上(德扑ai智能)微扑克... 透视线上(德扑ai智能)微扑克有没有辅助挂(详细辅助普及教程)本然真的是有挂辅助器中分为三种模型:软...