无法从FileObserver与Android中的Activity通信

时间:2018-11-22 06:01:01

标签: android fileobserver

Activity捕获到新事件时,我正在尝试从FileObserverFileObserver发送事件。每当在我的FileObserver中创建新文件或文件夹时,我都能获得适当的事件。但是,当我尝试向活动发送更新时。没用这是我的下面的代码。

public class RemoteCommandFolderObserver extends FileObserver {
    private IConfigParams configParams;
    private static final String TAG = "MainActivity";
    private static final int mask = (FileObserver.CREATE |
            FileObserver.DELETE |
            FileObserver.DELETE_SELF |
            FileObserver.MODIFY |
            FileObserver.MOVED_FROM |
            FileObserver.MOVED_TO |
            FileObserver.MOVE_SELF);

    @Inject
    public RemoteCommandFolderObserver(StorageInfoUtil storageInfoUtil, IConfigParams configParams) {
        super(storageInfoUtil.getRemoteCommandPath(), mask);
        File file = new File(storageInfoUtil.getRemoteCommandPath());
        if (!file.exists()) {
            file.mkdirs();
        }
        this.configParams = configParams;
    }

    @Override
    public void onEvent(int event, String path) {
        event &= FileObserver.ALL_EVENTS;
        switch (event) {
            case FileObserver.CREATE:
                String command = "";
                if (path.equalsIgnoreCase("Inservice")) {
                    configParams.setInt(PreferenceConstants.REMOTE_COMMAND, 1);// Move to InService
                    command = "INSERVICE_REMOTE";
                } else if(path.equalsIgnoreCase("Outofservice")){
                    configParams.setInt(PreferenceConstants.REMOTE_COMMAND,0);// Move to Out of Service
                    command = "OUTOFSERVICE_REMOTE";
                }
                AlarmBroadcastObserver.getInstance().updateValue(new Intent().setAction(command));
                Log.d(TAG, "CREATE:" + path);
                break;
            case FileObserver.DELETE:
                Log.d(TAG, "DELETE:" + path);
                break;
            case FileObserver.DELETE_SELF:
                Log.d(TAG, "DELETE_SELF:" + path);
                break;
            default:
                // just ignore
                Log.d(TAG, "DEFAULT: " + path);
                break;
        }
    }
}

AlarmBroadcastObserver:

public class AlarmBroadcastObserver extends Observable {
    private static AlarmBroadcastObserver instance = new AlarmBroadcastObserver();

    public static AlarmBroadcastObserver getInstance(){
        return instance;
    }

    private AlarmBroadcastObserver(){}

    public void updateValue(Object data) {
        synchronized (this) {
            setChanged();
            notifyObservers(data);
        }
    }
}

MainActivity:

public class MainActivity extends BaseActivity implements Observer {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        AlarmBroadcastObserver.getInstance().addObserver(this);
    }

    @Override
    public void update(Observable o, final Object arg) {
        final Intent intent = (Intent) arg;
        switch (intent.getAction()) {
            case "INSERVICE_REMOTE":
                //check current state. It should be OUT_OF_SERVICE due to remote
                Log.e("MainActivity", "INSERVICE");
                break;
            case "OUTOFSERVICE_REMOTE":
                //OUT_OF_SERVICE due to remote
                Log.e("MainActivity", "OUTOFSERVICE");
                break;
        }
    }

    @Override
    protected void onDestroy() {
        AlarmBroadcastObserver.getInstance().deleteObserver(this);
        super.onDestroy();
    }
}

0 个答案:

没有答案