繁体   English   中英

android从contentobserver发送广播消息

[英]android send broadcast message from contentobserver

我有一类contentobserver ,我想从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);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM