我正在尝试运行后台服务。这已经是一项棘手的服务,我已经在StackOverflow上尝试了很多答案。但是,在仅具有版本5.0的oppo设备中,该服务不在后台运行。如果有人对此有解决方案。请帮帮忙
以下是我的服务类的代码
public class MultipleChatBackgroundService extends Service {
private ServiceCallbacks serviceCallbacks;
public static boolean isrunning = false;
private String mychannel;
public interface ServiceCallbacks {
void update(ArrayList<Message_Bean> message, boolean type);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return new MultipleChatBackgroundService.MyBinder();
}
public class MyBinder extends Binder {
MultipleChatBackgroundService getService() {
return MultipleChatBackgroundService.this;
}
}
@Override
public void onCreate() {
super.onCreate();
isrunning = true;
try {
if (!Constants.MYCHANNEL.isEmpty() && Constants.MYCHANNEL != null)
mychannel = Constants.MYCHANNEL;
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
public void recieveMessage(ArrayList<Message_Bean> messageBeans, String channel_id,
String msg) {
FcmListenerService fcmListenerService = new FcmListenerService();
if (serviceCallbacks != null) {
serviceCallbacks.update(messageBeans, true);
//chat notification in foreground
if (!AppController.isActivityVisible()) {
fcmListenerService.sendNotification(this, messageBeans, channel_id, msg);
}
} else {
if (this != null) {
//If activity is visible then update the chat
if (AppController.isActivityVisible()) {
Intent intent = new Intent("activity.chatactivity");
intent.putExtra("update_chat", new Gson().toJson(messageBeans));
sendBroadcast(intent);
} else {
if (Constants.allChannelNames.contains(channel_id)) {
if (Integer.parseInt(messageBeans.get(0).getSender_id()) != PrefsManager.with(this).getObject(
Constants.PREF_USER, PojoDefault.class).getResponse().getUser_id()) {
Log.d("notification", "working");
fcmListenerService.sendNotification(this, messageBeans, channel_id, msg);
}
}
}
}
}
}
@Override
public void onDestroy() {
super.onDestroy();
isrunning = false;
}
public void setCallbacks(MultipleChatBackgroundService.ServiceCallbacks callbacks) {
serviceCallbacks = callbacks;
}
public void clearCallbacks() {
serviceCallbacks = null;
}
@Override
public void onTaskRemoved(Intent rootIntent) {
Intent restartServiceTask = new Intent(getApplicationContext(), this.getClass());
restartServiceTask.setPackage(getPackageName());
PendingIntent restartPendingIntent = PendingIntent.getService(getApplicationContext(), 1, restartServiceTask, PendingIntent.FLAG_ONE_SHOT);
AlarmManager myAlarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
myAlarmService.set(
AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + 1000,
restartPendingIntent);
super.onTaskRemoved(rootIntent);
}}
发布于 2018-12-17 13:54:40
在清单文件对应的<service>标签中设置属性"stopWithTask"=false。
或者使用以下代码:
@Override
public void onTaskRemoved(Intent rootIntent) {
Intent restartService = new Intent(getApplicationContext(),
this.getClass());
restartService.setPackage(getPackageName());
PendingIntent restartServicePI = PendingIntent.getService(
getApplicationContext(), 1, restartService,
PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmService = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() +1000, restartServicePI);
}https://stackoverflow.com/questions/53809580
复制相似问题