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

相关内容

热门资讯

辅助开挂!陕西挖坑技巧,来玩a... 辅助开挂!陕西挖坑技巧,来玩app 德州 辅助(透视)开挂辅助软件(真实有挂);超受欢迎的陕西挖坑技...
开挂辅助!微乐手游微乐辅助免费... 开挂辅助!微乐手游微乐辅助免费,佛手大菠萝辅助(透视)开挂辅助神器(有挂教学);微乐手游微乐辅助免费...
开挂辅助!广东闲来辅助神器免费... 开挂辅助!广东闲来辅助神器免费版,wepoker有没有透视方法(透视)开挂辅助安装(竟然有挂);广东...
辅助开挂!德友汇辅助器,wep... 辅助开挂!德友汇辅助器,wepoker好友助力码(透视)开挂辅助下载(有挂方针);德友汇辅助器是一款...
辅助开挂!蜀山四川亲友房辅助免... 辅助开挂!蜀山四川亲友房辅助免费,哈糖大菠萝能开挂吗(透视)开挂辅助插件(有挂攻略);AI辅助机器人...
辅助开挂!微乐河南小程序辅助器... 辅助开挂!微乐河南小程序辅助器免费,poker world辅助器(透视)开挂辅助插件(有挂技巧);微...
开挂辅助!微信小游戏辅助器,w... 【福星临门,好运相随】;开挂辅助!微信小游戏辅助器,wepoker底牌透视脚本(透视)开挂辅助下载(...
开挂辅助!微信闲来辅助神器ap... 开挂辅助!微信闲来辅助神器app,pokemmo手机脚本(透视)开挂辅助脚本(有挂方略);微信闲来辅...
辅助开挂!上饶中至打炸漏洞,德... 辅助开挂!上饶中至打炸漏洞,德州透视脚本(透视)开挂辅助下载(有挂攻略);人气非常高,ai更新快且高...
辅助开挂!火神大厅辅助,wep... 辅助开挂!火神大厅辅助,wepoker模拟器哪个(透视)开挂辅助工具(真的有挂);火神大厅辅助最新版...