wjc
2025-05-07 b9cc7390e8e8ce64c41c26fb369c98ce669d660c
app/src/main/java/com/hdl/photovoltaic/services/ForeService.java
New file
@@ -0,0 +1,136 @@
package com.hdl.photovoltaic.services;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ServiceInfo;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.os.IBinder;
import android.text.TextUtils;
import android.util.Log;
import static android.app.Notification.VISIBILITY_SECRET;
import androidx.core.app.NotificationCompat;
import com.hdl.photovoltaic.R;
/***
 * @Description: 前台服务
 * channelId必须要一致,否则会报 android.app.RemoteServiceException: Bad notification for startForeground 错误
 * 8.0之上一定要使用 NotificationChannel 适配下才行
 * 步骤
 * 1.通过 “通知服务” 创建 NotificationChannel
 * 2.通过 Notification.Builder 构造器 创建 Notification
 * 3.通过 startForeground 开启服务
 * 4.高于9.0的版本 manifest需要增加  <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
 *
 * 在onCreate中创建一个广播接收器,试试能不能接收到 开单或者预约结束后的通知
 */
public class ForeService extends Service {
    final String TAG = ForeService.class.getName();
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    @SuppressLint("ForegroundServiceType")
    @Override
    public void onCreate() {
        super.onCreate();
//        Log.d(TAG, "ForeService onCreate() 进程Id:" + android.os.Process.myPid());
//        try {
//            // Kotlin 示例
//            if (Build.VERSION.SDK_INT >= 34) {
//                startForeground(1, getNotification(getString(R.string.app_name), "Running"), ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK);
//            } else {
//                startForeground(1, getNotification(getString(R.string.app_name), "Running"));
//            }
//        } catch (Exception e) {
//        }
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        //停止的时候销毁前台服务
        stopForeground(true);
    }
    private Notification getNotification(String title, String message) {
        createNotificationChannel();
        //创建一个通知消息的构造器
        Notification.Builder builder = new Notification.Builder(this);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            //Android8.0开始必须给每个通知分配对应的渠道
            builder = new Notification.Builder(this, "f_channel_id");
        }
        builder.setAutoCancel(true)//设置是否允许自动清除
                .setSmallIcon(R.drawable.logo)//设置状态栏里的小图标
                .setContentTitle(title)//设置通知栏里面的标题文本
                .setContentText(message);//设置通知栏里面的内容文本
        //根据消息构造器创建一个通知对象
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
            return builder.build();
        }
        return null;
    }
    @TargetApi(Build.VERSION_CODES.O)
    private void createNotificationChannel() {
        NotificationChannel channel = new NotificationChannel("f_channel_id", "CHANNEL_NAME", NotificationManager.IMPORTANCE_HIGH);
        //是否绕过请勿打扰模式
        channel.canBypassDnd();
        //闪光灯
        channel.enableLights(true);
        //锁屏显示通知
        channel.setLockscreenVisibility(VISIBILITY_SECRET);
        //闪关灯的灯光颜色
        channel.setLightColor(Color.RED);
        //桌面launcher的消息角标
        channel.canShowBadge();
        //是否允许震动
        channel.enableVibration(false);
        //获取系统通知响铃声音的配置
        channel.getAudioAttributes();
        //获取通知取到组
        channel.getGroup();
        //设置可绕过  请勿打扰模式
        channel.setBypassDnd(true);
        //设置震动模式
        channel.setVibrationPattern(new long[]{100, 100, 200});
        //是否会有灯光
        channel.shouldShowLights();
        getManager().createNotificationChannel(channel);
    }
    private NotificationManager mManager;
    private NotificationManager getManager() {
        if (mManager == null) {
            mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        }
        return mManager;
    }
}