How to get Context in an Intent Service
android, intentservice
Solution
So my question is how do I get the context in an IntentService?
The `IntentService` is the `Context`, as `IntentService` inherits from `Context`.
Problem
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. 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. 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? } } ``` The example code pretty much follows exactly the Android reference manual code here: https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver.html