需要关于如何永远在后台,即使设备睡眠,如WhatsApp的运行Android的服务,code的例子吗? [英] Need code example on how to run an Android service forever in the background even when device sleeping, like Whatsapp?

查看:546
本文介绍了需要关于如何永远在后台,即使设备睡眠,如WhatsApp的运行Android的服务,code的例子吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了各种方法来实现这一点,但我的服务最终就会被杀死。

我想用AlarmManager触发类每隔一小时。即使设备处于睡眠状态,应该发送的闪烁LED警报,振动或声音。在任何情况下,它应该运行下去。

我已经注意到了WhatsApp一直在运行,即使我杀了所有正在运行的应用程序,并清除内存,使设备睡了,还是WhatsApp的接收信息和提醒我。他们是如何做的呢?我想用我的应用程序做同样的。

解决方案

由于我张贴了这个问题,我已经实现了两个不同的方法来此解决方案到多个应用程序。


  

方法1

该提取物是从一个应用程序,我使用推送通知,需要立即叫醒了该设备。在这里,我要做的就是1)使用WAKE_LOCK权限和2)使用Wakelocker抽象类3)根据需要使用它的活动:

清单:

 <使用-权限的Andr​​oid:名称=android.permission.WAKE_LOCK/>
 

WakeLocker类:​​

 公共抽象类WakeLocker {
私有静态PowerManager.WakeLock wakeLock;

公共静态无效的获取(上下文的背景下){
    如果(wakeLock!= NULL)wakeLock.release();

    电源管理器PM =(电源管理器)context.getSystemService(Context.POWER_SERVICE);
    wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |
            PowerManager.ACQUIRE_CAUSES_WAKEUP |
            PowerManager.ON_AFTER_RELEASE,WakeLock);
    wakeLock.acquire();
}

公共静态无效发行(){
    如果(wakeLock!= NULL)wakeLock.release(); wakeLock = NULL;
}
}
 

活动类的例子:

 私人最终BroadcastReceiver的接收器=新的BroadcastReceiver(){
    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){
        //醒来移动,如果它是睡
        WakeLocker.acquire(getApplicationContext());
        // 做一点事
        WakeLocker.release();
}
 


  

方法2

最好的,当你想给Android的控制权醒来,并且可以忍受周期性地唤醒你的code。只需使用一个AlarmManager定期调用一个服务类。下面是我的LifeLog24应用程序的一些code:

MainActivity

 意图ll24 =新的意图(背景下,AlarmReceiverLifeLog.class);
    PendingIntent recurringLl24 = PendingIntent.getBroadcast(上下文,0,ll24,PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager报警=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
    alarms.setRepeating(AlarmManager.RTC_WAKEUP,first_log.getTime(),AlarmManager.INTERVAL_HOUR,recurringLl24); //登录重复
 

报警类别

 公共类AlarmReceiverLifeLog扩展的BroadcastReceiver {

    私有静态最后字符串变量=LL24;
    静态上下文的背景下;

    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){

        Log.v(TAG,报警器的LIFELOG ......);

        意图ll24Service =新的意图(背景下,LifeLogService.class);
        context.startService(ll24Service);
    }
    }
 

和LifeLogService.class是我做我的东西。每隔一小时报警醒来,在这种情况下,触发这反过来运行服务的BroadcastReceiver。还有更多的它,以确保服务没有运行两次等等,但你明白了一点它是如何做。而AlarmManager实际上做到这一点,因为你不用担心电池的使用等,而Android需要唤醒你的服务定期的护理的最好方法。

I have tried various ways to achieve this, but my service eventually gets killed.

I want to use AlarmManager to trigger a class every one hour. Even if the device is sleeping, it should sent a flashing LED alert, vibration or sound. In any case, it should run forever.

I have noticed that Whatsapp is always running, even though I kill all the running apps and clear the memory, put the device to sleep, and still Whatsapp receive messages and alerts me. How are they doing it? I want to do the same with my app.

解决方案

Since I posted this question, I have implemented two different approaches to this solution into multiple apps.


APPROACH 1

This extract is from an app where I use push notifications, which need instant wake up calls for the device. Here what I do is 1) use WAKE_LOCK permission and 2) use a Wakelocker abstract class 3) use it in an Activity as needed:

Manifest:

<uses-permission android:name="android.permission.WAKE_LOCK" />

WakeLocker class:

public abstract class WakeLocker {
private static PowerManager.WakeLock wakeLock;

public static void acquire(Context context) {
    if (wakeLock != null) wakeLock.release();

    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |
            PowerManager.ACQUIRE_CAUSES_WAKEUP |
            PowerManager.ON_AFTER_RELEASE, "WakeLock");
    wakeLock.acquire();
}

public static void release() {
    if (wakeLock != null) wakeLock.release(); wakeLock = null;
}
}

Activity class example:

private final BroadcastReceiver receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        // Waking up mobile if it is sleeping
        WakeLocker.acquire(getApplicationContext());
        // do something
        WakeLocker.release();
}


APPROACH 2

Best when you want to give Android control over wake up, and can live with periodically waking up your code. Simply use an AlarmManager to invoke a Service class at regular intervals. Here is some code from my LifeLog24 app:

MainActivity

Intent ll24 = new Intent(context, AlarmReceiverLifeLog.class);
    PendingIntent recurringLl24 = PendingIntent.getBroadcast(context, 0, ll24, PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager alarms = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarms.setRepeating(AlarmManager.RTC_WAKEUP, first_log.getTime(), AlarmManager.INTERVAL_HOUR, recurringLl24); // Log repetition

Alarm Class

public class AlarmReceiverLifeLog extends BroadcastReceiver {

    private static final String TAG = "LL24";
    static Context context;

    @Override
    public void onReceive(Context context, Intent intent) {

        Log.v(TAG, "Alarm for LifeLog...");

        Intent ll24Service = new Intent(context, LifeLogService.class);
        context.startService(ll24Service);
    }
    }

and LifeLogService.class is where I do my stuff. Alarm wakes up every hour in this case and triggers the BroadcastReceiver which in return runs the service. There is more to it, to make sure service is not run twice and so on, but you get the point how it is done. And AlarmManager is actually the best way to do it since you don't worry about battery usage, etc. and Android takes care of waking up your Service at regular intervals.

这篇关于需要关于如何永远在后台,即使设备睡眠,如WhatsApp的运行Android的服务,code的例子吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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