funf调度和数据收集

时间:2014-11-05 11:15:23

标签: android scheduled-tasks android-sensors

我是Android编程的新手,也是funf传感器框架的新手。根据以下示例,我将按照指定的时间间隔安排探测。但是创建的sqlite数据库是空的,我不认为数据是完全收集的。任何想法,请。

public class MainActivity extends Activity {
    private static final String TAG = "APP";
    protected static final String PIPELINE_NAME = "default_pipeline";
    private FunfManager manager;
    private Pipeline pipeline;
    private Handler handler;

    private ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.e(TAG,"onServiceDisconnected");
            manager = null;
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.e(TAG,"onServiceConnected");
            manager = ((FunfManager.LocalBinder)service).getManager();
            pipeline = (BasicPipeline) manager.getRegisteredPipeline(PIPELINE_NAME);
            JsonObject jsonobject = (new JsonParser()).parse(getString(R.string.default_pipeline)).getAsJsonObject();
            if (pipeline == null){
                Log.e(TAG,"pipeling is null");
                manager.enablePipeline(PIPELINE_NAME);
                boolean flag = manager.saveAndReload(PIPELINE_NAME, jsonobject);
                Log.e(TAG,String.valueOf(flag));
                if (flag){
                    pipeline = manager.getRegisteredPipeline(PIPELINE_NAME);
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            Log.e(TAG,"will archive now");
                            pipeline.onRun("archive",null);
                        }
                    }, 10000L);
                }

            }
            else {
                Log.e(TAG,"pipeling is NOT null");
            }

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.e(TAG,"onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        handler = new Handler();
        bindService(new Intent(this, FunfManager.class), conn, BIND_AUTO_CREATE);
        waitForServiceConnection(3000);
    }

    @Override
      protected void onDestroy() {
        Log.e(TAG,"onDestroy");
        super.onDestroy();
        unbindService(conn);
      }

    public void waitForServiceConnection(long millisToWait) {
        long time = System.currentTimeMillis();
        while (System.currentTimeMillis() < time + millisToWait) {
            if (manager != null) {
                break;
            } else {
                try {
                    Thread.sleep(100);
                }
                catch (InterruptedException e) {

                }
            }
        }
    }
}

我的strings.xml具有指定的以下值

    <string name="default_pipeline">{
                "name": "example",
                "version":1,
                "archive": {
                        "@schedule": {"interval": 60}
                },
                "upload": {
                        "url": \"http://www.samplewebsite.com/uploadurl\",
                        "@schedule": {"interval": 60}
                },
                "update": {
                        "url": \"http://www.samplewebsite.com/funfconfig\",
                        "@schedule": {"interval": 60}
                },
                "data": [
            {"@type": "edu.mit.media.funf.probe.builtin.LocationProbe",
                         "@schedule": {"interval": 5}
                        }
                ]
        }</string>

提前致谢。

更新:基于注释更新了onServiceConnected,如下所示,但我仍然没有看到创建的数据库文件。我已经完全卸载并重新安装了应用程序重启手机。

  private ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.e(TAG,"onServiceDisconnected");
            manager = null;
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.e(TAG,"onServiceConnected");
            manager = ((FunfManager.LocalBinder)service).getManager();
            pipeline = (BasicPipeline) manager.getRegisteredPipeline(PIPELINE_NAME);
            JsonObject jsonobject = (new JsonParser()).parse(getString(R.string.default_pipeline)).getAsJsonObject();
            if (pipeline == null){
                Log.e(TAG,"pipeline is null");
                pipeline = (BasicPipeline) manager.getRegisteredPipeline(PIPELINE_NAME);
                manager.saveAndReload(PIPELINE_NAME, jsonobject);
                manager.enablePipeline(PIPELINE_NAME);
            }
            else {
                Log.e(TAG,"pipeline is NOT null");
                Log.e(TAG,"Pipeline enabled? "+String.valueOf(pipeline.isEnabled()));
            }
            if (pipeline != null && !pipeline.isEnabled()){
                Log.e(TAG,"pipeline is not null and is NOT enabled");
                manager.enablePipeline(PIPELINE_NAME);
            }

        }
    };

1 个答案:

答案 0 :(得分:1)

您的管道名称不是&#34; default_pipeline&#34;,这只是您的资源名称。它的例子&#34;根据你的配置,所以试试这个:

protected static final String PIPELINE_NAME = "example";

除了你的配置看起来基本上没问题。仍然可以进行一些优化:

private ServiceConnection conn = new ServiceConnection() {
    @Override
    public void onServiceDisconnected(ComponentName name) {
        Log.i(TAG,"onServiceDisconnected");
        manager = null;
    }

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        Log.i(TAG,"onServiceConnected");
        manager = ((FunfManager.LocalBinder)service).getManager();
        pipeline = (BasicPipeline) manager.getRegisteredPipeline(PIPELINE_NAME);
        if (pipeline == null){
            Log.i(TAG,"pipeline is null");
            JsonObject jsonobject = (new JsonParser()).parse(getString(R.string.default_pipeline)).getAsJsonObject();
            manager.saveAndReload(PIPELINE_NAME, jsonobject);
            manager.enablePipeline(PIPELINE_NAME);
            pipeline = (BasicPipeline) manager.getRegisteredPipeline(PIPELINE_NAME);
        }

        if (pipeline != null && !pipeline.isEnabled()){
            Log.e(TAG,"pipeline is not null and is NOT enabled");
            manager.enablePipeline(PIPELINE_NAME);
        }
    }
};

您还可以尝试简化配置以缩小范围:

{
    "name": "example",
    "version":1,
    "archive": {
        "@schedule": {"interval": 60}
    },
    "data": [
        {"@type": "edu.mit.media.funf.probe.builtin.LocationProbe",
         "@schedule": {"interval": 5}
        }
    ]
}