android service communicates with other class inst

2020-05-06 11:30发布

first let me describe the logic.

  1. User turn on Photos Upload button to launch a transfer service to upload local photos to server.

  2. When the app was left invisible to user. still monitor the local photos, if camera new photos, upload it immediately. I use this ContentResolver resolution to implement the function.

  3. after terminate the app, user may take new photos, when restart the app, scan the local sd-card and upload those new photo.

Here is my question:

When should I put step2 into Service in case the activity was destroied. should I use another service to implement the step3 business.

you can find the source on Github

see the source snippet below:

    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);
    `

So should I put cameraUploadObserver in service?

1条回答
爷、活的狠高调
2楼-- · 2020-05-06 12:17

follow steps to implement the requirement:

  1. create a service to fulfill the two parts of requirements

  2. UI button to trigger the service

  3. inside service class, register a Content Observer to detect camera event, the event files onChange() method, so you can upload photo in onChange() method

  4. register a Transfer service, bind the connection and use that to upload all local photos under onStartCommand() method.

go to the github site to view the whole project.

查看更多
登录 后发表回答