Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

android - Too much work in main thread, app freezes

Here I make call to an activity, which is a chat application.

The catLog:

02-26 12:30:38.996: I/Choreographer(807): Skipped 35 frames!  The application may be doing too much work on its main thread.
02-26 12:30:39.196: I/Choreographer(807): Skipped 31 frames!  The application may be doing too much work on its main thread.
02-26 12:30:39.516: I/Choreographer(807): Skipped 31 frames!  The application may be doing too much work on its main thread.
02-26 12:30:39.996: I/Choreographer(807): Skipped 32 frames!  The application may be doing too much work on its main thread.
02-26 12:30:40.066: I/Choreographer(807): Skipped 37 frames!  The application may be doing too much work on its main thread.
02-26 12:30:40.207: I/Choreographer(807): Skipped 33 frames!  The application may be doing too much work on its main thread.
02-26 12:30:40.896: I/Choreographer(807): Skipped 33 frames!  The application may be doing too much work on its main thread.
02-26 12:30:41.586: I/Choreographer(807): Skipped 30 frames!  The application may be doing too much work on its main thread.
02-26 12:30:42.266: I/Choreographer(807): Skipped 30 frames!  The application may be doing too much work on its main thread.
02-26 12:30:42.486: I/Choreographer(807): Skipped 31 frames!  The application may be doing too much work on its main thread.
02-26 12:30:42.556: I/Choreographer(807): Skipped 37 frames!  The application may be doing too much work on its main thread.
02-26 12:30:42.826: I/Choreographer(807): Skipped 32 frames!  The application may be doing too much work on its main thread.
02-26 12:30:43.316: I/Choreographer(807): Skipped 30 frames!  The application may be doing too much work on its main thread.
02-26 12:30:43.777: I/Choreographer(807): Skipped 30 frames!  The application may be doing too much work on its main thread.
02-26 12:30:43.826: I/Choreographer(807): Skipped 32 frames!  The application may be doing too much work on its main thread.
02-26 12:30:43.866: I/Choreographer(807): Skipped 34 frames!  The application may be doing too much work on its main thread.
.   .      .   .      .  
.   .      .   .      .
.   .      .   .      .

This is how I start the activity from MainActivity:

public class MainActivity extends Activity {
     Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button b = (Button) findViewById(R.id.button1);
     handler = new Handler(Looper.getMainLooper());


    b.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            final  Intent i = new Intent(com.example.mainactivity.MainActivity.this,com.quickblox.sample.chat.ui.activities.SplashActivity.class);
             handler.post(new Runnable() {
                    @Override
                    public void run() {
                        startActivity(i);
                    }
                });

How can I prevent this,the app hangs after calling the activity.Whats wrong?

UPDATE:

SplashActivity:

public class SplashActivity extends Activity implements QBCallback {

    private static final String APP_ID = "7467";
    private static final String AUTH_KEY = "TxRFWfX8tTXQ4gv";
    private static final String AUTH_SECRET = "y-QJrO2j69VTaCs";

    private ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        progressBar = (ProgressBar) findViewById(R.id.progressBar);

        QBSettings.getInstance().fastConfigInit(APP_ID, AUTH_KEY, AUTH_SECRET);
        QBAuth.createSession(this);
    }

    @Override
    public void onComplete(Result result) {
        progressBar.setVisibility(View.GONE);

        if (result.isSuccess()) {
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
            finish();
        } else {
            AlertDialog.Builder dialog = new AlertDialog.Builder(this);
            dialog.setMessage("Error(s) occurred. Look into DDMS log for details, " +
                    "please. Errors: " + result.getErrors()).create().show();
        }
    }

    @Override
    public void onComplete(Result result, Object context) {
    }
}

UPDATE :

You can find Details in this thread.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

startActivity(i); just send an Intent to the system. So you don't need to start activity in seperate thread.

Instead you should do all your hard work fromSplashActivity onCreate method in another thread.

public class MainActivity extends Activity {
//...
 @Override
 protected void onCreate(Bundle savedInstanceState) {
   //...
   final  Intent intent = new Intent(this,SplashActivity.class);
   startActivity(intent);
 }
}

public class SplashActivity extends Activity {
//...
 @Override
 protected void onCreate(Bundle savedInstanceState) {
   //...
   new Thread(new Runnable()
   {
     @Override
     public void run() {
      //do loading data or whatever hard here

      runOnUiThread(new Runnable(){
        @Override
        public void run() {
          //..update your UI here
        }
      });
     }
   }).start();
 }
}

Remeber that you can update your UI only on Main Android(often called as UI) thread.

I suggest that yours QBAuth.createSession(this); are opening some sort of Http connection, that why your App gets freeze. Move it to separate thread.

UPDATE :

public class SplashActivity extends Activity{

private static final String APP_ID = "7467";
private static final String AUTH_KEY = "TxRFWfX8tTXQ4gv";
private static final String AUTH_SECRET = "y-QJrO2j69VTaCs";

public static class QBAuth{
    private static QBCallback mQBCallback;

    public static void createSession(QBCallback qbCallback) {
        // do request initialization
        mQBCallback = qbCallback;
    }
}

public interface QBCallback{
    public void onComplete(Result result);
    public void onComplete(Result result, Object context);
}

public class Result{
    boolean isSuccess;
    String errors;

    public boolean isSuccess() {
        return isSuccess;
    }

    public void setSuccess(boolean isSuccess) {
        this.isSuccess = isSuccess;
    }

    public String getErrors() {
        return errors;
    }

    public void setErrors(String errors) {
        this.errors = errors;
    }
}

private ProgressBar progressBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);

    progressBar = (ProgressBar) findViewById(R.id.progressBar);


    new Thread(new Runnable() {
        @Override
        public void run() {
            QBSettings.getInstance().fastConfigInit(APP_ID, AUTH_KEY, AUTH_SECRET);
            QBAuth.createSession(mQBCallback);
        }
    });
}


private QBCallback mQBCallback = new QBCallback() {
    @Override
    public void onComplete(Result result) {
        handleResult(result);
    }

    @Override
    public void onComplete(Result result, Object context) {

    }
};

private void handleResult(final Result result) {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            progressBar.setVisibility(View.GONE);

            if (result.isSuccess()) {
                Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            } else {
                AlertDialog.Builder dialog = new AlertDialog.Builder(SplashActivity.this);
                dialog.setMessage("Error(s) occurred. Look into DDMS log for details, " +
                        "please. Errors: " + result.getErrors()).create().show();
            }
        }
    });
}
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...