如何关闭服务中的进度对话框?

问题描述 投票:0回答:3

我有一个活动,当用户开始下载时会显示进度对话框并从ftp下载开始在服务中

我想在服务完成文件下载后关闭此进度对话框

如何将其停用?

android service dialog
3个回答
0
投票

更好的方法是使用LocalBroadcastManagerActivity通知Service

[Step1:通过您的服务发送本地广播

public class MyService extends Service {

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {

      // do your operation here(create worker thread for blocking operations)

      sendLocalBroadCast() //call this method as soon as above operations completes
      return Service.START_NOT_STICKY;
  }
} 

  private void sendLocalBroadCast() {  

  Intent intent = new Intent("MY_SERVICE_NOTIFICATION");
  LocalBroadcastManager.getInstance(this).sendBroadcast(intent);

}

请注意,系统会在您服务的主线程上调用onStartCommand(Intent intent,int flags,int startId)。一种服务的主线程与UI操作所处的线程相同在同一过程中运行的活动的位置。你应该总是避免使主线程的事件循环停止。长时间运行时操作,网络呼叫或大容量磁盘I / O,您应该开始新线程,或使用AsyncTask

[Step2:让您的Activity收听此广播

    public class MyActivity extends Activity{

         BroadcastReceiver mReceiver = new BroadcastReceiver() {

          @Override
          public void onReceive(Context context, Intent intent) {

           // you can dismiss your progress dialog here. This method will be called when we receive broadcast from service after the service operation is completed

          }
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {

        //register for listening to "MY_SERVICE_NOTIFICATION" event 


          LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver,
              new IntentFilter("MY_SERVICE_NOTIFICATION"));
        }

        @Override
        protected void onDestroy() {
          super.onDestroy();

       // remove the receiver   
        LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver);
        }
    }

0
投票

创建一个具有listen方法的接口finishListener,在活动中实现它以执行您想要的任何操作,然后从那里调用listen方法将其传递给服务构造函数


0
投票

很简单

 alertdialog.dismiss();

只需将其放在安装代码的底部

© www.soinside.com 2019 - 2024. All rights reserved.