5秒定时服务
This commit is contained in:
@@ -10,11 +10,22 @@ import android.app.Notification;
|
||||
import android.app.NotificationChannel;
|
||||
import android.app.NotificationManager;
|
||||
import android.content.pm.ServiceInfo;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class Main extends Service {
|
||||
private Handler handler = new Handler(Looper.getMainLooper());
|
||||
private Runnable logRunnable = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Log.i("PineappleService", "服务正在运行...");
|
||||
// 5秒后再次执行
|
||||
handler.postDelayed(this, 5000);
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
@@ -46,6 +57,9 @@ public class Main extends Service {
|
||||
startForeground(1, notification);
|
||||
}
|
||||
|
||||
// 启动定时日志任务
|
||||
handler.post(logRunnable);
|
||||
|
||||
return START_STICKY;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user