Thursday, June 28, 2012

How to update Activity from Service using ResultReceiver

Today I am going to show how can we update Activity using android.os.ResultReceiver. What we need is just create and inner class inside an Activity that extends ResultReceiver and override its onReceiveResult() methods that will be called while sending data from Service class and inside this method we can update UI components.

What I will show in Demo?
I will just create an Activity with a TextView and update the TextView with current seconds of time.

So, create an Activity with main.xml having a TextView. Also, an inner class that extends ResultReceiver and  a class that extends Runnable to be used for runOnUiThread.

public class ResultReceiverDemoActivity extends Activity{

Intent intent;
TextView txtview;
MyResultReceiver resultReceiver;

public void onCreate(Bundle savedInstanceState) {

resultReceiver = new MyResultReceiver(null);
txtview = (TextView) findViewById(;
intent = new Intent(this, MyService.class);
intent.putExtra("receiver", resultReceiver);

protected void onDestroy() {

class UpdateUI implements Runnable
String updateString;

public UpdateUI(String updateString) {
this.updateString = updateString;
public void run() {

class MyResultReceiver extends ResultReceiver
public MyResultReceiver(Handler handler) {

protected void onReceiveResult(int resultCode, Bundle resultData) {

if(resultCode == 100){
runOnUiThread(new UpdateUI(resultData.getString("start")));
else if(resultCode == 200){
runOnUiThread(new UpdateUI(resultData.getString("end")));
runOnUiThread(new UpdateUI("Result Received "+resultCode));

As, you can see there is nothing much in the above code just a simple one. The new thing about you would be the class that extends ResultReceiver. It is having an overrided method onReceiveResult(int resultCode, Bundle resultData) with parameters resultCode and Bundle. These two parameters we will pass from the Service class using send(resultCode, Bundle resultData) of ResultReceiver which will be pass to onReceiveResult(int resultCode, Bundle resultData) where we can update the UI.

Also, one more important thing is that you might have seen that I am passing a putExtra to Service class as
intent.putExtra("receiver", resultReceiver); where resultReceiver is the instance of MyResultReceiver class that extends ResultReceiver. We will get the putExtra in the Service class and use the same instance to send data from Service to Activity using send(resultCode, Bundle resultData).

Now, lets add the Service class that is also a simple one having a Timer with 1 second.

public class MyService extends Service{

Timer timer = new Timer();
MyTimerTask timerTask;
ResultReceiver resultReceiver;

public int onStartCommand(Intent intent, int flags, int startId) {

resultReceiver = intent.getParcelableExtra("receiver");

timerTask = new MyTimerTask();
timer.scheduleAtFixedRate(timerTask, 1000, 1000);
public IBinder onBind(Intent intent) {
return null;
public void onDestroy() {
Bundle bundle = new Bundle();
bundle.putString("end", "Timer Stopped....");
resultReceiver.send(200, bundle);

class MyTimerTask extends TimerTask
public MyTimerTask() {
Bundle bundle = new Bundle();
bundle.putString("start", "Timer Started....");
resultReceiver.send(100, bundle);
public void run() {
SimpleDateFormat dateFormat = new SimpleDateFormat("s");
resultReceiver.send(Integer.parseInt(dateFormat.format(System.currentTimeMillis())), null);

So, simply we are getting the putExtra of ResultReceiver's instance using resultReceiver = intent.getParcelableExtra("receiver"); inside onStartCommand() to use it further for sending the data to Activity. You can send any data to Activity using send(resultCode, Bundle resultData) method of ResultReceive, you can send an error message also with the same method checking the resultCode.
Source code can be found here.

Calling SOAP WebService using HTTPPOST and getting response in XML.

Today I am going to show you how we can call soap web-service using HTTP-POST. As we know for calling SOAP web-service you require ksoap jar to be added to your project and you can call it. But, sometimes when you have complex response from SOAP web-services its very difficult to parse.So, my motive in this post is to call the SOAP web-service and get the response in XML rather than getting response in SOAP. Though you can get the request by using "requestDump" & response in XML by using "responseDump" as

HttpTransportSE androidHttpTransport = new HttpTransportSE(URL, timeout);
androidHttpTransport.debug=true;, envelope);
String requestString = androidHttpTransport.requestDump;
Log.d("Request in XML", requestString);
String response = androidHttpTransport.responseDump;
Log.d("Response in XML", response);

So, what you can do is create a file with an Request and keep it inside assets folder of your project. Also you have to add %s in place of paramters if you want to pass dynamic parameters to your web-service. I am going to use a live web-service from so that you can test the source code directly if you don't have your own web-service.

I am going to use a web-service that will convert Celcius to Fahrenheit. Below is my request.xml in assets folder.

<?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="" xmlns:xsd="" xmlns:soap="">
<CelsiusToFahrenheit xmlns="">

You can see I am using %s in place of Celcius value because I am going to pass the parameter dynamically from the code not a static one. Now we have to read the xml from assets folder and pass parameter to it dynamically. Below is how you can read the xml from assets folder and convert it to String.

public static String convertStreamToString(InputStream is) throws Exception {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
    return sb.toString();

You can just pass InputStream to this method and get the xml as response from this method. Just call it by

String xml = convertStreamToString(getAssets().open("request.xml"));

Now, you have the xml String you need to pass the parameter that is the value of Celcius, so you can do that by using String.format(String format, Object...args)
In this case you can do it as

String RequestString = String.format(xml, "12"); (If you have multiple paramters you can pass it by comma seperated)

Now you have your Request String ready to be passed to HTTPPost and get the Response. So, just pass the URL and RequestString to get the Response.
I had just created a simple method that will except the arguments as URL of our web-service and a String request that is the xml from assets folder.

public String getResponseByXML(String URL, String request) {
    HttpPost httpPost = new HttpPost(URL);
    StringEntity entity;
    String response_string = null;
    try {
        entity = new StringEntity(request, HTTP.UTF_8);
        HttpClient client = new DefaultHttpClient();
        HttpResponse response = client.execute(httpPost);
        response_string = EntityUtils.toString(response.getEntity());
        Log.d("request", response_string);
        } catch (Exception e) {
    return response_string;

Call above method to get the HTTP-POST Response using the URL and Request String that we created with parameter.

private String URL = "";
String ResponseInXML = getResponseByXML(URL,  RequestString);
Log.d("ResponseInXML", ResponseInXML);

So, you will get your response in 
ResponseInXML . Then you can easily parse ResponseInXML  using any XML Parser. You can checkout for a Demo example for github.

Sunday, June 3, 2012

ListView with CheckBox Scrolling Issue

Today I am going to show how to deal with Custom ListView having Chekbox. Many developers are facing the issue of Checkbox item getting uncheck or check while scrolling the ListView. So, I will make it clear to developers how to deal with ListView having Checkbox. You can learn about the recycling of view in ListView from this Blog.

The issue with CheckBox inside ListView is that the view gets recycled due to recycling of ListView and the value of Checkbox(check or uncheck) is not maintained. To, maintain the state to CheckBox there has to be something that can store the state of Checkbox.

So, we have a Model class that will have name and selected property of ListView row having TextView and CheckBox.

public class Model {
    private String name;
    private boolean selected;
    public Model(String name) { = name;
    public String getName() {
        return name;
    public boolean isSelected() {
        return selected;
    public void setSelected(boolean selected) {
        this.selected = selected;

Now, we will setup the main Activity that will set the Adapter for the Custom ListView.

public class MainActivity extends Activity implements OnItemClickListener{
    ListView listView;
    ArrayAdapter<Model> adapter;
    List<Model> list = new ArrayList<Model>();
    public void onCreate(Bundle icicle) {
        listView = (ListView) findViewById(;
        adapter = new MyAdapter(this,getModel());
    public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
                TextView label = (TextView) v.getTag(;
CheckBox checkbox = (CheckBox) v.getTag(;
Toast.makeText(v.getContext(), label.getText().toString()+" "+isCheckedOrNot(checkbox), Toast.LENGTH_LONG).show();
    private String isCheckedOrNot(CheckBox checkbox) {
        return "is checked";
        return "is not checked";
    private List<Model> getModel() {
        list.add(new Model("Linux"));
        list.add(new Model("Windows7"));
        list.add(new Model("Suse"));
        list.add(new Model("Eclipse"));
        list.add(new Model("Ubuntu"));
        list.add(new Model("Solaris"));
        list.add(new Model("Android"));
        list.add(new Model("iPhone"));
        list.add(new Model("Java"));
        list.add(new Model(".Net"));
        list.add(new Model("PHP"));
        return list;


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
android:orientation="vertical" >

android:layout_height="fill_parent" />



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""
android:layout_height="wrap_content" >

android:textSize="30sp" >

android:focusableInTouchMode="false" >


Finally, now we will have the Adapter class.

public class MyAdapter extends ArrayAdapter<Model> {
    private final List<Model> list;
    private final Activity context;
    boolean checkAll_flag = false;
    boolean checkItem_flag = false;
    public MyAdapter(Activity context, List<Model> list) {
        super(context, R.layout.row, list);
        this.context = context;
        this.list = list;
    static class ViewHolder {
        protected TextView text;
        protected CheckBox checkbox;
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            LayoutInflater inflator = context.getLayoutInflater();
            convertView = inflator.inflate(R.layout.row, null);
            viewHolder = new ViewHolder();
            viewHolder.text = (TextView) convertView.findViewById(;
            viewHolder.checkbox = (CheckBox) convertView.findViewById(;
            viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    int getPosition = (Integer) buttonView.getTag();  // Here we get the position that we have set for the checkbox using setTag.
                    list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
            convertView.setTag(, viewHolder.text);
            convertView.setTag(, viewHolder.checkbox);
            } else {
            viewHolder = (ViewHolder) convertView.getTag();
        viewHolder.checkbox.setTag(position); // This line is important.
        return convertView;

So, the important line in the above Adapter class is to setTag() position of CheckBox and then retrieve it using getTag() inside onCheckedChanged() and then set the current state of CheckBox in your Model class instance. I am also attaching the complete source code for the same so that anyone can download and understand that how it works. Here is the complete source code.