gpt4 book ai didi

android - 支持库中的 WakefulBroadcastReceiver 与 commonsware 的 cwac-wakeful

转载 作者:太空宇宙 更新时间:2023-11-03 13:20:58 28 4
gpt4 key购买 nike

我正在使用通用软件 WakefulIntentService 进行唤醒工作。与使用 commonsware library 相比有什么优势吗?而不是 WakefulBroadcastReceiver来自支持库?

这是我使用支持库的代码

import android.support.v4.content.WakefulBroadcastReceiver;    

public class SimpleWakefulReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// This is the Intent to deliver to our service.
Intent service = new Intent(context, SimpleWakefulService.class);

// Start the service, keeping the device awake while it is launching.
Log.i("SimpleWakefulReceiver", "Starting service @ " + SystemClock.elapsedRealtime());
startWakefulService(context, service);
}
}

public class SimpleWakefulService extends IntentService {
public SimpleWakefulService() {
super("SimpleWakefulService");
}

@Override
protected void onHandleIntent(Intent intent) {

Log.i("SimpleWakefulReceiver", "Completed service @ " + SystemClock.elapsedRealtime());
SimpleWakefulReceiver.completeWakefulIntent(intent);
}
}

This是文档。

  1. 它们之间有什么区别?
  2. 我应该在哪里使用公共(public)软件库而不是支持图书馆?

最佳答案

它们几乎是一样的。

支持库的 WakefulBroadcastReceiver 采用部分唤醒锁,将锁 ID 作为额外内容放入您应该提供给 IntentService< 的 Intent 完成处理后,您必须在其中调用 completeWakefulIntent ()。所以获取和释放是在不同的地方完成的,这有点代码味道。

CommonsWare 的 WakefulIntentService 自己获取和释放部分唤醒锁。

如果您同意获取和释放应该在同一个地方完成,您可以将常规 BroadcastReceiverWakefulIntentService 结合使用。

如果您不太介意并认为使用知名库更重要,这样新开发人员(或一年后的您)就不必(重新)学习新东西,那么请使用支持库。

更新

此外:在 WakefulBroadcastReceiver 的文档中,它警告可能会被中断并丢失唤醒锁。您需要在 IntentService 中获取自己的唤醒锁以防止出现这种情况。使用 CommonsWare,您可以只依靠它来重新获取锁。

关于android - 支持库中的 WakefulBroadcastReceiver 与 commonsware 的 cwac-wakeful,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27763793/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com