Other Threads

1. List View not properly updated from AsyncTask

Hi Everyone,

After digging with the APIs I still cannot figure out how the
AsyncTask properly works and why some behavior occur.

I'm trying  to achieve one contact list that can be updated from a
background async task very frequently. There are situations where
invoking UI updates from the background thread just freeze the UI as
you see in example #2. or make the list not to react properly to
finger as a regular contact list. Can anyone give me some hint on how
to achieve the expected behavior?

I added here the activities that form my project (sorry for the lenght
of the post, my idea was to share the complete problem with everyone)

Example 1: Contact list without background task. This code contains
the behavior of a default contact list. This code produce the expected
behavior in responsive for the user but do not update. This activity
shows the ideal response for a contact list.

package com.pp.lists;

import android.app.ListActivity;
import android.content.Intent;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;

public class List1 extends ListActivity implements
View.OnClickListener {

    Button btnBack = null;
    SensorManager sensorManager = null;

    String[] items = { "lorem", "ipsum", "dolor", "sit", "amet",
"consectetuer", "adipiscing", "elit", "morbi", "vel",
            "ligula", "vitae", "arcu", "aliquet", "mollis", "etiam",
"vel", "erat", "placerat", "ante", "porttitor",
            "sodales", "pellentesque", "augue", "purus", "lorem",
"ipsum", "dolor", "sit", "amet", "consectetuer",
            "adipiscing", "elit", "morbi", "vel", "ligula", "vitae",
"arcu", "aliquet", "mollis", "etiam", "vel",
            "erat", "placerat", "ante", "porttitor", "sodales",
"pellentesque", "augue", "purus", "lorem", "ipsum",
            "dolor", "sit", "amet", "consectetuer", "adipiscing",
"elit", "morbi", "vel", "ligula", "vitae", "arcu",
            "aliquet", "mollis", "etiam", "vel", "erat", "placerat",
"ante", "porttitor", "sodales", "pellentesque",
            "augue", "purus", "lorem", "ipsum", "dolor", "sit",
"amet", "consectetuer", "adipiscing", "elit", "morbi",
            "vel", "ligula", "vitae", "arcu", "aliquet", "mollis",
"etiam", "vel", "erat", "placerat", "ante",
            "porttitor", "sodales", "pellentesque", "augue",
"purus" };

    /** Called when the activity is first created. */
    @Override
    public void onCreate( Bundle savedInstanceState ) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listdemo);
        setListAdapter(new ArrayAdapter<String>(this,
R.layout.listitem, R.id.label, items));

        btnBack = (Button) findViewById(R.id.btnBackFromList);
        btnBack.setOnClickListener(this);
    }

    public void onClick( View arg0 ) {
        if ( btnBack == arg0 ) {
            startActivity(new Intent(this, Main.class));
        }
    }

    public void onResume() {
        System.out.println("List1.onResume!");
        super.onResume();
    }

    public void onPause() {
        System.out.println("List1.onPause!");
        super.onPause();
    }

}

Example 2: This example update the contact list very frequently
properly but without freezing DO NOT allow the user to even scroll.

package com.pp.lists;

