我正在尝试在收到的广播上增加我的进度条,但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注册了Broadcast received
代码之后放置的myProgressDialog.incrementProgressBy(increment);
日志
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
就像我说的,我已经尝试将myProgressDialog.incrementProgressBy(increment);
命令放在我的代码中的其他地方,但无济于事。有什么建议吗?
答案 0 :(得分:6)
问题可能就在这一行:
public final static int increment = (1/180);
我相信int会被截断为0,所以你只需要将进度对话框递增0.你可能希望增量为1。
答案 1 :(得分:1)
不是使用广播接收器来更新你的UI,你不能只使用类似于观察可观察模式的东西,然后监听器会在UI线程中运行UI更新,如下所示:
public void updateUIRequested( int increment )
{
runOnUIThread( new Runnable() { myProgressDialog.incrementProgressBy(increment); } );
}//met
或者如果你想要更现代的东西,使用AsyncTask并仍然在UI线程中运行更新。
此致 斯特凡
答案 2 :(得分:0)
尝试将调试点放在myProgressDialog.incrementProgressBy()....增量的值是多少?我的猜测是它可能是0.或者将你的日志改为:
Log.d("RECEIVED", "Broadcast received, increment by " + increment);