首页 > 解决方案 > 后台服务未在 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);
}

}

标签: android

解决方案


在清单文件"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);
}

推荐阅读