Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android服务与其他类实例通信_Android_Service - Fatal编程技术网

android服务与其他类实例通信

android服务与其他类实例通信,android,service,Android,Service,首先让我描述一下逻辑 用户打开照片上载按钮启动传输服务,将本地照片上载到服务器 当应用程序对用户不可见时。仍然监控本地照片,如果相机有新照片,请立即上传。我用它来实现这个功能 终止应用程序后,用户可以拍摄新照片,重新启动应用程序时,扫描本地sd卡并上传这些新照片 我的问题是: 如果活动被破坏,我应该在什么时候将步骤2投入使用。我是否应该使用其他服务来实现step3业务 你可以找到 请参见下面的源代码片段: Intent txIntent = new Intent(this, Transf

首先让我描述一下逻辑

用户打开照片上载按钮启动传输服务,将本地照片上载到服务器

当应用程序对用户不可见时。仍然监控本地照片,如果相机有新照片,请立即上传。我用它来实现这个功能

终止应用程序后,用户可以拍摄新照片,重新启动应用程序时,扫描本地sd卡并上传这些新照片

我的问题是:

如果活动被破坏,我应该在什么时候将步骤2投入使用。我是否应该使用其他服务来实现step3业务

你可以找到

请参见下面的源代码片段:

    Intent txIntent = new Intent(this, TransferService.class);
    startService(txIntent);
    Log.d(DEBUG_TAG, "start TransferService");

    // bind transfer service
    Intent bIntent = new Intent(this, TransferService.class);
    bindService(bIntent, mConnection, Context.BIND_AUTO_CREATE);
    Log.d(DEBUG_TAG, "try bind TransferService");

    Intent monitorIntent = new Intent(this, FileMonitorService.class);
    startService(monitorIntent);

    Intent cameraUploadIntent = new Intent(this, CameraUploadService.class);
    startService(cameraUploadIntent);

    this.getApplicationContext().getContentResolver().registerContentObserver(
        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, false, cameraUploadObserver);
    `

那么,我是否应该让cameraUploadObserver投入使用呢?

按照以下步骤实现该要求:

创建一个服务来满足需求的两部分

触发服务的UI按钮

在服务类内部,注册一个内容观察者来检测相机事件,事件文件是onChange方法,所以您可以在onChange方法中上传照片

注册传输服务,绑定连接并使用该连接在onStartCommand方法下上载所有本地照片

查看整个项目