在主线程中工作太多,应用冻结了

时间:2014-02-26 17:54:35

标签: android

在这里,我打电话给一个活动,这是一个聊天应用程序。

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.
.   .      .   .      .  
.   .      .   .      .
.   .      .   .      .

这是我从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);
                    }
                });

如何防止这种情况,应用程序在调用activity后挂起。看错了什么?

更新:

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) {
    }
}

更新:

您可以在this主题中找到详细信息。

3 个答案:

答案 0 :(得分:7)

startActivity(i);只需向系统发送一个Intent。所以你不需要在单独的线程中启动活动。

相反,您应该在另一个帖子中使用SplashActivity onCreate方法完成所有艰苦工作。

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();
 }
}

请记住,您只能在主Android(通常称为UI)线程上更新您的UI。

我建议您的QBAuth.createSession(this);正在打开某种Http连接,这就是为什么你的应用程序会冻结。将其移至单独的线程。

更新:

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();
            }
        }
    });
}
}

答案 1 :(得分:3)

首先,主要活动中的处理程序完全没用。系统在UI线程上调用onClick回调,并且您的处理程序也绑定到UI线程,因此您可以不使用它。但我怀疑你是在那里遇到问题,这只是一个小小的开销。

您可能想尝试移动电话

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

到后台线程,但是可能会在UI线程上调用它们,这会导致一些问题。

由于您的代码是1:1,代码在这里:https://github.com/QuickBlox/quickblox-android-sdk/tree/master/sample-chat,我首先尝试使用示例应用来检查问题是否也可以在那里看到。如果是,只需在github projet中打开一个问题:)

如果不是,那么你的做法有所不同,我们需要更多代码来调查...

答案 2 :(得分:1)

尝试更改:

android:label="@string/app_name"

从您的被叫活动中剪切,然后粘贴您的启动器活动。

<activity android:name="com.quickblox.sample.chat.Launch"
 android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>