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

相关内容

热门资讯

详细说明透视器!八闽状元郎透视... 详细说明透视器!八闽状元郎透视器AI攻略,八闽状元郎多功能本来真的有透视器攻略八闽状元郎脚本下载中分...
盘点十款辅助!巴西辅助AI脚本... 盘点十款辅助!巴西辅助AI脚本,巴西多功能真是是真的有辅助脚本1、实时巴西透视辅助更新:用户可以随时...
教程辅助透视挂!友空互娱透视挂... 教程辅助透视挂!友空互娱透视挂AI方法,友空互娱多功能竟然存在有透视挂方法1、上手简单,内置详细流程...
一分钟教会你开挂!七七娱乐开挂... 一分钟教会你开挂!七七娱乐开挂AI插件,七七娱乐多功能竟然存在有开挂插件七七娱乐脚本下载中分为三种模...
发现玩家辅助!闲来玩游戏辅助A... 发现玩家辅助!闲来玩游戏辅助AI攻略,闲来玩游戏多功能真是存在有辅助攻略1、闲来玩游戏公共底牌简单,...
记者发布外挂!闲来玩游戏外挂A... 记者发布外挂!闲来玩游戏外挂AI攻略,闲来玩游戏多功能切实真的是有外挂攻略闲来玩游戏透视方法中分为三...
一分钟揭秘透视挂!萍乡打滚筒透... 一分钟揭秘透视挂!萍乡打滚筒透视挂AI攻略,萍乡打滚筒多功能确实存在有透视挂攻略1、用户打开应用后不...
查到实测辅助器!百川辅助器AI... 查到实测辅助器!百川辅助器AI方法,百川多功能都是有辅助器方法百川辅助器是一种具有地方特色的麻将游戏...
最新技巧辅助器!八戒贵宾室辅助... 最新技巧辅助器!八戒贵宾室辅助器AI软件,八戒贵宾室多功能切实存在有辅助器软件1、下载好八戒贵宾室透...
玩家科普外挂!心悦辽宁麻将外挂... 玩家科普外挂!心悦辽宁麻将外挂AIapp,心悦辽宁麻将多功能果然存在有外挂app1)心悦辽宁麻将辅助...