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

相关内容

热门资讯

透视有挂!wepoker有机器... 透视有挂!wepoker有机器人吗,拱趴大菠萝万能辅助器,2026新版技巧(一直确实有挂)-哔哩哔哩...
科技分享!wepoker透视是... 科技分享!wepoker透视是真的吗,hhpoker德州机器人,黑科技教程(一贯确实有挂)-哔哩哔哩...
透视透视!wepoker破解是... 透视透视!wepoker破解是真的还是假的,约局吧是否有挂,大神讲解(一直真的是有挂)-哔哩哔哩;1...
透视数据!德州透视脚本,wep... 透视数据!德州透视脚本,wepoker祈福有用吗,新2026教程(其实真的有挂)-哔哩哔哩;相信小伙...
推荐一款!智星菠萝可以辅助吗,... 推荐一款!智星菠萝可以辅助吗,拱趴大菠萝万能挂图解,攻略教程!(一直真的有挂)-哔哩哔哩;是一款可以...
六分钟了解!wepoker是不... 六分钟了解!wepoker是不是有人用挂,HH平台挂,2026新版技巧(一贯真的是有挂)-哔哩哔哩;...
技术分享!wepoker免费透... 技术分享!wepoker免费透视脚本,wepoker底牌透视脚本,详细教程!(果然是有挂的)-哔哩哔...
玩家攻略推荐!wpk官网下载链... 玩家攻略推荐!wpk官网下载链接,佛手在线大菠萝为什么都输,揭秘攻略(一贯真的是有挂)-哔哩哔哩;1...
盘点一款!wpk辅助,wepo... 盘点一款!wpk辅助,wepoker代打辅助机器人,力荐教程(一贯是有挂)-哔哩哔哩;wepoker...
带你了解!hhpoker透视脚... 带你了解!hhpoker透视脚本下载,pokemmo手机版透视脚本,新2026版(其实确实有挂)-哔...