Android ProgressDialog不会旋转
-
25-09-2019 - |
题
这是我的拳头Stackoverflow帖子,所以请对我温柔!我敢肯定,我想做的是可能的,这是我(或不做?)导致问题的事情...我不确定那是什么 某物 是。
我想做的是:
在我的应用程序同步和处理下载的数据时,显示一个progressDialog。
问题:
ProgressDialog显示但没有旋转(看起来像是冻结的),同步和处理发生,progressDialog关闭,应用程序正常继续。
我目前如何做到这一点:
创建一个progressDialog-在我的活动中
进行同步 - 在我的服务中
处理数据 - 在我的服务中
丢失进度的人 - 在我的活动中
我尝试的事情:
使用Asyntask(如果需要的话,可以提供代码)使用线程(下面的代码)(可以提供代码)(如果需要的话,可以提供代码)
在花了很多时间来寻找我的问题的答案之后,似乎其他人遇到了相同或相似的问题,并设法使用上面的一个想法来解决它。但是,我无法实施这些想法来解决我的特定问题。这使我确定这是我做错的事情...我只是不确定什么。
我编写的原始代码并将其用作我所有尝试修复的基础:
第一的
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在另一个线程中发生,而除了显示进度数据外,没有其他事情要做...?
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;
}
解决方案
旋转器的问题与您在新线程中创建的ServiceConnection有关。不幸的是,这将在主线程 / GUI线程上运行。
来自API参考:( http://developer.android.com/reference/android/content/content/serviceconnection.html)
像系统中的许多回调一样,此类的方法是从过程的主线程调用的。
因此,由于我不熟悉服务,因此我只能猜测,下载数据完成何时进行交流的一种方式是广播您在活动中聆听的意图,然后停止progressDialog。但是当然还有其他方式。
而且,作为旁注,您绝对不应从其他与主线程的线程中修改GUI,这是您试图通过试图解散对话框的新线程来完成的。您只能从主线程中解散对话框。
其他提示
干草,您可以尝试使用处理程序吗?
我认为您应该使用Android的处理程序功能来执行此操作
public class yourclass extends Activity implements Runnable {
static ProgressDialog progressDialog;
@Override
public void onCreate(Bundle icicle) {
progressDialog = ProgressDialog.show(keywordview.this, "", "Loading...", true);
Thread thread = new Thread(keywordview.this);
}
final Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
progressDialog.dismiss();
/*Here you can handle you process*/
}
};
public void run() {
//here your code for run
handler.sendEmptyMessage(0);
}
}