I am using asy task for doing loading proccess while the service is supposed to take care about the connection.
I am getting some error on the line:
serviceIntent.putExtra("receiver",new DownloadReceiver(new Handler()));
when I am using AsyncTask and service to do the connection.
my code is for AsyncTask is:
private class DownloadReceiver extends ResultReceiver{
public DownloadReceiver(Handler handler) {
super(handler);
}
@Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
super.onReceiveResult(resultCode, resultData);
// if (resultCode == DownloadService.UPDATE_PROGRESS) {
int progress = resultData.getInt("progress");
// mProgressDialog.setProgress(progress);
// if (progress == 100) {
// mProgressDialog.dismiss();
}
}
protected String doInBackground(String... params) {
Intent serviceIntent = new Intent();
serviceIntent.putExtra("url", "www.google.com");
new DownloadReceiver(new Handler());
serviceIntent.putExtra("receiver",new DownloadReceiver(new Handler()));
serviceIntent.setAction("connection.DownloadService");
context.startService(serviceIntent);
so what can be the problem?
I think you are getting an error since you are creating the Handler object inside the
doInBackground()method which executed in another thread than the UI one, then some where in your application you are accessing theUI Threadusing thatHandlerobject. Note : To do that correctly you SHOULD create the
HandlerinUI Thread. what a good place to do that for your case .or put it as a instance variable . and pass it in the constructor;
}