public abstract class WakefulBroadcastReceiver
extends BroadcastReceiver
java.lang.Object | ||
↳ | android.content.BroadcastReceiver | |
↳ | android.support.v4.content.WakefulBroadcastReceiver |
帮助器实现 BroadcastReceiver
的常见模式,该模式接收设备唤醒事件,然后将工作传递给 Service
,同时确保设备在转换期间不会回到睡眠状态。
此课程负责为您创建和管理部分唤醒锁; 您必须请求WAKE_LOCK
许可才能使用它。
WakefulBroadcastReceiver
使用方法startWakefulService()
启动执行工作的服务。 此方法与startService()
相当,只不过WakefulBroadcastReceiver
在服务启动时持有唤醒锁。 与startWakefulService()
传递的意图持有一个额外的识别唤醒锁。
import android.content.Context; import android.content.Intent; import android.os.SystemClock; import android.support.v4.content.WakefulBroadcastReceiver; import android.util.Log; 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); } }
该服务(在本例中为IntentService
)做了一些工作。 完成后,它通过调用completeWakefulIntent(intent)
来释放唤醒锁。 它作为参数传递的意图与WakefulBroadcastReceiver
最初传入的意图是一样的。
import android.app.IntentService; import android.content.Intent; import android.os.SystemClock; import android.util.Log; public class SimpleWakefulService extends IntentService { public SimpleWakefulService() { super("SimpleWakefulService"); } @Override protected void onHandleIntent(Intent intent) { // At this point SimpleWakefulReceiver is still holding a wake lock // for us. We can do whatever we need to here and then tell it that // it can release the wakelock. This sample just does some slow work, // but more complicated implementations could take their own wake // lock here before releasing the receiver's. // // Note that when using this approach you should be aware that if your // service gets killed and restarted while in the middle of such work // (so the Intent gets re-delivered to perform the work again), it will // at that point no longer be holding a wake lock since we are depending // on SimpleWakefulReceiver to that for us. If this is a concern, you can // acquire a separate wake lock here. for (int i=0; i<5; i++) { Log.i("SimpleWakefulReceiver", "Running service " + (i+1) + "/5 @ " + SystemClock.elapsedRealtime()); try { Thread.sleep(5000); } catch (InterruptedException e) { } } Log.i("SimpleWakefulReceiver", "Completed service @ " + SystemClock.elapsedRealtime()); SimpleWakefulReceiver.completeWakefulIntent(intent); } }
Public constructors |
|
---|---|
WakefulBroadcastReceiver() |
Public methods |
|
---|---|
static boolean |
completeWakefulIntent(Intent intent) 从以前的 |
static ComponentName |
startWakefulService(Context context, Intent intent) 做一个 |
Inherited methods |
|
---|---|
From class android.content.BroadcastReceiver
|
|
From class java.lang.Object
|
WakefulBroadcastReceiver ()
boolean completeWakefulIntent (Intent intent)
从前一个startWakefulService(Context, Intent)
完成执行。 现在正在进行的任何唤醒锁都将被释放。
Parameters | |
---|---|
intent |
Intent : The Intent as originally generated by startWakefulService(Context, Intent) . |
Returns | |
---|---|
boolean |
Returns true if the intent is associated with a wake lock that is now released; returns false if there was no wake lock specified for it. |
ComponentName startWakefulService (Context context, Intent intent)
做一个Context.startService
,但在服务启动时持有唤醒锁。 这将修改意图以保持额外的识别唤醒锁; 当服务在Service.onStartCommand
收到它时,它应该将它在Service.onStartCommand
收到的Intent传递回completeWakefulIntent(android.content.Intent)
以释放唤醒锁。
Parameters | |
---|---|
context |
Context : The Context in which it operate. |
intent |
Intent : The Intent with which to start the service, as per Context.startService . |
Returns | |
---|---|
ComponentName |