faisal iqbal faisal iqbal - 3 months ago 33
Android Question

android send broadcast message from contentobserver

i have a class of contentobserver i want to send broasdcast message from contentobserver . but when its call app crash and i see the logcate context is null please tell me how can send message from contentresolver.
here is my code

public class SettingsContentObserver extends ContentObserver {

Context context;
public SettingsContentObserver(Handler handler) {
super(handler);
}

@Override
public boolean deliverSelfNotifications() {
return super.deliverSelfNotifications();
}

@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);

//Profile1Activity.profile1(context);

Intent i = new Intent("settingschanged");
context.sendBroadcast(i);
}
}

Answer

Can you try to pass the application context to the ContentObserver?

public class SettingsContentObserver extends ContentObserver {

    Context mContext;
    public SettingsContentObserver(Handler handler, Context context) {
        super(handler);
        this.mContext = context;
    }

    @Override
    public boolean deliverSelfNotifications() {
        return super.deliverSelfNotifications();
    }

    @Override
    public void onChange(boolean selfChange) {
        super.onChange(selfChange);

        //Profile1Activity.profile1(context);

        Intent i = new Intent("settingschanged");
        mContext.sendBroadcast(i);
    }
}