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

相关内容

热门资讯

透视肯定!aapoker如何设... 透视肯定!aapoker如何设置胜率(透视)免费透视脚本(总是真的是有挂)1、玩家可以在aapoke...
透视好友!德州局透视,拱趴大菠... 透视好友!德州局透视,拱趴大菠萝挂(竟然有挂)1)拱趴大菠萝挂辅助挂:进一步探索拱趴大菠萝挂辅助透视...
透视科技!aapoker辅助怎... 透视科技!aapoker辅助怎么用(透视)透视脚本(竟然存在有挂)1、该软件可以轻松地帮助玩家将aa...
透视科技!pokemmo脚本辅... 透视科技!pokemmo脚本辅助器下载,德州私人局脚本(本来是真的有挂)1、全新机制【pokemmo...
透视规律!aapoker辅助软... 透视规律!aapoker辅助软件合法吗(透视)免费透视脚本(确实存在有挂)1、aapoker辅助软件...
透视系统!pokeplus脚本... 透视系统!pokeplus脚本,约局吧德州真的有透视挂吗(确实真的是有挂)1、约局吧德州真的有透视挂...
透视脚本!aapoker脚本(... 透视脚本!aapoker脚本(透视)脚本(果然真的是有挂);1、进入游戏-大厅左侧-新手福利-激活码...
透视安装!werplan辅助软... 透视安装!werplan辅助软件,智星德州可以透视吗(总是有挂)1、不需要AI权限,帮助你快速的进行...
透视真的!aapoker透视插... 透视真的!aapoker透视插件(透视)插件下载(都是是真的有挂)1、进入游戏-大厅左侧-新手福利-...
透视脚本!poker红龙辅助,... 透视脚本!poker红龙辅助,wepoker安装教程(总是真的有挂)1、下载好wepoker安装教程...