android - 后台服务未在 oppo 设备中运行
问题描述
我正在尝试运行后台服务。它已经是一项 STICKY 服务,我已经在 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);
}
}
解决方案
在清单文件"stopWithTask"=false
的相应标签中设置属性。<service>
或使用以下代码:
@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);
}
推荐阅读
- typescript - typescript monorepo "@/*" 别名
- reactjs - 如何使用 React TypeScript 创建通用事件处理程序
- google-cloud-platform - 数据流流式自动缩放究竟何时开始发生?
- c# - OmniSharp 智能感知无法识别新的类文件
- javascript - 如何在 JavaScript 中创建 CSS 动画?
- ios - NSManagedObject 出现 NSInvalidArgumentException 错误
- reactjs - 在点击处理程序中获取属性值?
- javascript - 未定义未捕获的参考错误drumKit
- python - 用于处理多列的 Python 宏
- python - 在 MacOS 上下载新的 python 但在询问 python 版本时仍然得到旧版本