??ANR(Activity Not Respone)(無響應)
? ? 先介紹下Main線程(也稱為UI線程、主線程)
? ? 功能: 1.創建UI控件
? ? ? ? ? ? ?2.更新UI控件狀態
? ? ? ? ? ? ?3.事件處理
? ? 限制:Main線程不建議有超過5秒的事件
? ? 出現條件:
? ? ? ? 當用戶輸入事件5s內沒有得到響應,將彈出ANR對話框
? ? ? ??廣播接收者的onReceive()執行時間超過10s
? ? 解決方案(原則):
? ? ? ? 所有可能的耗時操作都要在子線程()中執行
? ? ? ? 常見耗時操作:
? ? ? ? ? ? I/O:網絡操作
? ? ? ? ? ? ? ? ? ?SDcard?
? ? ? ? ? ? 數據運算
FC(Force close)
? ? 原因:
????? ? 1.Error
????? ? OOM(out of memory error)
????? ? StackOverFlowError
????? ? 2.RuntimeException
? ? 解決辦法:
? ? ? ? 看日志
? ? 子線程不能更新UI的解決思路:
? ? ?? 1、 將子線程執行結果發送到Main線程:handler+massage線程間通訊
? ? ? ? ? ? 發送消息:
????????????????????????Message?msg?=?mHandler.obtainMessage(UPLOAD,?json);? ???
????????????????????????mHandler.sendMessage(msg);
? ??? ??? ? 接收消息:
? ? ? ??public?void?handleMessage(Message?msg)?{
????????????switch?(msg.what)?{
????????????case?UPLOAD:
????????????????mTextView2.setText((String)?msg.obj);
????????????break;
????????????case?DOWNLOAD:
????????????????mTextView.setText((String)?msg.obj);
????????????????break;
????????????default:
????????????????break;
轉載于:https://blog.51cto.com/1206995290qq/1844257