Com*_*ish 17 android alarmmanager android-asynctask
我有一个AsyncTask获取一些数据,然后用这个新数据更新UI.它已经工作好几个月,但我最近添加了一个功能,当有新数据时显示通知.现在,当我的应用程序通过通知启动时,有时我会得到此异常并且onPostExecute不会被调用.
这是应用程序启动时发生的情况:
1)展开UI并查找视图
2)取消AlarmManager检查新数据并重置警报的警报(通过).(这样,如果用户禁用警报,则在下次重新启动之前将其取消.)
3)开始AsyncTask.如果应用程序是从通知启动的,请传入一些数据,然后取消通知.
我被困在导致这个异常的原因上.似乎异常来自AsyncTask代码,所以我不确定如何解决它.
谢谢!
这是一个例外:
I/My App( 501): doInBackground exiting
W/MessageQueue( 501): Handler{442ba140} sending message to a Handler on a dead thread
W/MessageQueue( 501): java.lang.RuntimeException: Handler{442ba140} sending message to a Handler on a dead thread
W/MessageQueue( 501): at android.os.MessageQueue.enqueueMessage(MessageQueue.java:179)
W/MessageQueue( 501): at android.os.Handler.sendMessageAtTime(Handler.java:457)
W/MessageQueue( 501): at android.os.Handler.sendMessageDelayed(Handler.java:430)
W/MessageQueue( 501): at android.os.Handler.sendMessage(Handler.java:367)
W/MessageQueue( 501): at android.os.Message.sendToTarget(Message.java:348)
W/MessageQueue( 501): at android.os.AsyncTask$3.done(AsyncTask.java:214)
W/MessageQueue( 501): at java.util.concurrent.FutureTask$Sync.innerSet(FutureTask.java:252)
W/MessageQueue( 501): at java.util.concurrent.FutureTask.set(FutureTask.java:112)
W/MessageQueue( 501): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:310)
W/MessageQueue( 501): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
W/MessageQueue( 501): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
W/MessageQueue( 501): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
W/MessageQueue( 501): at java.lang.Thread.run(Thread.java:1096)
Run Code Online (Sandbox Code Playgroud)
编辑:这是我的onCreate主要活动中的方法(通知打开的方法).onClickListeners为了节省空间,我省略了一些.我不认为它们会产生任何影响,因为它们所连接的按钮没有按下.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // Call the parent
setContentView(R.layout.main); // Create the UI from the XML file
// Find the UI elements
controls = (SlidingDrawer) findViewById(R.id.drawer); // Contains the
// buttons
// comic = (ImageView) findViewById(R.id.comic); // Displays the comic
subtitle = (TextView) findViewById(R.id.subtitleTxt); // Textbox for the
// subtitle
prevBtn = (Button) findViewById(R.id.prevBtn); // The previous button
nextBtn = (Button) findViewById(R.id.nextBtn); // The next button
randomBtn = (Button) findViewById(R.id.randomBtn); // The random button
fetchBtn = (Button) findViewById(R.id.comicFetchBtn); // The go to specific id button
mostRecentBtn = (Button) findViewById(R.id.mostRecentBtn); // The button to go to the most recent comic
comicNumberEdtTxt = (EditText) findViewById(R.id.comicNumberEdtTxt); // The text box to Zooming image view setup
zoomControl = new DynamicZoomControl();
zoomListener = new LongPressZoomListener(this);
zoomListener.setZoomControl(zoomControl);
zoomComic = (ImageZoomView) findViewById(R.id.zoomComic);
zoomComic.setZoomState(zoomControl.getZoomState());
zoomComic.setImage(BitmapFactory.decodeResource(getResources(), R.drawable.defaultlogo));
zoomComic.setOnTouchListener(zoomListener);
zoomControl.setAspectQuotient(zoomComic.getAspectQuotient());
resetZoomState();
// enter the new id
imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); // Used to hide the soft keyboard
Log.i(LOG_TAG, "beginning loading of first comic");
int notificationComicNumber = getIntent().getIntExtra("comic", -1);
Log.i(LOG_TAG, "comic number from intent: " + notificationComicNumber);
if (notificationComicNumber == -1) {
fetch = new MyFetcher(this, zoomComic, subtitle, controls, comicNumberEdtTxt, imm, zoomControl);
fetch.execute(MyFetcher.LAST_DISPLAYED_COMIC);
} else {
fetch = new MyFetcher(this, zoomComic, subtitle, controls, comicNumberEdtTxt, imm, zoomControl);
fetch.execute(notificationComicNumber);
((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).cancelAll();
}
Log.i(LOG_TAG, "ending loading of new comic");
Log.i(LOG_TAG, "first run checks beginning");
// Get SharedPreferences
prefs = getSharedPreferences("prefs", Context.MODE_PRIVATE);
// Check if this is the first run of the app for this version
if (prefs.getBoolean("firstRun-" + MAJOR_VERSION_NUMBER, true)) {
prefs.edit().putBoolean("firstRun-" + MAJOR_VERSION_NUMBER, false).commit();
firstRunVersionDialog();
}
// Check if this is the first run of the app
if (prefs.getBoolean("firstRun", true)) {
prefs.edit().putBoolean("firstRun", false).commit();
firstRunDialog();
}
Log.i(LOG_TAG, "First run checks done");
// OnClickListener s for the buttons omitted to save space
Run Code Online (Sandbox Code Playgroud)
编辑2:我一直在挖掘Android源代码,追踪异常的来源.这是线456和457 sendMessageAtTime中Handler:
msg.target = this;
sent = queue.enqueueMessage(msg, uptimeMillis);
Run Code Online (Sandbox Code Playgroud)
这是enqueueMessage来自MessageQueue:
final boolean enqueueMessage(Message msg, long when) {
if (msg.when != 0) {
throw new AndroidRuntimeException(msg
+ " This message is already in use.");
}
if (msg.target == null && !mQuitAllowed) {
throw new RuntimeException("Main thread not allowed to quit");
}
synchronized (this) {
if (mQuiting) {
RuntimeException e = new RuntimeException(
msg.target + " sending message to a Handler on a dead thread");
Log.w("MessageQueue", e.getMessage(), e);
return false;
} else if (msg.target == null) {
mQuiting = true;
}
msg.when = when;
//Log.d("MessageQueue", "Enqueing: " + msg);
Message p = mMessages;
if (p == null || when == 0 || when < p.when) {
msg.next = p;
mMessages = msg;
this.notify();
} else {
Message prev = null;
while (p != null && p.when <= when) {
prev = p;
p = p.next;
}
msg.next = prev.next;
prev.next = msg;
this.notify();
}
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
我对什么mQuiting是有点困惑,但看起来前一次enqueueMessage被称为msg.targetnull.
Jon*_*low 40
这是由于Android框架中的AsyncTask中存在错误.AsyncTask.java具有以下代码:
private static final InternalHandler sHandler = new InternalHandler();
Run Code Online (Sandbox Code Playgroud)
它期望在主线程上初始化它,但是这不能保证,因为它将在导致类运行其静态初始化器的任何线程上初始化.我重现了Handler引用工作线程的问题.
导致这种情况发生的常见模式是使用IntentService类.C2DM示例代码执行此操作.
一个简单的解决方法是将以下代码添加到应用程序的onCreate方法:
Class.forName("android.os.AsyncTask");
Run Code Online (Sandbox Code Playgroud)
这将强制在主线程中初始化AsyncTask.我在android bug数据库中提交了一个bug.请参阅http://code.google.com/p/android/issues/detail?id=20915.
sdw*_*sdw 18
为了概括Jonathan Perlow针对他明确指出的错误的解决方案,我在任何使用AsyncTask的类中使用以下内容.looper/handler/post是如何在Android应用程序中的任何位置运行UI线程而不传递活动或其他上下文句柄的方法.在类中添加此静态初始化块:
{ // https://stackoverflow.com/questions/4280330/onpostexecute-not-being-called-in-asynctask-handler-runtime-exception
Looper looper = Looper.getMainLooper();
Handler handler = new Handler(looper);
handler.post(new Runnable() {
public void run() {
try {
Class.forName("android.os.AsyncTask");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
});
}
Run Code Online (Sandbox Code Playgroud)
我们在尝试运行单元测试时遇到了问题.我找到了解决方法,但没有具体确定问题.我们只知道尝试在Android JUnit测试中使用AsyncTask <>导致onPostExecute()不被调用.现在我们知道为什么.
这篇文章展示了如何在Android JUnit测试中运行多线程异步代码:
在基于Android AsyncTask的JUnit测试中使用CountDownLatch
为了与非UI单元测试一起使用,我创建了一个简单的android.test.InstrumentationTestCase子类.它有一个"ok"标志和一个CountDownLatch.reset()或reset(count)创建一个新的CountDownLatch({1,count}).good()在latch上设置ok = true,count--和calls.countDown().bad()设置ok = false,并一直倒计时.waitForIt(seconds)等待超时或coundown latch为零.然后它调用assertTrue(ok).
那么测试就像:
someTest() {
reset();
asyncCall(args, new someListener() {
public void success(args) { good(); }
public void fail(args) { bad(); }
});
waitForIt();
}
Run Code Online (Sandbox Code Playgroud)
由于AsyncTask静态初始化错误,我们必须在传递给runTestOnUiThread()的Runnable中运行我们的实际测试.如上所述进行适当的静态初始化,除非正在测试的调用需要在UI线程上运行,否则这不是必需的.
我现在使用的另一个习惯是测试当前线程是否是UI线程,然后在适当的线程上运行所请求的操作,无论如何.有时,允许调用者请求同步与异步,在必要时覆盖是有意义的.例如,网络请求应始终在后台线程上运行.在大多数情况下,AsyncTask线程池是完美的.只是意识到只有一个数字会同时运行,阻止其他请求.要测试当前线程是否是UI线程:
boolean onUiThread = Looper.getMainLooper().getThread() == Thread.currentThread();
Run Code Online (Sandbox Code Playgroud)
然后,使用一个简单的子类的AsyncTask的(只是doInBackground()和onPostExecute()需要)<>到在非UI线程或handler.post()或postDelayed()运行到UI线程上运行.
为调用者提供运行同步或异步的选项(获取本地未显示的本地有效onUiThread值;如上所述添加本地布尔值):
void method(final args, sync, listener, callbakOnUi) {
Runnable run = new Runnable() { public void run() {
// method's code... using args or class members.
if (listener != null) listener(results);
// Or, if the calling code expects listener to run on the UI thread:
if (callbackOnUi && !onUiThread)
handler.post(new Runnable() { public void run() {listener()}});
else listener();
};
if (sync) run.run(); else new MyAsync().execute(run);
// Or for networking code:
if (sync && !onUiThread) run.run(); else new MyAsync().execute(run);
// Or, for something that has to be run on the UI thread:
if (sync && onUiThread) run.run() else handler.post(run);
}
Run Code Online (Sandbox Code Playgroud)
此外,使用AsyncTask可以非常简单和简洁.使用下面的RunAsyncTask.java定义,然后编写如下代码:
RunAsyncTask rat = new RunAsyncTask("");
rat.execute(new Runnable() { public void run() {
doSomethingInBackground();
post(new Runnable() { public void run() { somethingOnUIThread(); }});
postDelayed(new Runnable() { public void run() { somethingOnUIThreadInABit(); }}, 100);
}});
Run Code Online (Sandbox Code Playgroud)
或者简单地说:new RunAsyncTask("").execute(new Runnable(){public void run(){doSomethingInBackground();}});
RunAsyncTask.java:
package st.sdw;
import android.os.AsyncTask;
import android.util.Log;
import android.os.Debug;
public class RunAsyncTask extends AsyncTask<Runnable, String, Long> {
String TAG = "RunAsyncTask";
Object context = null;
boolean isDebug = false;
public RunAsyncTask(Object context, String tag, boolean debug) {
this.context = context;
TAG = tag;
isDebug = debug;
}
protected Long doInBackground(Runnable... runs) {
Long result = 0L;
long start = System.currentTimeMillis();
for (Runnable run : runs) {
run.run();
}
return System.currentTimeMillis() - start;
}
protected void onProgressUpdate(String... values) { }
protected void onPostExecute(Long time) {
if (isDebug && time > 1) Log.d(TAG, "RunAsyncTask ran in:" + time + " ms");
v = null;
}
protected void onPreExecute() { }
/** Walk heap, reliably triggering crash on native heap corruption. Call as needed. */
public static void memoryProbe() {
System.gc();
Runtime runtime = Runtime.getRuntime();
Double allocated = new Double(Debug.getNativeHeapAllocatedSize()) / 1048576.0;
Double available = new Double(Debug.getNativeHeapSize()) / 1048576.0;
Double free = new Double(Debug.getNativeHeapFreeSize()) / 1048576.0;
long maxMemory = runtime.maxMemory();
long totalMemory = runtime.totalMemory();
long freeMemory = runtime.freeMemory();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13074 次 |
| 最近记录: |