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

相关内容

热门资讯

7分钟项目!九九山城插件程序,... 7分钟项目!九九山城插件程序,拱趴游戏诀窍,科技教程(有挂分享)-哔哩哔哩1、九九山城插件程序透视辅...
6分钟学习!雀友会广东潮汕辅助... 6分钟学习!雀友会广东潮汕辅助,hhpoker免费辅助器,攻略教程(有挂方式)-哔哩哔哩运雀友会广东...
四分钟绝活儿!新道游正版辅助,... 四分钟绝活儿!新道游正版辅助,卡农辅助,2025版教程(有挂教程)-哔哩哔哩该软件可以轻松地帮助玩家...
2分钟诀窍!微友辅助器免费版v... 2分钟诀窍!微友辅助器免费版v3.0,川友汇挂牌,透牌教程(有挂教学)-哔哩哔哩1、微友辅助器免费版...
3分钟讲义!甘肃白银麻将辅助软... 3分钟讲义!甘肃白银麻将辅助软件,微信边锋辅助软件,靠谱教程(的确有挂)-哔哩哔哩1、玩家可以在甘肃...
三分钟诀窍!福建兄弟十三水辅助... 三分钟诀窍!福建兄弟十三水辅助器下载,欢乐茶馆辅助,揭秘教程(存在有挂)-哔哩哔哩一、福建兄弟十三水...
6分钟资料!摸一把游戏辅助软件... 6分钟资料!摸一把游戏辅助软件,小闲辅助神器,解密教程(真实有挂)-哔哩哔哩1、进入到摸一把游戏辅助...
第9分钟讲义!人海大厅辅助插件... 第9分钟讲义!人海大厅辅助插件,拱趴大菠萝修改器,爆料教程(有挂教学)-哔哩哔哩人海大厅辅助插件辅助...
3分钟教程书!新众乐辅助,情怀... 3分钟教程书!新众乐辅助,情怀娱乐辅助器,德州论坛(有挂存在)-哔哩哔哩1、首先打开情怀娱乐辅助器最...
十分钟资料!白银胡乐白银版辅助... 十分钟资料!白银胡乐白银版辅助,小程序边锋辅助,普及教程(有挂工具)-哔哩哔哩1、白银胡乐白银版辅助...