android从contentobserver发送广播消息

时间:2016-08-11 04:54:19

标签: android broadcastreceiver android-contentresolver

我有一类contentobserver,我想从contentobserver发送broasdcast消息。但是当它的调用应用程序崩溃并且我看到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);
    }
}

1 个答案:

答案 0 :(得分:1)

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