import android.app.ListActivity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class List2 extends ListActivity implements
View.OnClickListener {

    Button btnBack = null;
    Handler handler = new Handler();

    String[] items = { "lorem", "ipsum", "dolor", "sit", "amet",
"consectetuer", "adipiscing", "elit", "morbi", "vel",
            "ligula", "vitae", "arcu", "aliquet", "mollis", "etiam",
"vel", "erat", "placerat", "ante", "porttitor",
            "sodales", "pellentesque", "augue", "purus", "lorem",
"ipsum", "dolor", "sit", "amet", "consectetuer",
            "adipiscing", "elit", "morbi", "vel", "ligula", "vitae",
"arcu", "aliquet", "mollis", "etiam", "vel",
            "erat", "placerat", "ante", "porttitor", "sodales",
"pellentesque", "augue", "purus", "lorem", "ipsum",
            "dolor", "sit", "amet", "consectetuer", "adipiscing",
"elit", "morbi", "vel", "ligula", "vitae", "arcu",
            "aliquet", "mollis", "etiam", "vel", "erat", "placerat",
"ante", "porttitor", "sodales", "pellentesque",
            "augue", "purus", "lorem", "ipsum", "dolor", "sit",
"amet", "consectetuer", "adipiscing", "elit", "morbi",
            "vel", "ligula", "vitae", "arcu", "aliquet", "mollis",
"etiam", "vel", "erat", "placerat", "ante",
            "porttitor", "sodales", "pellentesque", "augue",
"purus" };

    private ArrayAdapter adapter = null;
    private ListView list = null;

    BGSorter task = null;

    EditText txtTest = null;
    public boolean isRunning = true;

    /** Called when the activity is first created. */
    @Override
    public void onCreate( Bundle savedInstanceState ) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listdemo);
        adapter = new ArrayAdapter<String>(this, R.layout.listitem,
R.id.label, items);
        setListAdapter(adapter);

        btnBack = (Button) findViewById(R.id.btnBackFromList);
        btnBack.setOnClickListener(this);

        list = this.getListView();
    }

    public void onClick( View arg0 ) {
        if ( btnBack == arg0 ) {
            startActivity(new Intent(this, Main.class));
        }
    }

    public void onResume() {

        WorkingObject.status = true;

        // Start BG job
        System.out.println("List2.onResume!");

        task = new BGSorter();
        task.execute();

        super.onResume();
    }

    public void onPause() {

        System.out.println("List2.onPause!");
        WorkingObject.status = false;
        task.cancel(true);
        super.onPause();
    }

    class BGSorter extends AsyncTask<Void, String, Void> {

        @Override
        protected void onProgressUpdate( String... args ) {
            try {
                System.out.println("OnProgressUpdate=" +
Thread.currentThread().getName());
                list.invalidateViews();
            } catch ( Exception e ) {
                System.out.println(e.toString());
            }
        }

        @Override
        protected Void doInBackground( Void... arg0 ) {
            System.out.println("DoBackground=" + Thread.currentThread
().getName());

            int random1 = (int) (Math.random() * 10000 % 10); //
items.length);
            int random2 = (int) (Math.random() * 10000 % 10); //
items.length);

            String temp = items[random1];
            items[random1] = items[random2];
            items[random2] = temp;

            publishProgress("random1=" + random1 + " random2=" +
random2);
            return null;
        }

        @Override
        protected void onPostExecute( Void result ) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);

            System.out.println(isCancelled());

            // Launch another copy of the thread
            // if ( isRunning ) {
            if ( WorkingObject.status ) {
                task = new BGSorter();
                task.execute();
            }
        }
    }

    class Holder {
        public int position = 0;
    }

    class UpdateList implements Runnable {
        @Override
        public void run() {
            System.out.println("UpdateList=" + Thread.currentThread
().getName());
            list.invalidateViews();
        }
    }
}

EXAMPLE 3: Allow the user to interact and scroll but not to 100%
accurately. You can see the difference between example 3 and 1 in
result. Here, you will see that after start the scrolling with the
finger,  the list do not respond properly if you try to stop the
scroll as in sample #1. Can anyone give me an idea why?

package com.pp.lists;

import android.app.ListActivity;
import android.content.Intent;
import android.hardware.SensorManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class List3 extends ListActivity implements
View.OnClickListener {

    Button btnBack = null;

    Handler handler = new Handler();

    String[] items = { "lorem", "ipsum", "dolor", "sit", "amet",
"consectetuer", "adipiscing", "elit", "morbi", "vel",
            "ligula", "vitae", "arcu", "aliquet", "mollis", "etiam",
"vel", "erat", "placerat", "ante", "porttitor",
            "sodales", "pellentesque", "augue", "purus", "lorem",
"ipsum", "dolor", "sit", "amet", "consectetuer",
            "adipiscing", "elit", "morbi", "vel", "ligula", "vitae",
"arcu", "aliquet", "mollis", "etiam", "vel",
            "erat", "placerat", "ante", "porttitor", "sodales",
"pellentesque", "augue", "purus", "lorem", "ipsum",
            "dolor", "sit", "amet", "consectetuer", "adipiscing",
"elit", "morbi", "vel", "ligula", "vitae", "arcu",
            "aliquet", "mollis", "etiam", "vel", "erat", "placerat",
"ante", "porttitor", "sodales", "pellentesque",
            "augue", "purus", "lorem", "ipsum", "dolor", "sit",
"amet", "consectetuer", "adipiscing", "elit", "morbi",
            "vel", "ligula", "vitae", "arcu", "aliquet", "mollis",
"etiam", "vel", "erat", "placerat", "ante",
            "porttitor", "sodales", "pellentesque", "augue",
"purus" };

    private ArrayAdapter adapter = null;
    private ListView list = null;

    BGSorter task = null;

    EditText txtTest = null;
    public boolean isRunning = true;

    /** Called when the activity is first created. */
    @Override
    public void onCreate( Bundle savedInstanceState ) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listdemo);
        adapter = new ArrayAdapter<String>(this, R.layout.listitem,
R.id.label, items);

        setListAdapter(adapter);

        btnBack = (Button) findViewById(R.id.btnBackFromList);
        btnBack.setOnClickListener(this);

        list = this.getListView();
    }

    public void onClick( View arg0 ) {
        if ( btnBack == arg0 ) {
            startActivity(new Intent(this, Main.class));
        }
    }

    public void onResume() {

        // Start BG job
        System.out.println("List2.onResume!");

        task = new BGSorter();
        task.execute();

        super.onResume();
    }

    public void onPause() {

        System.out.println("List2.onPause!");
        task.cancel(true);
        super.onPause();
    }

    class BGSorter extends AsyncTask<Void, String, Void> {

        @Override
        protected void onProgressUpdate( String... args ) {
            try {
                System.out.println("OnProgressUpdate=" +
Thread.currentThread().getName());
                list.invalidateViews();
            } catch ( Exception e ) {
                System.out.println(e.toString());
            }
        }

        @Override
        protected Void doInBackground( Void... arg0 ) {

            while ( !isCancelled() ) {
                System.out.println("DoBackground=" +
Thread.currentThread().getName());

                int random1 = (int) (Math.random() * 10000 % 10); //
items.length);
                int random2 = (int) (Math.random() * 10000 % 10); //
items.length);

                String temp = items[random1];
                items[random1] = items[random2];
                items[random2] = temp;

                publishProgress("random1=" + random1 + " random2=" +
random2);
            }
            return null;
        }

    }

    class Holder {
        public int position = 0;
    }

    class UpdateList implements Runnable {
        @Override
        public void run() {
            System.out.println("UpdateList=" + Thread.currentThread
().getName());
            adapter.notifyDataSetChanged();
        }
    }
}

