android 从 contentobserver 发送广播消息

android send broadcast message from contentobserver

我有 contentobserver 个 class,我想从 contentobserver 发送广播消息。但是当它的呼叫应用程序崩溃并且我看到 logcate 上下文是 null 时,请告诉我如何从 contentresolver 发送消息。 这是我的代码:

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

您可以尝试将应用程序上下文传递给 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);
    }
}