首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >后台服务未在oppo设备上运行

后台服务未在oppo设备上运行
EN

Stack Overflow用户
提问于 2018-12-17 13:47:46
回答 1查看 628关注 0票数 0

我正在尝试运行后台服务。这已经是一项棘手的服务,我已经在StackOverflow上尝试了很多答案。但是,在仅具有版本5.0的oppo设备中,该服务不在后台运行。如果有人对此有解决方案。请帮帮忙

以下是我的服务类的代码

代码语言:javascript
复制
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);
}

}

EN

回答 1

Stack Overflow用户

发布于 2018-12-17 13:54:40

在清单文件对应的<service>标签中设置属性"stopWithTask"=false

或者使用以下代码:

代码语言:javascript
复制
@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);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53809580

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档