[英]How to let Activity know that cyclic Service has finished its task once?
I have some problems working with Android Services. 我在使用Android服务时遇到一些问题。 I already have a Service which downloads a file from a server.
我已经有一个可以从服务器下载文件的服务。 (The Service checks cyclic for new data) Aftwerwards it parses the file and adds values to an ArrayList wich will be saved to SharedPreferences.
(服务会循环检查是否有新数据)之后,它将解析该文件并将值添加到ArrayList,并将其保存到SharedPreferences。 In my Activity there are two methods.
在我的活动中,有两种方法。 One will display the values from the ArrayList/SharedPreferences in UI and the second method sets a Notification if needed.
一种将在UI中显示ArrayList / SharedPreferences中的值,第二种方法在需要时设置一个Notification。
But how do I now when my Service completed its task so the two methods can be started? 但是,当我的服务完成其任务以便可以启动这两种方法时,我现在该怎么办?
Register a BroadcastReceiver in your Activity something like: 在您的活动中注册BroadcastReceiver,如下所示:
myReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// do my stuff
}
};
registerReceiver(myReceiver , new IntentFilter("com.myapp.DOWNLOADCOMPLETE"));
Then in your service send the broadcast: 然后在您的服务中发送广播:
Intent i = new Intent("com.myapp.DOWNLOADCOMPLETE");
sendBroadcast(i);
You can also putExtras on your intent if you need to pass some values: 如果您需要传递一些值,也可以将Extras放在意图上:
Documentation BroadcastReceiver 文档广播接收器
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.