tags:

views:

45

answers:

3

Hi friends, my android application shows an error of keydispatching timeout. i does'nt happen every time when i touch the screen. i happens after some seconds of application starting. What will be the reason for this?? I used 4 imageviews,oncreate(). When i touch the imageview, it has to download images from the server through GPRS. I implemented main thread's full operations in onCreate() method. Is my probem with this?? Should i implement onStart(),onResume(),onPause(),onRestart() and onDestroy() methods??

A: 

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);

    dlgAlert   = new AlertDialog.Builder(this);
    //      location   = new Location(provider);
    imView     = (ImageView)findViewById(R.id.mapview);
    zoomInView = (ImageView)findViewById(R.id.zoomin);
    zoomOutView = (ImageView)findViewById(R.id.zoomout);
    exitView = (ImageView)findViewById(R.id.exit);

imView.setOnTouchListener(new View.OnTouchListener(){ public boolean onTouch(View view, MotionEvent event) { if(event.getAction() == MotionEvent.ACTION_DOWN ){ //opertions for down action }else if (event.getAction() == MotionEvent.ACTION_UP) { //operations for up action }

asifkt
+1  A: 

You can't do processing that takes any notable degree of time (such as downloading something from the net) in the UI thread, which is where all of those onWhatever methods are called.

You must move this part of the functionality to a service or do it in a looper thread.

Chris Stratton
Please don't use a service for this. Use a AsyncTask http://developer.android.com/intl/de/reference/android/os/AsyncTask.html
Janusz
I'll defer to you on that. My main point was get it out of the UI thread.
Chris Stratton
+2  A: 

I believe you are getting ANR(App Not Responding) message .

I suggest you to do downloading the image in a separate thread. Use AsyncTask to do such downloading tasks & avoid downloading data in main Thread's onCreate() or any other lifecycle method

 private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
     protected Long doInBackground(URL... urls) {
         int count = urls.length;
         long totalSize = 0;
         for (int i = 0; i < count; i++) {
             totalSize += Downloader.downloadFile(urls[i]);
             publishProgress((int) ((i / (float) count) * 100));
         }
         return totalSize;
     }

     protected void onProgressUpdate(Integer... progress) {
         setProgressPercent(progress[0]);
     }

     protected void onPostExecute(Long result) {
         showDialog("Downloaded " + result + " bytes");
     }
 }

Check the link http://developer.android.com/reference/android/os/AsyncTask.html

100rabh