简体   繁体   English

根Droid上的AsyncTask问题

[英]Problem with AsyncTask on rooted Droid

I've got a widget on the Android market called DigiClock widget, and after the last update i've been having some extremely rare and random problems on rooted Motorola Droids ( there may be other handsets with the problem, but the only responses i've had are from rooted droid users ). 我在Android市场上有一个名为DigiClock的小部件,在最近一次更新后,我对根植的Motorola Droids遇到了一些极为罕见和随机的问题(可能还有其他手机有问题,但唯一的回答是“ ve来自具有root权限的droid用户)。 The problem occurs when an activity is launched that runs an AsyncTask that retrieves all the installed applications from the device while showing a ProgressDialog ( Horizontal style ). 启动运行AsyncTask的活动时会出现问题,该活动会在显示ProgressDialog(水平样式)的同时从设备中检索所有已安装的应用程序。 The applicable java code file can be found here: 可在此处找到适用的Java代码文件:

http://code.google.com/p/android-digiclockwidget/source/browse/trunk/src/com/davidgoemans/simpleClockWidget/LauncherChooser.java http://code.google.com/p/android-digiclockwidget/source/browse/trunk/src/com/davidgoemans/simpleClockWidget/LauncherChooser.java

If you wish to diff the changes made between a working and non-working version, that can be found here: 如果您希望区分在工作版本和非工作版本之间所做的更改,可以在这里找到:

http://code.google.com/p/android-digiclockwidget/source/diff?spec=svn10&old=7&r=9&format=side&path=/trunk/src/com/davidgoemans/simpleClockWidget/LauncherChooser.java http://code.google.com/p/android-digiclockwidget/source/diff?spec=svn10&old=7&r=9&format=side&path=/trunk/src/com/davidgoemans/simpleClockWidget/LauncherChooser.java

What seems to happen on the droid is: * Progress box pops up * Progress box dismisses before it's finished running * Empty list shows up 机器人上似乎发生了什么:*进度框弹出*进度框在完成运行前关闭*空列表显示

It seems like the AsyncTask that fetches the applications is getting killed. 似乎获取应用程序的AsyncTask被杀死了。 Is this possible? 这可能吗?

Thanks, David 谢谢大卫

EDIT : 编辑

Found the problem, turns out that in Android 2.0 ( NOT 2.1 or 1.6 ) 发现了问题,原来是在Android 2.0中(不是2.1或1.6)

List packages = getPackageManager().getInstalledPackages(PackageManager.GET_ACTIVITIES); 列出软件包= getPackageManager()。getInstalledPackages(PackageManager.GET_ACTIVITIES);

crashes in my situation, but 在我的情况下崩溃,但是

List packages = getPackageManager().getInstalledPackages(0); 列出软件包= getPackageManager()。getInstalledPackages(0);

doesn't. 没有。

I am not certain whether it is the cause of the symptoms but I can see some problems with the code: 我不确定是否是症状的原因,但是我可以看到一些代码问题:

  • You must not manipulate UI objects like your ProgressDialog from any thread except for the main UI thread, because UI objects are not thread-safe. 您不得从除主UI线程之外的任何线程中操作诸如ProgressDialog之类的UI对象,因为UI对象不是线程安全的。 Instead of calling progressDialog.setProgress(), call AsyncTask.publishProgress() and then override AsyncTask.onProgressUpdate(). 而不是调用progressDialog.setProgress(),而是调用AsyncTask.publishProgress(),然后重写AsyncTask.onProgressUpdate()。

http://developer.android.com/resources/articles/painless-threading.html http://developer.android.com/resources/articles/painless-threading.html

  • Also, your code does not prevent both threads from accessing "menuEntries" at the same time. 此外,您的代码也不会阻止两个线程同时访问“ menuEntries”。 The simplest way to fix this part would be to move the call to setListAdapter() out of onCreate() and into onPostExecute(). 解决此问题的最简单方法是将对setListAdapter()的调用从onCreate()移到onPostExecute()。

Found the problem , turns out that in Android 2.0 ( NOT 2.1 or 1.6 ) 找到了问题 ,原来是在Android 2.0中(不是2.1或1.6)

List packages = getPackageManager().getInstalledPackages(PackageManager.GET_ACTIVITIES);

crashes in my situation, but 在我的情况下崩溃,但是

List packages = getPackageManager().getInstalledPackages(0);

doesn't. 没有。 I found this by testing in the 2.0 emulator. 我通过在2.0模拟器中进行测试发现了这一点。 This adds to the pain of having to test my app now on 1.5, 1.6, 2.0 and 2.1 instead of just the fringe cases :/ 这增加了必须立即在1.5、1.6、2.0和2.1上测试我的应用程序的痛苦,而不仅仅是边缘情况:/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM