如何让服务保持活力? [英] How to keep a Service alive?

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

问题描述

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 答案告诉您一种在停止时重复启动服务一次又一次的方法.但是为了让服务保持活力,就像在后台播放歌曲一样.

@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,该方法显示在 Ongoing 部分的 Notifications Bar 中.通过这种方式,应用程序将在后台保持活动状态,不会出现任何中断.

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天全站免登陆