Joh*_*ohn 5 java android ui-thread progressdialog serviceconnection
这是我的拳头stackoverflow帖子所以请对我温柔!我确信我正在尝试做的事情是可能的,这是我做过的事情(或者没做过的事情?)导致问题......我只是不确定那是什么东西.
我正在尝试做的事:
在我的应用程序同步并处理下载的数据时显示ProgressDialog.
问题:
ProgressDialog显示但不旋转(这使它看起来像已冻结),同步和处理发生,ProgressDialog关闭,应用程序继续正常.
我目前如何做到这一点:
创建ProgressDialog - 在我的活动中
执行同步 - 在我的服务
处理数据 - 在我的服务
Dismis中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*/
...
}
Run Code Online (Sandbox Code Playgroud)
然后
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;
}
}
Run Code Online (Sandbox Code Playgroud)
所以现在我假设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;
}
}
Run Code Online (Sandbox Code Playgroud)
如果您需要任何进一步的信息,请告知我们以帮助我解决此问题.
编辑:
这是我用来代替处理程序的代码.这与以前一样显示相同的行为(微调器不旋转).
Thread doBind = new Thread(new Runnable(){
public void run(){
doBindService();
}
});
doBind.start();
Run Code Online (Sandbox Code Playgroud)
.
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
dialog.dismiss();
}
};
Run Code Online (Sandbox Code Playgroud)
.
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;
}
Run Code Online (Sandbox Code Playgroud)
微调器的问题与您在新线程中创建的 ServiceConnection 有关。不幸的是,这将在您的主线程/gui 线程上运行。
来自 API 参考:(http://developer.android.com/reference/android/content/ServiceConnection.html)
与系统中的许多回调一样,此类上的方法是从进程的主线程调用的。
因此,由于我不熟悉服务,我只能猜测当您的服务完成下载数据时进行通信的一种方式是广播您在 Activity 中侦听的 Intent,然后停止 ProgressDialog。但当然还有其他方法。
顺便说一句,你不应该从主线程以外的任何线程修改 gui,你试图通过生成一个试图关闭对话框的新线程来实现这一点。您应该只从主线程中关闭该对话框。