Thanks for the time. I'will be more than glad to receive your
feedback.
Pablo
--~--~---------~--~----~------------~-------~--~----~

2. Another question MapView

Hi again, im confused because i have this issue:

I run app in emulator(MapView example from apis demo) but when is load only
show map with googles logo.
I see this in the log:
07-14 13:25:32.925: ERROR/ActivityThread(731): Failed to find provider info
for com.google.settings
07-14 13:25:33.456: INFO/MapActivity(731): Handling network change
notification:CONNECTED
07-14 13:25:33.465: ERROR/MapActivity(731): Couldn't get connection factory
client
07-14 13:25:33.906: DEBUG/dalvikvm(731): GC freed 4645 objects / 283496
bytes in 128ms
07-14 13:25:34.365: INFO/ActivityManager(570): Displayed activity
com.HelloMap/.HelloMap: 2889 ms
07-14 13:25:34.795: DEBUG/dalvikvm(731): GC freed 8042 objects / 599648
bytes in 108ms
07-14 13:25:34.895: INFO/InetAddress(731): Unknown host www.google.com,
throwing UnknownHostException
07-14 13:25:36.456: INFO/InetAddress(731): Unknown host www.google.com,
throwing UnknownHostException
07-14 13:25:38.025: INFO/InetAddress(731): Unknown host www.google.com,
throwing UnknownHostException
07-14 13:25:39.605: INFO/InetAddress(731): Unknown host www.google.com,
throwing UnknownHostException


But why with app maps show me the maps and the log is right:
07-14 13:30:41.435: INFO/ActivityManager(570): Displayed activity
com.google.android.apps.maps/com.google.android.maps.MapsActivity: 4857 ms
07-14 13:30:42.096: DEBUG/InetAddress(680): www.google.com: 209.85.229.105
(family 2, proto 6)
07-14 13:30:42.096: DEBUG/InetAddress(680): www.google.com: 209.85.229.104
(family 2, proto 6)
07-14 13:30:42.115: DEBUG/InetAddress(680): www.google.com: 209.85.229.103
(family 2, proto 6)
07-14 13:30:42.135: DEBUG/InetAddress(680): www.google.com: 209.85.229.99
(family 2, proto 6)
07-14 13:30:42.135: DEBUG/InetAddress(680): www.google.com: 209.85.229.106
(family 2, proto 6)
07-14 13:30:42.135: DEBUG/InetAddress(680): www.google.com: 209.85.229.147
(family 2, proto 6)


Regards

Iaki

--~--~---------~--~----~------------~-------~--~----~

3. Android on PSP?

4. oauth_callback is not calling the activity

5. how does omx component comes to know about the number of channels and sampling rate of the audio clip

6. How to build qwerty.kcm.bin file

7. Connect android emulator to internet