解决方案 »
- android 中flash 全屏怎么实现的!求解,谢谢。
- 关于android usb摄像头的连接
- 拨号后自动输入预数据
- 文件路径Uri中带中文,系统默认转成16进制码,在getData()后怎么处理还原中文呢?
- android 更新和恢复出厂设置的api 是什么?
- android:launchMode="singleTask"的问题
- minSdkVersion为何用不好呢?
- android平台下手机拍照速度何如?
- 关于父Activity更新UI
- 使用Navigation组件,如何实现多个Fragment间任意跳转
- 关于android开发用viewflipper实现滑屏问题
- 在手机上模拟总是意外停止
我不是要在子线程里面创建Handler,而是在Service中创建Handler,但是不调用Looper.prepare()也不对,调用也不对。
一个Thread只可以set一个Looper,service默认是主线程,在Activity启动的时候已经loop起来了,所以会抛出异常。 private static void prepare(boolean quitAllowed) {
if (sThreadLocal.get() != null) {
throw new RuntimeException("Only one Looper may be created per thread");
}
sThreadLocal.set(new Looper(quitAllowed));
}
public Handler(Callback callback, boolean async) {
if (FIND_POTENTIAL_LEAKS) {
final Class<? extends Handler> klass = getClass();
if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) &&
(klass.getModifiers() & Modifier.STATIC) == 0) {
Log.w(TAG, "The following Handler class should be static or leaks might occur: " +
klass.getCanonicalName());
}
} mLooper = Looper.myLooper();
if (mLooper == null) {
throw new RuntimeException(
"Can't create handler inside thread that has not called Looper.prepare()");
}
mQueue = mLooper.mQueue;
mCallback = callback;
mAsynchronous = async;
}