本文整理汇总了Java中android.os.Looper类的典型用法代码示例。如果您正苦于以下问题:Java Looper类的具体用法?Java Looper怎么用?Java Looper使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Looper类属于android.os包,在下文中一共展示了Looper类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: run
import android.os.Looper; //导入依赖的package包/类
public void run () {
if (mIsConnected) return;
try {
mChannel = DatagramChannel.open();
mChannel.configureBlocking(false);
mChannel.connect(new InetSocketAddress(mAddress, mPort));
if (mListenerThread == null) {
mListenerThread = new ListenerThread();
mListenerThread.start();
mActivityHandler.sendMessage(composeMessage(MSG_ON_CONNECT, ""));
mIsConnected = true;
}
Looper.prepare();
mSendHandler = new Handler();
Looper.loop();
} catch (Exception e) {
mActivityHandler.sendMessage(composeMessage(MSG_ON_CONNECTION_FAIL, e.toString()));
}
}
示例2: SnackbarManager
import android.os.Looper; //导入依赖的package包/类
private SnackbarManager() {
mLock = new Object();
mHandler = new Handler(Looper.getMainLooper(), new Handler.Callback() {
@Override
public boolean handleMessage(Message message) {
switch (message.what) {
case MSG_TIMEOUT:
handleTimeout((SnackbarRecord) message.obj);
return true;
}
return false;
}
});
}
示例3: requestPermissions
import android.os.Looper; //导入依赖的package包/类
public static void requestPermissions(@NonNull final Activity activity, @NonNull final String[] permissions, final int requestCode) {
if (VERSION.SDK_INT >= 23) {
ActivityCompatApi23.requestPermissions(activity, permissions, requestCode);
} else if (activity instanceof OnRequestPermissionsResultCallback) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
public void run() {
int[] grantResults = new int[permissions.length];
PackageManager packageManager = activity.getPackageManager();
String packageName = activity.getPackageName();
int permissionCount = permissions.length;
for (int i = 0; i < permissionCount; i++) {
grantResults[i] = packageManager.checkPermission(permissions[i], packageName);
}
((OnRequestPermissionsResultCallback) activity).onRequestPermissionsResult(requestCode, permissions, grantResults);
}
});
}
}
示例4: log
import android.os.Looper; //导入依赖的package包/类
private void log(String logMsg){
if (isMainThread()){
mLogs.add(logMsg + "(主线程)");
mAdapter.notifyDataSetChanged();
}else {
mLogs.add(logMsg + "(非主线程)");
//此处必须在UI线程更新(因为此时RecycelrView可能还在计算布局或者滚动)
new Handler(Looper.getMainLooper())
.post(new Runnable() {
@Override
public void run() {
mAdapter.notifyDataSetChanged();
}
});
}
Log.d(TAG, "log: itemCount ->" + mAdapter.getItemCount());
}
示例5: setupTime
import android.os.Looper; //导入依赖的package包/类
private static void setupTime(final TextView timeView) {
final Handler handler = new Handler(Looper.getMainLooper());
TimerTask task = new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
@Override
public void run() {
mTime++;
if (mTime >= 3600) {
timeView.setText(String.format("%d:%02d:%02d", mTime / 3600, (mTime % 3600) / 60, (mTime % 60)));
} else {
timeView.setText(String.format("%02d:%02d", (mTime % 3600) / 60, (mTime % 60)));
}
}
});
}
};
timer = new Timer();
timer.schedule(task, 0, 1000);
}
示例6: startSeek
import android.os.Looper; //导入依赖的package包/类
public void startSeek() {
synchronized (mSync) {
mPlayWhenDoneSeek = isPlaying();
if (!isPaused() && !isSeeking()) {
Log.d(TAG, "start seeking with: request Seek");
mAudioDecoder.requestSeek();
mVideoDecoder.requestSeek();
} else if (!isSeeking()) {
Log.d(TAG, "start seeking with: seeking");
try {
mVideoDecoder.startSeeking();
mAudioDecoder.startSeeking();
Handler handler = new Handler(Looper.getMainLooper());
handler.post(mOnStartSeekingRunnable);
} catch (IOException e) {
Log.d(TAG, "error when start seek");
}
}
mSync.notifyAll();
}
}
示例7: StaticDataCacheHelper
import android.os.Looper; //导入依赖的package包/类
private StaticDataCacheHelper(){
bnItemDoor = new HashMap<>();
mHandler = new Handler(Looper.getMainLooper());
String validEntries = SharePreferenceHandler.getInstance().getString("VALID_BN_ITEMS",null);
if (validEntries == null){
bnValidEntries = new HashMap<>();
}else {
try {
Type type = new TypeToken<HashMap<String, Integer>>(){}.getType();
bnValidEntries = new Gson().fromJson(validEntries,type);
}catch (Exception e){
NLog.i(TagUtil.makeTag(getClass()),"bnValidEntries init ",e);
bnValidEntries = new HashMap<>();
}
}
}
示例8: findClass
import android.os.Looper; //导入依赖的package包/类
@Override
protected Class<?> findClass(String className) throws ClassNotFoundException {
Class<?> clazz = null;
if (Thread.currentThread().getId() != Looper.getMainLooper().getThread().getId()) {
BundleUtil.checkBundleStateSyncOnChildThread(className);
} else {
BundleUtil.checkBundleStateSyncOnUIThread(className);
}
clazz = loadFromInstalledBundles(className, true);
if (clazz != null)
return clazz;
ComponentName comp = new ComponentName(RuntimeVariables.androidApplication.getPackageName(),className);
if (isProvider(comp)){
return Atlas.class.getClassLoader().loadClass("android.taobao.atlas.util.FakeProvider");
}else if(isReceiver(comp)){
return Atlas.class.getClassLoader().loadClass("android.taobao.atlas.util.FakeReceiver");
}
throw new ClassNotFoundException("Can't find class " + className + printExceptionInfo());
}
示例9: uninstall
import android.os.Looper; //导入依赖的package包/类
public static synchronized void uninstall() {
if (!sInstalled) {
return;
}
sInstalled = false;
sExceptionHandler = null;
//卸载后恢复默认的异常处理逻辑,否则主线程再次抛出异常后将导致ANR,并且无法捕获到异常位置
Thread.setDefaultUncaughtExceptionHandler(sUncaughtExceptionHandler);
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
throw new QuitCockroachException("Quit Cockroach.....");//主线程抛出异常,迫使 while (true) {}结束
}
});
}
示例10: cancel
import android.os.Looper; //导入依赖的package包/类
/** {@inheritDoc} */
@SuppressWarnings("unchecked")
@Override
public void cancel() {
if (Looper.myLooper() == null) {
throw new AndroidRuntimeException("Animators may only be run on Looper threads");
}
if ((mStarted || mRunning) && getListeners() != null) {
if (!mRunning) {
// If it's not yet running, then start listeners weren't called. Call them now.
notifyStartListeners();
}
ArrayList<AnimatorListener> tmpListeners =
(ArrayList<AnimatorListener>) getListeners().clone();
for (AnimatorListener listener : tmpListeners) {
listener.onAnimationCancel(this);
}
}
endAnimation();
}
示例11: post
import android.os.Looper; //导入依赖的package包/类
/**
* Posts the given event to the event bus.
*/
public void post(Object event) {
PostingThreadState postingState = currentPostingThreadState.get();
List<Object> eventQueue = postingState.eventQueue;
eventQueue.add(event);
if (!postingState.isPosting) {
postingState.isMainThread = Looper.getMainLooper() == Looper.myLooper();
postingState.isPosting = true;
if (postingState.canceled) {
throw new EventBusException("Internal error. Abort state was not reset");
}
try {
while (!eventQueue.isEmpty()) {
postSingleEvent(null, eventQueue.remove(0), postingState);
}
} finally {
postingState.isPosting = false;
postingState.isMainThread = false;
}
}
}
示例12: sendPacket
import android.os.Looper; //导入依赖的package包/类
public void sendPacket(InetAddress address, int port, byte[] payload, int offset, int length) {
if (address == null)
return;
if (Looper.myLooper() == Looper.getMainLooper()) {
new Thread(() -> sendPacket(address, port, payload, offset, length));
return;
}
DatagramPacket packet = new DatagramPacket(payload, length);
packet.setAddress(address);
packet.setPort(port);
try {
socket.send(packet);
} catch (IOException e) {
e.printStackTrace();
}
}
示例13: ensureNotOnMainThread
import android.os.Looper; //导入依赖的package包/类
private static void ensureNotOnMainThread(Context context) {
Looper looper = Looper.myLooper();
if (looper != null && looper == context.getMainLooper()) {
throw new IllegalStateException("calling this from your main thread can lead to " +
"deadlock");
}
}
示例14: loadBitmapFromHttp
import android.os.Looper; //导入依赖的package包/类
/**
* 加载网络图片缓存到磁盘中
* @param uri
* @param reqWidth
* @param reqHeight
* @return
*/
private Bitmap loadBitmapFromHttp(String uri, int reqWidth, int reqHeight) {
if (Looper.myLooper() == Looper.getMainLooper()) {
throw new RuntimeException("can not visit network from UI thread.");
}
if (mDiskLruCache == null) {
return null;
}
String key = hashKeyFromUri(uri);
try {
DiskLruCache.Editor editor = mDiskLruCache.edit(key);
if (editor != null) {
OutputStream outputStream = editor.newOutputStream(DISK_CACHE_INDEX);
if (downloadBitmapToStream(uri, outputStream)){
editor.commit();
} else {
editor.abort();
}
mDiskLruCache.flush();
return loadBitmapFromDisCache(uri, reqWidth, reqHeight);
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
示例15: startup
import android.os.Looper; //导入依赖的package包/类
public void startup(Context context) throws Throwable {
if (!isStartUp) {
if (Looper.myLooper() != Looper.getMainLooper()) {
throw new IllegalStateException("VirtualCore.startup() must called in main thread.");
}
StubManifest.STUB_CP_AUTHORITY = context.getPackageName() + "." + StubManifest.STUB_DEF_AUTHORITY;
ServiceManagerNative.SERVICE_CP_AUTH = context.getPackageName() + "." + ServiceManagerNative.SERVICE_DEF_AUTH;
this.context = context;
mainThread = ActivityThread.currentActivityThread.call();
unHookPackageManager = context.getPackageManager();
hostPkgInfo = unHookPackageManager.getPackageInfo(context.getPackageName(), PackageManager.GET_PROVIDERS);
detectProcessType();
PatchManager patchManager = PatchManager.getInstance();
patchManager.init();
patchManager.injectAll();
ContextFixer.fixContext(context);
isStartUp = true;
if (initLock != null) {
initLock.open();
initLock = null;
}
}
}