我在某些设备上遇到了一个错误,有时在开始新活动时会出现 ANR。我的设备(Samsung Galaxy S2)有一点延迟,但我从来没有收到 ANR。有时会出现 ANR 的其他设备是:HTC Desire HD 和 Xperia(据我所知)。我认为这可能是我做的某种 cpu 密集型的事情,这使得旧手机比新手机滞后,但我不知道。我使用 Asynctask 来加载所有繁重的东西,而 ui 线程并没有真正做太多事情。
这是代码
我在这里创建一个单独的线程来访问共享首选项。
@Override
protected void onResume() {
super.onResume();
sound = false;
Thread prefThread = new Thread(new Runnable(){
public void run(){
SharedPreferences prefs = getSharedPreferences(SharedPrefValues.OPTION_PREF, Context.MODE_PRIVATE);
sound = prefs.getBoolean("sound", true);
String difficulty = prefs.getString(SharedPrefValues.DIFFICULTY, SharedPrefValues.DIF_MEDIUM);
prefs = getSharedPreferences(SharedPrefValues.HIGHSCORE_PREF, Context.MODE_PRIVATE);
for(int i = 0; i < mapPnl.buttons.size(); i++)
mapPnl.SetRank(mapPnl.buttons.get(i), prefs.getString(SharedPrefValues.HIGHSCORE_RANK_NAME + difficulty + mapPnl.buttons.get(i).getName(), SharedPrefValues.RANK_NOT_OK));
}
});
prefThread.start();
mapPnl.speedX = 0;
}
bgTask 是一个 AsyncTask。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
Display display = getWindowManager().getDefaultDisplay();
screenSize = new Point();
screenSize.x = display.getWidth();
screenSize.y = display.getHeight();
mapPnl = new MapPnl(this);
LoadBitmapsStuff bgTask = new LoadBitmapsStuff(this);
bgTask.execute();
addContentView(mapPnl, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
}
这是绘制所有内容并处理 onTouchEvents 的 Surfaceview
private class MapPnl extends SurfaceView implements SurfaceHolder.Callback{
...
public MapPnl(Context context) {
super(context);
getHolder().addCallback(this);
buttons = new ArrayList<ButtonMap>();
posX = 0;
speedX = 0;
maxX = 0;
drawThread = new DrawThread(getHolder());
drawThread.setRunning(true);
}
....
}
我在surfaceCreated中启动drawThread
public void surfaceCreated(SurfaceHolder arg0) {
if(drawThread.getState() == Thread.State.NEW)
drawThread.start();
else if(drawThread.getState() == Thread.State.TERMINATED){
drawThread = new DrawThread(getHolder());
drawThread.setRunning(true);
drawThread.start();
}
}
Surfacedestroyed from guide on the web
public void surfaceDestroyed(SurfaceHolder arg0) {
boolean retry = true;
drawThread.setRunning(false);
while (retry) {
try {
drawThread.join();
retry = false;
} catch (InterruptedException e) {
// we will try it again and again...
}
}
}
我认为这是重要的代码。据我所知,UIthread 仅通过我显示的这些部分运行(可能不是 ondestroy,但我读到有人在另一个线程中遇到了问题。)。这应该在大多数系统上运行得相当快。
在 asyncTask 完成运行之前,drawthread 仅显示“正在加载”的文本。因此,当所有内容都被加载和绘制时,用户开始触摸屏幕,这就是 ANR 出现的时候。有小费吗?
更新(12-05-18):我设法从模拟器中获得了一个 logcat。我使用了慢速计算机,当我使用快速计算机时,ANR 没有出现。
PID 860 是活动
03-31 01:41:03.453: I/Process(71): Sending signal. PID: 860 SIG: 3
03-31 01:41:03.453: I/dalvikvm(860): threadid=3: reacting to signal 3
03-31 01:41:03.672: I/dalvikvm(860): Wrote stack traces to '/data/anr/traces.txt'
03-31 01:41:03.672: I/Process(71): Sending signal. PID: 71 SIG: 3
03-31 01:41:03.682: I/dalvikvm(71): threadid=3: reacting to signal 3
03-31 01:41:03.992: I/dalvikvm(71): Wrote stack traces to '/data/anr/traces.txt'
03-31 01:41:03.992: I/Process(71): Sending signal. PID: 381 SIG: 3
03-31 01:41:03.992: I/dalvikvm(381): threadid=3: reacting to signal 3
03-31 01:41:04.212: I/Process(71): Sending signal. PID: 349 SIG: 3
03-31 01:41:04.212: I/dalvikvm(349): threadid=3: reacting to signal 3
03-31 01:41:04.322: I/dalvikvm(349): Wrote stack traces to '/data/anr/traces.txt'
03-31 01:41:04.332: I/Process(71): Sending signal. PID: 664 SIG: 3
03-31 01:41:04.364: I/dalvikvm(664): threadid=3: reacting to signal 3
03-31 01:41:04.542: I/Process(71): Sending signal. PID: 364 SIG: 3
03-31 01:41:04.612: I/dalvikvm(364): threadid=3: reacting to signal 3
03-31 01:41:04.744: I/Process(71): Sending signal. PID: 371 SIG: 3
03-31 01:41:04.953: I/Process(71): Sending signal. PID: 822 SIG: 3
03-31 01:41:05.162: I/Process(71): Sending signal. PID: 139 SIG: 3
03-31 01:41:05.162: I/dalvikvm(139): threadid=3: reacting to signal 3
03-31 01:41:05.221: I/dalvikvm(822): threadid=3: reacting to signal 3
03-31 01:41:05.292: I/dalvikvm(371): threadid=3: reacting to signal 3
03-31 01:41:05.312: I/dalvikvm(139): Wrote stack traces to '/data/anr/traces.txt'
03-31 01:41:05.402: E/ActivityManager(71): ANR in se.blogspot.developingjsa.view (se.blogspot.developingjsa.view/.FirstMenu)
03-31 01:41:05.402: E/ActivityManager(71): Reason: keyDispatchingTimedOut
03-31 01:41:05.402: E/ActivityManager(71): Load: 1.79 / 1.63 / 1.31
03-31 01:41:05.402: E/ActivityManager(71): CPU usage from 5383ms to 64ms ago:
03-31 01:41:05.402: E/ActivityManager(71): elopingjsa.view: 51% = 49% user + 1% kernel / faults: 36 minor 7 major
03-31 01:41:05.402: E/ActivityManager(71): system_server: 44% = 41% user + 3% kernel / faults: 133 minor 1 major
03-31 01:41:05.402: E/ActivityManager(71): adbd: 1% = 0% user + 0% kernel
03-31 01:41:05.402: E/ActivityManager(71): m.android.phone: 1% = 0% user + 0% kernel / faults: 112 minor
03-31 01:41:05.402: E/ActivityManager(71): ronsoft.openwnn: 0% = 0% user + 0% kernel / faults: 22 minor
03-31 01:41:05.402: E/ActivityManager(71): com.svox.pico: 0% = 0% user + 0% kernel / faults: 7 minor
03-31 01:41:05.402: E/ActivityManager(71): logcat: 0% = 0% user + 0% kernel
03-31 01:41:05.402: E/ActivityManager(71): qemud: 0% = 0% user + 0% kernel
03-31 01:41:05.402: E/ActivityManager(71): id.defcontainer: 0% = 0% user + 0% kernel / faults: 6 minor
03-31 01:41:05.402: E/ActivityManager(71): ackageinstaller: 0% = 0% user + 0% kernel / faults: 5 minor
03-31 01:41:05.402: E/ActivityManager(71): TOTAL: 100% = 91% user + 8% kernel + 0% irq + 0% softirq