Files
peko-android/app/src/main/java/com/chwl/app/service/DaemonService.java

118 lines
4.8 KiB
Java

package com.chwl.app.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.IBinder;
import androidx.annotation.Nullable;
import com.chwl.app.R;
import com.chwl.app.reciever.NotificationClickReceiver;
import com.chwl.core.manager.AvRoomDataManager;
import com.chwl.core.room.bean.RoomInfo;
import com.chwl.library.utils.ResUtil;
/**
* @author chenran
* @date 2017/11/16
*/
public class DaemonService extends Service {
private static final String TAG = "DaemonService";
public static final int NOTICE_ID = 100;
private static final String CHANNEL_ID = "IN_ROOM";
private static final String CHANNEL_NAME = ResUtil.getString(R.string.erban_service_daemonservice_01);
private String title;
private boolean isStartForeground = false;
public static void start(Context context, RoomInfo roomInfo) {
Intent intent = new Intent(context, DaemonService.class);
intent.putExtra("title", roomInfo.getTitle());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intent);
} else {
context.startService(intent);
}
}
public static void stop(Context context) {
Intent intent = new Intent(context, DaemonService.class);
context.stopService(intent);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
RoomInfo roomInfo = AvRoomDataManager.get().mCurrentRoomInfo;
if (roomInfo != null) {
title = roomInfo.getTitle();
Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle(title);
builder.setContentText(ResUtil.getString(R.string.erban_service_daemonservice_02));
builder.setTicker(ResUtil.getString(R.string.erban_service_daemonservice_03));
Intent clickIntent = new Intent(this, NotificationClickReceiver.class);
int flag = PendingIntent.FLAG_UPDATE_CURRENT;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
flag = flag | PendingIntent.FLAG_IMMUTABLE;
}
PendingIntent contentIntent = PendingIntent.getBroadcast(
this.getApplicationContext(),
(int) (System.currentTimeMillis() / 100000),
clickIntent,
flag);
builder.setContentIntent(contentIntent);
//8.0系统适配
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
String channelID = getPackageName() + "1";
String channelName = ResUtil.getString(R.string.erban_service_daemonservice_04);
NotificationChannel channel = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_HIGH);
channel.setSound(null, null);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.createNotificationChannel(channel);
builder.setChannelId(channelID);
}
startForeground(NOTICE_ID, builder.build());
isStartForeground = true;
} else {
Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentText(ResUtil.getString(R.string.erban_service_daemonservice_05));
builder.setTicker(ResUtil.getString(R.string.erban_service_daemonservice_06));
//8.0系统适配
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelID = getPackageName() + "1";
String channelName = ResUtil.getString(R.string.erban_service_daemonservice_04);
NotificationChannel channel = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_HIGH);
channel.setSound(null, null);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.createNotificationChannel(channel);
builder.setChannelId(channelID);
}
startForeground(NOTICE_ID, builder.build());
isStartForeground = true;
stopSelf();
}
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
if (isStartForeground) {
stopForeground(true);
}
}
}