108 lines
3.4 KiB
Java
108 lines
3.4 KiB
Java
package com.arpa.hndahesudintocctmsdriver.service;
|
|
|
|
import android.app.Notification;
|
|
import android.app.NotificationChannel;
|
|
import android.app.NotificationManager;
|
|
import android.app.PendingIntent;
|
|
import android.app.Service;
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.os.Build;
|
|
import android.os.Handler;
|
|
import android.os.IBinder;
|
|
import android.util.Log;
|
|
|
|
import androidx.annotation.Nullable;
|
|
import androidx.core.app.NotificationCompat;
|
|
import androidx.core.app.TaskStackBuilder;
|
|
|
|
import com.arpa.hndahesudintocctmsdriver.R;
|
|
import com.arpa.hndahesudintocctmsdriver.ui.login.WelcomeActivity;
|
|
|
|
import java.util.Timer;
|
|
import java.util.TimerTask;
|
|
|
|
|
|
/**
|
|
* @author hlh
|
|
* @version 1.0.0
|
|
* @date 2021/8/10 19:00
|
|
* @description:
|
|
*/
|
|
public class TestService extends Service {
|
|
|
|
private Context con;
|
|
private Handler hd=new Handler(msg -> {
|
|
switch (msg.what){
|
|
case 1:
|
|
|
|
|
|
case 2:
|
|
break;
|
|
}
|
|
return false;
|
|
});
|
|
//DanWeiBroadcastReceiver db;
|
|
@Nullable
|
|
@Override
|
|
//想要使用Service必须实现这个方法,该方法返回一个IBinder对象
|
|
//应用程序使用这个对象与Service组件进行通信
|
|
public IBinder onBind(Intent intent) {
|
|
return null;
|
|
}
|
|
|
|
//Service被创建的时候回调onCreate方法
|
|
public void onCreate(){
|
|
super.onCreate();
|
|
con=this;
|
|
new Timer().schedule(new TimerTask() {
|
|
@Override
|
|
public void run() {
|
|
notification("消息","您有一条新的订单");
|
|
Log.e("1234","1234");
|
|
}
|
|
},100,5000);
|
|
}
|
|
|
|
|
|
//Service被启动的时候回调onStartCommand方法
|
|
public int onStartCommand(Intent intent, int flags, int startId){
|
|
Log.e("---static---","启动");
|
|
return START_STICKY;
|
|
}
|
|
|
|
//Service被销毁的时候回调onDestroy方法
|
|
public void onDestroy(){
|
|
super.onDestroy();
|
|
Log.e("---static---","关闭");
|
|
Intent intentOne = new Intent(con,TestService.class);
|
|
startService(intentOne);
|
|
}
|
|
|
|
private void notification(String title,String value) {
|
|
Intent intent = new Intent(this, WelcomeActivity.class);
|
|
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
|
|
//8.0 以后需要加上channelId 才能正常显示
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
|
|
String channelId = "default";
|
|
String channelName = "默认通知";
|
|
manager.createNotificationChannel(new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH));
|
|
}
|
|
//设置TaskStackBuilder
|
|
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
|
|
stackBuilder.addParentStack(WelcomeActivity.class);
|
|
stackBuilder.addNextIntent(intent);
|
|
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
Notification notification = new NotificationCompat.Builder(this, "default")
|
|
.setSmallIcon(R.mipmap.ic_launcher)
|
|
.setContentTitle("您有一条新的订单")
|
|
.setContentText(value)
|
|
.setAutoCancel(true)
|
|
.setDefaults(Notification.DEFAULT_ALL)
|
|
.setWhen(System.currentTimeMillis())
|
|
.setContentIntent(pendingIntent)
|
|
.build();
|
|
manager.notify(1, notification);
|
|
}
|
|
}
|