如何保持服务的生命力? [英] How to keep a Service alive?

查看:119
本文介绍了如何保持服务的生命力?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Whatsapp服务如何在华为手机中保持后台运行?

How Whatsapp service keep working in background in huawei phones ?

我删除了受保护应用程序的whatsapp,但屏幕上未关闭Whatsapp服务 休息时间.

I removed whatsapp of protected apps but Whatsapp service not closed in screen off time.

我正在编写需要运行的关键应用程序,但是我的服务在屏幕上消失了.

I'm writing critical app that need to run every time but my service killed in screen off.

我想编写类似Whatsapp或AirDroid服务的服务 任何人都可以解释一下吗?

I want to write service like Whatsapp or AirDroid service anyone can explain about that ?

我的意思是说如何编写华为手机无法通过屏幕关闭特别关闭的服务

I mean how to write service that specially not close by screen off in HUAWEI phones

这是我的服务代码

AppLifeService

AppLifeService

public class AppLifeService extends Service {
@Override
public IBinder onBind(Intent intent) {

    return null;
}



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

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    super.onStartCommand(intent, flags, startId);


    startForeground(5, AppLifeReciever.createNotification(this));


    return  START_STICKY;
}


@Override
public void onDestroy() {


    //startService(new Intent(this, AppLifeService.class)); Updated : not need


    super.onDestroy();

}
}

推荐答案

@Sohail Zahid (答案)告诉您一种repeatedly再次启动服务 的方法.但是为了使服务保持活力,就像在后台播放歌曲一样.

@Sohail Zahid Answer tells you a way to repeatedly start a service again and again when stopped. But in order to keep a service alive like playing a song in a background.

我发现的最佳方法是

startForeground(int, Notification)

每个通知的int值必须唯一

where int value must be unique for every notification

您需要为进行中部分的通知栏中显示的方法提供Notification. 通过这种方式,应用程序将在后台保持活动状态,而不会发生任何干扰.

You'll need to supply a Notification to the method which is displayed in the Notifications Bar in the Ongoing section. In this way the app will keep alive in background without any interuption.

这篇关于如何保持服务的生命力?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