【发布时间】:2010-11-17 12:18:27
【问题描述】:
这是我的第一个 stackoverflow 帖子,所以请对我温柔一点!我确定我正在尝试做的事情是可能的,并且是我已经做过(或没有做过?)的事情导致了问题......我只是不确定那是什么something 是。
我想要做什么:
在我的应用同步和处理下载的数据时显示 ProgressDialog。
问题:
ProgressDialog 显示但不旋转(这使它看起来像已冻结),同步和处理发生,ProgressDialog 关闭,应用程序继续正常运行。
我目前是如何做到这一点的:
创建一个 ProgressDialog - 在我的活动中
进行同步 - 在我的服务中
处理数据 - 在我的服务中
关闭 ProgressDialog - 在我的活动中
我尝试过的事情:
使用线程(下面的代码)
使用 AsynTask(如果需要可以提供代码)
使用Handler(如果需要可以提供代码)
在花费大量时间寻找我的问题的答案之后,似乎其他人也遇到了相同或相似的问题,并设法使用上述想法之一解决了它。但是,我无法实施任何这些想法来解决我的特定问题。这让我确定这是我做错了什么......我只是不确定是什么。
我编写并用作所有尝试修复的基础的原始代码:
首先
public class myActivity extends ListActivity {
private ProgressDialog dialog;
private boolean mIsBound;
private myService mBoundService;
private ServiceConnection mConnection;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*if we need to login start login activity for result*/
...
}
...
/*other methods*/
...
}
然后
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
switch (requestCode){
case ACTIVITY_LOGIN:
/*after login we know we always need to sync*/
dialog = new ProgressDialog(myActivity.this);
dialog.setMessage("Synchronising...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
Thread doBind = new Thread(new Runnable(){public void run(){doBindService();}});
doBind.start();
break;
}
}
所以现在我假设 doBind 发生在不同的线程中,使 UI 线程除了显示 ProgressDialogue 之外无事可做...?
private boolean doBindService() {
mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
mBoundService = ((myService.LocalBinder)service).getService();
while (mBoundService.isRunning()){
/*wait for service to finish before doing anything else... myMethod() is expecting the sync to have completed*/
/*I suspect this loop to be the thing causing the problem. But I'm not sure why because it is in a different thread so shouldn't interfear with the dialog?! And if that is what is causing the problem then how else can I do this?*/
}
/*get the activity to do Stuff with the downloaded data*/
myMethod();
/*finished with the service for now so unbind*/
doUnbindService();
if (dialog != null) {
dialog.dismiss();
}
}
public void onServiceDisconnected(ComponentName className) {
mBoundService = null;
}
};
boolean myReturn = bindService(new Intent(myActivity.this, myService.class), mConnection, Context.BIND_AUTO_CREATE);
mIsBound = myReturn;
return myReturn;
}
private void doUnbindService() {
if (mIsBound) {
unbindService(mConnection);
mIsBound = false;
}
}
如果您需要任何进一步的信息来帮助我解决这个问题,请告诉我。
编辑:
这是我用来代替处理程序的代码。这显示与以前相同的行为(微调器不旋转)。
Thread doBind = new Thread(new Runnable(){
public void run(){
doBindService();
}
});
doBind.start();
.
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
dialog.dismiss();
}
};
.
private boolean doBindService() {
mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
mBoundService = ((myService.LocalBinder)service).getService();
while (mBoundService.isRunning()){
}
//...do stuff same as before...
handler.sendEmptyMessage(0);
}
public void onServiceDisconnected(ComponentName className) {
mBoundService = null;
}
};
boolean myReturn = bindService(new Intent(myActivity.this, myService.class), mConnection, Context.BIND_AUTO_CREATE);
mIsBound = myReturn;
return myReturn;
}
【问题讨论】:
-
我在进度对话框中看到了同样的冻结行为。通常是在任务几乎完成并且对话框即将关闭时。你的对话持续了多长时间?如果你让线程休眠几秒钟,那会让对话框旋转吗?
-
我在这里询问过的 ProgressDialog 有类似的问题:stackoverflow.com/questions/3821306/…。我认为 Android 代码库中存在错误。
-
@softarn:感谢您的建议。如果我这样做: Thread doBind = new Thread(new Runnable(){ public void run(){ try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } doBindService(); } } );对话框旋转 10 秒,然后冻结 3 到 4 秒,然后消失...
-
就像ageektrapped 说的那样,这似乎是一个错误。在我阅读问题之前,我并没有真正注意到这一点。现在真的很困扰我:) 问题:code.google.com/p/android/issues/detail?id=4266
-
第一个问题做得好的+1
标签: java android progressdialog serviceconnection ui-thread