【发布时间】:2014-04-22 14:58:49
【问题描述】:
我在服务中获取数据,并且这些数据会不断更新。 我可以在 Log 中看到这些数据,但我想在活动中看到它以显示它。那么我应该在我的服务中添加什么,以便我的活动会立即收到更新数据的通知?
任务完成后,我可以使用onTaskCompleted 方法在服务中查看和获取数据,这也是我在服务中使用它的方式。
(PS:我可以将数据从活动发送到服务)。
编辑
我添加了一些代码来解决问题,但是“SEND_DATA_INTENT”中有错误:SEND_DATA_INTENT cannot be resolved to a variable
public class DownloadService extends Service implements OnTaskCompleted {.....
@Override
public void onTaskCompleted(String result){
results.add(result);//Ici on ajoute a chaque appel du service le debit
Intent intent = new Intent(SEND_DATA_INTENT);//i have an error here :SEND_DATA_INTENT cannot be resolved to a variable
intent.putExtra("type", "message");
sendBroadcast(intent);
}
//i would like to send data to the activty here ,because i'm sure that the task is end, and i have the data that i need.
}
}
在主 Activity 我添加 DataReciver 类
private class DataReciver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(DownloadService.SEND_DATA_INTENT)) //i have an error here :SEND_DATA_INTENT cannot be resolved to a variable
{
Bundle bdl = intent.getExtras();
String type = bdl.getString("type");
}
}
}
【问题讨论】:
-
有几种方法可以做到这一点。您可以使用 Binder 或 BroadcastMessages。为了更好地实现发布您如何将数据从活动发送到服务。
-
感谢您的回复,但正如我所说,我可以将数据从活动发送到服务,但我想要的是将数据从服务发送到活动@Demand
-
@Demand 你能把 loc 带到我的编辑处吗。谢谢。
标签: android service android-activity broadcastreceiver android-broadcast