如何在意图服务中获取上下文 [英] How to get Context in an Intent Service
问题描述
在这种情况下:我有一个WakefulBroadcastReceiver,它可以 备份到网络计算机或云.它准备在 午夜时分,当我知道平板电脑将可以访问 局域网.备份会将数据存储到实例化WakefulBroadcastReceiver的片段选择"的位置和文件中, 使用存储访问框架.所以我需要能够访问 ContentResolver,为此,我需要上下文.
Here's the scenario: I have a WakefulBroadcastReceiver that does a backup to a network computer or the cloud. It's set to go off in the middle of the night, when I know the tablet will have access to the LAN. The backup will store the data to a location and a file that was "picked" by the fragment that instantiated the WakefulBroadcastReceiver, using the Storage Access Framework. So I need to be able to access the ContentResolver and to do that I need the context.
从我对文档的所有阅读中,这就是 BroadcastReceiver旨在用于-可能长时间运行 当没有太多其他任务时应该在后台完成的任务 发生. -就像备份一样.我只是没有看过任何例子 一切都在一起.
From all my reading of the documents, this is what the BroadcastReceiver is meant to be used for - a potentially long running task that should be done in the background when not much else is happening. - Like a backup. I just haven't seen any examples that puts everything together.
如何在IntentService中获取上下文?这里 是接收器和调度服务的代码段.
How do I get the context in an IntentService? Here is a snippet of the receiver and the scheduling service.
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参考 此处的手动代码:
The example code pretty much follows exactly the Android reference manual code here:
https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver.html
推荐答案
所以我的问题是如何在IntentService中获取上下文?
So my question is how do I get the context in an IntentService?
IntentService
是Context
的,因为IntentService
继承自Context
.
这篇关于如何在意图服务中获取上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!