Android的服务与其他类的实例通信 [英] android service communicates with other class instances
问题描述
首先我给大家介绍的逻辑。
first let me describe the logic.
-
用户对照片依次上传按钮启动的接送服务上传本地照片到服务器。
User turn on Photos Upload button to launch a transfer service to upload local photos to server.
当应用程序被留下看不见的用户。继续监测当地的照片,如果相机新品的照片,立刻把它上传。我用这个 ContentResolver的分辨率实施功能。
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.
后终止应用程序,用户可以采取新的照片,当重新启动应用程序,扫描本地SD卡,并上传这些新照片。
after terminate the app, user may take new photos, when restart the app, scan the local sd-card and upload those new photo.
下面是我的问题:
我应该在什么时候把第二步
投入运营的情况下活性destroied。我应该使用其他服务来实现第3步
业务。
When should I put step2
into Service in case the activity was destroied. should I use another service to implement the step3
business.
您可以找到在Github <源/ A>
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);
`
所以,我应该把cameraUploadObserver服务?
So should I put cameraUploadObserver in service?
推荐答案
按照步骤来实现的要求:
follow steps to implement the requirement:
-
创建服务来满足需求两部分。
create a service to fulfill the two parts of requirements
UI按钮来触发该服务
UI button to trigger the service
里面的服务类,登记内容观察员检测相机的事件,该事件文件的onChange()方法,这样你就可以上传照片的onChange()方法
inside service class, register a Content Observer to detect camera event, the event files onChange() method, so you can upload photo in onChange() method
转让登记服务,绑定的连接,并用它来上传onStartCommand下的所有本地照片()方法。
register a Transfer service, bind the connection and use that to upload all local photos under onStartCommand() method.
<一个href="https://github.com/Logan676/seadroid/blob/lg2/src/com/seafile/seadroid2/sync/CameraUploadService.java"相对=nofollow>去GitHub的网站查看整个项目。
这篇关于Android的服务与其他类的实例通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!