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、全新机制【道游辅助脚本ai辅助工具...
据相关数据显示!陕麻圈黑科技!... 据相关数据显示!陕麻圈黑科技!切实是有辅助软件(有挂详细)-哔哩哔哩1、进入游戏-大厅左侧-新手福利...
不少玩家反映!福州十八扑有外g... 不少玩家反映!福州十八扑有外g挂吗!其实有挂辅助修改器(详细教程)-哔哩哔哩1、完成福州十八扑有外g...
记者获悉!福建兄弟13水辅助!... 记者获悉!福建兄弟13水辅助!总是是真的辅助安装(有挂方式)-哔哩哔哩1、完成福建兄弟13水辅助辅助...
截至目前!天天贵阳app破解版... 截至目前!天天贵阳app破解版!都是真的有辅助插件(有挂解惑)-哔哩哔哩1、实时天天贵阳app破解版...
截至发稿!掌中乐游戏中心云南辅... 截至发稿!掌中乐游戏中心云南辅助!一贯存在有辅助app(有挂规律)-哔哩哔哩掌中乐游戏中心云南辅助是...