shyam
shyam

Reputation: 1306

Run asynchronous task in OnReceive() method of broadcast receiver

I want to run async task in onReceive(Context context, Intent intent) method. now i want to use context of OnReceive....because i have to use : Cursor cursor = context.getContentResolver().query(); for getting the cursor so its throwing null pointer exception if I use context defined globally..so how to take context of Onreceive() method for running it asynch task inside it.

Upvotes: 0

Views: 316

Answers (1)

Lalit Poptani
Lalit Poptani

Reputation: 67286

Simply create a global Context in the BroadCastReceiver and assign the Context instance inside the onReceive() to this global Context instance and use it,

Context context;

@Override
    public void onReceive(Context arg0, Intent arg1) {
        this.context = arg0;
            // now use context instance in your AsyncTask class.
        }

Upvotes: 1

Related Questions