package edu.hebut.learn_tong_zhi_lan;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import java.util.Timer;
import java.util.TimerTask;
public class newService extends Service {
private final int time_second=60;
private final int NOTIFICATION_ID=63751;
code
@Override
public void onCreate() {
super.onCreate();
System.out.println("new Service onCreate");
}
Timer _timer = new Timer();
int _remain_time=50;
@Override
public void onDestroy() {
System.out.println("new Service onDestroy");
stopForeground(true);
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("new Service onStartCommand------------start");
try {
channel = new NotificationChannel("my_channel", "My Channel", NotificationManager.IMPORTANCE_DEFAULT);
notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}catch (Exception e){
System.out.println("new Service onStartCommand------------error");
System.out.println(e);
}
startForeground(60);
System.out.println("new Service onStartCommand------------end");
startForeground(60);
_timer.schedule(new TimerTask() {
@Override
public void run() {
if(_remain_time<=0)
{
_timer.cancel();
stopForeground(true);
return;
}
Log.e("clock","time:"+_remain_time);
updateTime(_remain_time);
_remain_time-=10;
}
}, 10000, 10000);
return super.onStartCommand(intent, flags, startId);
}
private final IBinder binder = new newService.MyBinder();
public class MyBinder extends Binder {
public newService getService() {
return newService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
return binder;
}
NotificationChannel channel=null;
NotificationManager notificationManager=null;
public void startForeground(int time_second) {
System.out.println("startForeground------------------start------------");
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "my_channel")
.setContentTitle("学刻")
.setContentText("剩余时间:"+(
time_second/10==0?"最后一分钟":(time_second/10+"分钟"))
)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setSmallIcon(R.drawable.ic_launcher_foreground);
Notification notification = builder.build();
try {
startForeground(NOTIFICATION_ID, notification);//random number
} catch (Exception e) {
System.out.println("startForeground(NOTIFICATION_ID, notification); error");
System.out.println(e);
}
System.out.println("startForeground------------------end------------");
}
public void updateTime(int time_second) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "my_channel")
.setContentTitle("学刻")
.setContentText("剩余时间:"+(
time_second/10==0?"最后一分钟":(time_second/10+"分钟"))
)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setSmallIcon(R.drawable.ic_launcher_foreground);
Notification notification = builder.build();
notificationManager.notify(NOTIFICATION_ID, notification);//random number
}
public void stopForeground() {
stopForeground(true);
}
}