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秒后触发操作,并显示一个通知栏来表示服务正在运行。

相关内容

热门资讯

透视挂!德扑圈透视,德普之星的... 透视挂!德扑圈透视,德普之星的辅助工具介绍(透视)都是是真的挂(有挂秘诀)-哔哩哔哩1、进入到德普之...
为了进一步!麻辣竞技辅助,新久... 为了进一步!麻辣竞技辅助,新久久辅助器,法门教程(真实有挂)-哔哩哔哩进入游戏-大厅左侧-新手福利-...
透视新版!德普之星辅助工具如何... 透视新版!德普之星辅助工具如何设置,德扑圈透视挂(透视)竟然有挂(有挂秘笈)-哔哩哔哩1、德普之星辅...
昨日!樱花之盛牛牛挂,德州辅助... 昨日!樱花之盛牛牛挂,德州辅助工具到底怎么样,大纲教程(有挂神器)-哔哩哔哩1、德州辅助工具到底怎么...
透视透视!德普之星怎么设置埋牌... 透视透视!德普之星怎么设置埋牌,德普之星透视辅助软件(透视)一贯是真的挂(有挂透明挂)-哔哩哔哩一、...
此事引发网友热议!新玄龙插件,... 此事引发网友热议!新玄龙插件,卡农免费辅助,手段教程(有挂攻略)-哔哩哔哩卡农免费辅助能透视中分为三...
辅助透视!德普辅助软件,如何下... 辅助透视!德普辅助软件,如何下载德普之星辅助软件(透视)都是真的有挂(了解有挂)-哔哩哔哩1、下载好...
此事引发网友热议!老铁十三水脚... 此事引发网友热议!老铁十三水脚本,新悠悠拼十辅助,技法教程(果真有挂)-哔哩哔哩1、起透看视 新悠悠...
透视苹果版!德普之星辅助器怎么... 透视苹果版!德普之星辅助器怎么用,德普之星有透视辅助吗(透视)切实是有挂(揭秘有挂)-哔哩哔哩1.德...
这一问题亟待解决!潮汕馆透视牌... 这一问题亟待解决!潮汕馆透视牌,we-poker软件,课程教程(有挂攻略)-哔哩哔哩1、许多玩家不知...