【问题标题】:How to use the service in backgroundtask for downloading data's from server in android?如何在后台任务中使用服务从android中的服务器下载数据?
【发布时间】:2023-03-20 16:08:02
【问题描述】:

我想从服务器下载数据。所以当前活动在屏幕上显示下载进度条。如果我移动到另一个活动并返回下载页面活动页面,我想显示下载光标和下载速率和大小没有中断。我应该在 android 中使用什么类型的服务?谁能给我一些 android 中的逻辑或代码?

【问题讨论】:

    标签: android service background threadpool android-progressbar


    【解决方案1】:

    使用服务开始下载过程。要下载数据,请使用 AsyncTask。

    类似这样的:

    //in activity
    Intent service = new Intent (this, Service.class);
    service.setAction(ACTION);//ACTION is some String constant to determine action
    startService(service);
    
    // in service
    
        public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent.getAction().equals(ACTION )){
    YourAsyncTask task = new YourAsyncTask();
    task .execute();
    }
    
    return Service.START_NOT_STICKY;}
    

    因此,您开始下载,即使您关闭活动,进程也不会终止。 要检查下载过程,您可以使用本地广播接收器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-25
      • 1970-01-01
      • 1970-01-01
      • 2015-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多