场景如下:我有一个 WakefulBroadcastReceiver,它执行以下操作:
备份到网络计算机或云端。它设置为在
半夜,当我知道平板电脑可以访问
局域网。备份会将数据存储到实例化 WakefulBroadcastReceiver 的片段“选取”的位置和文件,
使用存储访问框架。所以我需要能够访问
ContentResolver 并为此我需要上下文。
从我阅读的所有文档来看,这就是
BroadcastReceiver 旨在用于 - 潜在的长时间运行
当没有太多其他事情时应该在后台完成的任务
正在发生。 - 就像备份一样。我只是还没有看到任何例子
一切都在一起。
如何获取 IntentService 中的上下文?这里
是接收器和调度服务的片段。
public class BackupAlarmReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, BackupSchedulingService.class);
startWakefulService(context, service);
}
}
public class BackupSchedulingService extends IntentService {
public BackupSchedulingService() {
super("BackupSchedulingService");
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
// How to get the context - it was a parameter when
// creating the new IntentService class above?
}
}
示例代码几乎完全遵循 Android 参考
手动代码在这里:
https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver.html https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver.html
所以我的问题是如何获取 IntentService 中的上下文?
The IntentService
is the Context
, as IntentService
继承自Context
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)