Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
252 views
in Technique[技术] by (71.8m points)

java - How to run background service for Instant Messaging using Kurento API in Oreo?

Android Oreo has released with many restrictions on running background services/Task. Services now don't behave like normal in Oreo as they used to before.

But what if I have to run a service in background for 24*7 for Instant Messaging.

I am developing an application for Instant messaging using kurento Third Party API. To achieve this I will have to run a background service which communicate with server for new messages.

Lower then Oreo its working fine.

How do I prevent android system to not kill the service?.

I don't want to show a notification all time while my service is running because i will run my service for 24*7 for new messages so it feels cheap UI Experience to user.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

After Nougat version the way back ground service is changed. If you want your background service to work, you can do as mentioned in the code below. In IntentService, the life cycle method onCreate() is called. And in this method add below code.

  @Override
public void onCreate() {
    super.onCreate();

    int NOTIFICATION_ID = (int) (System.currentTimeMillis()%10000);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        startForeground(NOTIFICATION_ID, new Notification.Builder(this).build());
    }

}

And when you are calling service, call your service via blow code

  Intent intent = new Intent(context, FindNumberService.class);
    intent.putExtras(bundle);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        context.startForegroundService(intent);
    }
    else {
        context.startService(intent);
    }

Hope this will help you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...