加载数据后隐藏ProgressDialog

时间:2013-11-27 03:49:10

标签: android broadcastreceiver progressdialog intentservice

在我的Android应用程序中,我有一个MainActivity。它有一个EditText和一个Button。用户输入他的推文句柄并按下按钮。启动IntentService,检索用户的推文,然后将第一条推文返回BroadcastReceiver

由于加载推文需要时间,我想在加载推文之前显示加载器。

我在按钮单击侦听器中使用以下代码来显示加载对话框

ProgressDialog progress = new ProgressDialog(MainActivity.this);
progress.setTitle("Loading");
progress.setMessage("Wait while loading...");
progress.show();

但是一旦加载完成,我不知道如何隐藏它。

以下是MainActivityIntentServiceBroadcastReceiver

的代码

MainActivity

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    analyze = (Button)findViewById(R.id.analyze);
    twitter_username = (EditText)findViewById(R.id.twitter_username);
    analyze.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            ProgressDialog progress = new ProgressDialog(MainActivity.this);
            progress.setTitle("Loading");
            progress.setMessage("Wait while loading...");
            progress.show();
            Intent i = new Intent(MainActivity.this , TwitterChecker.class);
            i.putExtra("username", twitter_username.getText().toString());
            startService(i);

        }});


}

IntentService

public class TwitterChecker extends IntentService {
    public TwitterChecker(){
        super("TwitterChecker");
    }

@Override
protected void onHandleIntent(Intent intent) {
    String username = intent.getStringExtra("username");
    TwitterAuthenticator authenticator = TwitterAuthenticator.getInstance();
    String accessToken = null;
    try {
        accessToken = authenticator.authenticate();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (AuthenticationException e) {
        e.printStackTrace();
    }
    Twitter tweets = FetchTweets.fetch(accessToken , username);
    Log.i("Info" , "IntentService started");
    Intent tweet = new Intent("com.kaysush.action.TWEET");
    tweet.putExtra("tweet", tweets.get(0).getText());
    sendBroadcast(tweet); // Once loaded the tweet is sent to the Receiver

}

}

BraodcastReceiver     公共类TweetsReceiver扩展BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    Log.i("TWEET RECEIVED" , intent.getStringExtra("tweet"));
             //Once loading is done a toast is shown
     Toast.makeText(context, intent.getStringExtra("tweet"), Toast.LENGTH_LONG).show();
}

}

2 个答案:

答案 0 :(得分:1)

  

加载数据后隐藏ProgressDialog

您可以这样做,但您需要使用非静态但动态的BroadcastReceiver。所以这是解决方案:

首先,在您的Activity BroadcastReceiver中动态注册

private void registerReceiver() {
   receiver = new BroadcastReceiver() {

      @Override
      public void onReceive(Context context, Intent intent) {
         if (intent.getAction().equals(Const.LOADING_COMPLETE_ACTION)) {
            if (dlg != null) {
               dlg.dismiss();
            }
         }
      }
   }

   IntentFilter intentFilter = new IntentFilter();
   intentFilter.addAction(Const.LOADING_COMPLETE_ACTION);

   registerReceiver(receiver, intentFilter);
}

然后在您的 IntentService 中,您需要的只是发送广播

sendBroadcast(new Intent(Const.LOADING_COMPLETE_ACTION));

注意:还要在Activity范围内定义ProgressDialog变量,以便从 onReceive()方法访问它。

答案 1 :(得分:0)

ProgressDialog progress;  // Make global

分配onClick侦听器

progress = new ProgressDialog(MainActivity.this);

然后在回调方法onReceive()

progress.cancel();