为什么我的进度条不能正常增加?

发布于 2024-11-27 12:35:59 字数 2299 浏览 1 评论 0原文

我试图在收到的广播上增加进度条,但从我的角度来看, myProgressDialog.incrementProgressBy(increment); 代码没有任何效果。我尝试将命令放置在不同的地方,但我仍然看不到任何效果。

这是我的代码的样子。

public static final int max = 180;
public final static int increment = (1/180);

  @Override
   public void onCreate(Bundle savedInstanceState) {
            ...
        i = new Intent();
    i.setAction(ITEM_CREATED);
    registerReceiver(myBroadcastReceiver, new IntentFilter(ITEM_CREATED));              
        findFeeds = new Runnable(){
           @Override
           public void run() 
            {
              getFeedObjects();
            }
           };
           beginThread();
        } 

 public static Context getAppContext() {
    return context;
}

private void beginThread()
{
    switch (checkConnectionState(OffsideLiteActivity.this))
    {
        case 0:
        thread =  new Thread(null, findItems, "DoingInBackground");
        thread.start();
        myProgressDialog = new ProgressDialog(this);
            myProgressDialog.setCancelable(true);
            myProgressDialog.setMessage("Loading...");
            myProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            myProgressDialog.setProgress(0);
            myProgressDialog.setMax(max);
            myProgressDialog.show();

        break;
        case 1:
        connectionError();
        break;
        default:
    }
}

     private BroadcastReceiver myBroadcastReceiver =
    new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) 
        {
            myProgressDialog.incrementProgressBy(increment);
            Log.d("RECEIVED", "Broadcast received");
        }

   };

有趣的是,logcat 注册了我在 myProgressDialog.incrementProgressBy(increment); 代码

logcat
之后放置的 Broadcast receive 日志

08-03 13:56:15.109: DEBUG/RECEIVED(8666): Broadcast received
08-03 13:56:15.109: DEBUG/RECEIVED(8666): Broadcast received
08-03 13:56:15.109: DEBUG/RECEIVED(8666): Broadcast received
08-03 13:56:15.109: DEBUG/RECEIVED(8666): Broadcast received
08-03 13:56:15.109: DEBUG/RECEIVED(8666): Broadcast received

就像我说的,我尝试将myProgressDialog.incrementProgressBy(increment); 在我的代码中的其他地方命令,但无济于事。有什么建议吗?

I am trying to increment my progress bar on a broadcast recieved but the myProgressDialog.incrementProgressBy(increment); code has no effect from my point of view. I have tried placing the command in different places but I still can see no effect at all.

Here is what my code looks like.

public static final int max = 180;
public final static int increment = (1/180);

  @Override
   public void onCreate(Bundle savedInstanceState) {
            ...
        i = new Intent();
    i.setAction(ITEM_CREATED);
    registerReceiver(myBroadcastReceiver, new IntentFilter(ITEM_CREATED));              
        findFeeds = new Runnable(){
           @Override
           public void run() 
            {
              getFeedObjects();
            }
           };
           beginThread();
        } 

 public static Context getAppContext() {
    return context;
}

private void beginThread()
{
    switch (checkConnectionState(OffsideLiteActivity.this))
    {
        case 0:
        thread =  new Thread(null, findItems, "DoingInBackground");
        thread.start();
        myProgressDialog = new ProgressDialog(this);
            myProgressDialog.setCancelable(true);
            myProgressDialog.setMessage("Loading...");
            myProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            myProgressDialog.setProgress(0);
            myProgressDialog.setMax(max);
            myProgressDialog.show();

        break;
        case 1:
        connectionError();
        break;
        default:
    }
}

     private BroadcastReceiver myBroadcastReceiver =
    new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) 
        {
            myProgressDialog.incrementProgressBy(increment);
            Log.d("RECEIVED", "Broadcast received");
        }

   };

The interesting part is that logcat registers the Broadcast received log that i placed after the myProgressDialog.incrementProgressBy(increment); code

logcat

08-03 13:56:15.109: DEBUG/RECEIVED(8666): Broadcast received
08-03 13:56:15.109: DEBUG/RECEIVED(8666): Broadcast received
08-03 13:56:15.109: DEBUG/RECEIVED(8666): Broadcast received
08-03 13:56:15.109: DEBUG/RECEIVED(8666): Broadcast received
08-03 13:56:15.109: DEBUG/RECEIVED(8666): Broadcast received

Like I said I have tried putting the myProgressDialog.incrementProgressBy(increment); command elsewhere in my code, but to no avail. Any suggestion?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吃素的狼 2024-12-04 12:35:59

问题可能出在这一行:

public final static int increment = (1/180);

我相信 int 会被截断为 0,因此您只需将进度对话框增加 0。您可能希望增量为 1。

The problem might be this line:

public final static int increment = (1/180);

I believe that int will be truncated to 0, so you're only ever incrementing the progress dialog by 0. You probably want increment to be 1.

请持续率性 2024-12-04 12:35:59

与其使用广播接收器来更新你的 UI,你不能只使用类似观察者可观察模式的东西吗?然后侦听器将在 UI 线程中运行 UI 更新,如下所示:

public void updateUIRequested( int increment )
{
   runOnUIThread( new Runnable() { myProgressDialog.incrementProgressBy(increment); } );
}//met

或者如果你想要更现代的东西,请使用 AsyncTask并仍然在 UI 线程中运行更新。

问候,
史蒂芬

Instead of using a broadcastreceiver to update your UI, couldn't you just use something like an obser-observable pattern, the listener would then run the UI update in the UI thread like this :

public void updateUIRequested( int increment )
{
   runOnUIThread( new Runnable() { myProgressDialog.incrementProgressBy(increment); } );
}//met

or if you want something more modern, use an AsyncTask and still run the update in the UI Thread.

Regards,
Stéphane

女皇必胜 2024-12-04 12:35:59

尝试将调试点放在 myProgressDialog.incrementProgressBy() 处......增量的值是多少?我的猜测是它可能是 0。或者将您的日志更改为:

Log.d("RECEIVED", "Broadcast received, increment by " + increment);

Try putting a debug point right at myProgressDialog.incrementProgressBy().... what is the value of increment? My guess is that it could be 0. Or change your Log to:

Log.d("RECEIVED", "Broadcast received, increment by " + increment);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文