This is my first stackoverflow post, so please be careful with me! I am sure that what I am trying to do is possible, and that is what I did (or did not?) That causes the problem ... I'm just not sure what it is.
What I'm trying to do:
Show ProgressDialog while my application synchronizes and processes the downloaded data.
Problem:
ProgressDialog shows, but does not rotate (which makes it look like it is frozen), synchronization and processing occur, ProgressDialog closes, the application continues as usual.
How I do it now:
Create a ProgressDialog - in my activity
Make a synchronization - in my service
Process data - in my service
Dismis the ProgressDialog - in my activity
What I tried:
Using a stream (code below) Using AsynTask (can provide code if necessary) Using a handler (can provide code if necessary)
Having spent a lot of time searching for an answer to my question, it seems that others had the same or similar problem and managed to solve it using one of the above ideas. However, I could not implement any of these ideas to solve my specific problem. It makes me sure that this is what I'm doing wrong ... I'm just not sure what.
, , :
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);
...
}
...
...
}
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
switch (requestCode){
case ACTIVITY_LOGIN:
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 , - , ProgressDialogue...?
private boolean doBindService() {
mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
mBoundService = ((myService.LocalBinder)service).getService();
while (mBoundService.isRunning()){
}
myMethod();
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;
}
}
, , , .
EDIT:
, . ( ), .
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()){
}
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;
}