Search code examples
androidbroadcastreceiverandroid-contentresolver

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 I 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);
    }
}

Solution

  • 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);
        }
    }