PushService.java 3.77 KB
package com.glsx.pushsdk.service;


import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;

import com.glsx.pushsdk.receiver.GLNetworkConnectChangedReceiver;
import com.blankj.utilcode.util.LogUtils;

import androidx.core.app.NotificationCompat;


@SuppressWarnings("deprecation")
public class PushService extends Service {
    public static final String TAG = "PushService";
    private GLNetworkConnectChangedReceiver mNetworkConnectChangedReceiver;
    private final IBinder mBinder = new LocalBinder();

    public class LocalBinder extends Binder {
        PushService getService() {
            return PushService.this;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        LogUtils.w(TAG, "onStartCommand");
        registerRevice();
        //GLPushManager.getInstance().init(this);
        return super.onStartCommand(intent, flags, startId);
        //return START_STICKY;
    }

    /**
     * 注册网络变更广播接收器
     */
    private void registerRevice() {

        IntentFilter filter = new IntentFilter();
        filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(
                mNetworkConnectChangedReceiver = new GLNetworkConnectChangedReceiver(),
                filter);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        LogUtils.w(TAG, "onCreate");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            setForegroundService();
        }

    }

    @androidx.annotation.RequiresApi(api = Build.VERSION_CODES.O)
    public void setForegroundService() {
        String CHANNEL_ID = "GlPushService";
        String CHANNEL_NAME = "running...";

        int importance = NotificationManager.IMPORTANCE_LOW;
        //构建通知渠道
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, importance);
        channel.setDescription(CHANNEL_ID);
        //在创建的通知渠道上发送通知
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
        //builder.setSmallIcon(R.drawable.ic_logo) //设置通知图标
        builder.setContentTitle(CHANNEL_ID)//设置通知标题
                .setContentText(CHANNEL_NAME)//设置通知内容
                .setAutoCancel(true) //用户触摸时,自动关闭
                .setOngoing(true);//设置处于运行状态
        //向系统注册通知渠道,注册后不能改变重要性以及其他通知行为
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(channel);
        //将服务置于启动状态 NOTIFICATION_ID指的是创建的通知的ID
        startForeground(1, builder.build());
    }


    @Override
    public void onDestroy() {
        LogUtils.w(TAG, "onDestroy");
        stopForeground(true);
        super.onDestroy();
        unregisterReceiver(mNetworkConnectChangedReceiver);

    }

    @Override
    public void onRebind(Intent intent) {
        super.onRebind(intent);
        LogUtils.w(TAG, "onRebind");
    }

    @Override
    public boolean onUnbind(Intent intent) {
        LogUtils.w(TAG, "onUnbind");
        return super.onUnbind(intent);

    }

}