How to get Context in an Intent Service

ⅰ亾dé卋堺 提交于 2019-12-04 14:58:18

问题


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


回答1:


So my question is how do I get the context in an IntentService?

The IntentService is the Context, as IntentService inherits from Context.




回答2:


Just call getApplicationContext()




回答3:


You can get the context in onStartCommand() function.

    public class ExampleService extends IntentService {
    private Context mContext;

    public ExampleService(String name) {
    super(name);
    }

    @Override
    protected void onHandleIntent(Intent intent) {

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
    mContext = getApplicationContext();
    return super.onStartCommand(intent, flags, startId);
    }
    }



回答4:


Use BackupSchedulingService.this (ServiceName.this) to get Context

Reason: Service extends ContextWrapper
ContextWrapper extends Context




回答5:


You can use Intent service class as Context. because this is the hierarchy of IntentService class.

  • android.content.Context
    • android.content.ContextWrapper
      • android.app.Service
        • android.app.IntentService

So In java

BackupSchedulingService.this

And in Kotlin

this@BackupSchedulingService

Hope this will help future devs...



来源:https://stackoverflow.com/questions/28754499/how-to-get-context-in-an-intent-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!