Java Code Examples for android.os.Looper#loop()

The following examples show how to use android.os.Looper#loop() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CameraGLView.java    From Tok-Android with GNU General Public License v3.0 6 votes vote down vote up
/**
 * message loop
 * prepare Looper and create Handler for this thread
 */
@Override
public void run() {
    LogUtil.i(TAG, "Camera thread start");
    Looper.prepare();
    synchronized (mReadyFence) {
        mHandler = new CameraGLView.CameraHandler(this);
        mIsRunning = true;
        mReadyFence.notify();
    }
    Looper.loop();
    LogUtil.i(TAG, "Camera thread finish");
    synchronized (mReadyFence) {
        mHandler = null;
        mIsRunning = false;
    }
}
 
Example 2
Source File: ComplexActivity.java    From AndroidProjects with MIT License 6 votes vote down vote up
@Override
public void run() {
    super.run();

    Looper.prepare();
    mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Log.e("Edwin", "LooperThread1---->what = " + msg.what + " ,currentThread:  " + Thread.currentThread());
        }
    };

    mHandler.sendEmptyMessage(1);
    Looper.loop();
    // TODO: 2017/2/27 Loop后面不能执行了
    mHandler.sendEmptyMessage(2);
    mHandler.sendEmptyMessage(3);
    mHandler.sendEmptyMessage(4);
    Looper.loop();
}
 
Example 3
Source File: TextureMovieEncoder.java    From AndroidPlayground with MIT License 6 votes vote down vote up
/**
 * Encoder thread entry point.  Establishes Looper/Handler and waits for messages.
 * <p>
 * @see java.lang.Thread#run()
 */
@Override
public void run() {
    // Establish a Looper for this thread, and define a Handler for it.
    Looper.prepare();
    synchronized (mReadyFence) {
        mHandler = new EncoderHandler(this);
        mReady = true;
        mReadyFence.notify();
    }
    Looper.loop();

    Log.d(TAG, "Encoder thread exiting");
    synchronized (mReadyFence) {
        mReady = mRunning = false;
        mHandler = null;
    }
}
 
Example 4
Source File: TextureMovieEncoder.java    From grafika with Apache License 2.0 6 votes vote down vote up
/**
 * Encoder thread entry point.  Establishes Looper/Handler and waits for messages.
 * <p>
 * @see java.lang.Thread#run()
 */
@Override
public void run() {
    // Establish a Looper for this thread, and define a Handler for it.
    Looper.prepare();
    synchronized (mReadyFence) {
        mHandler = new EncoderHandler(this);
        mReady = true;
        mReadyFence.notify();
    }
    Looper.loop();

    Log.d(TAG, "Encoder thread exiting");
    synchronized (mReadyFence) {
        mReady = mRunning = false;
        mHandler = null;
    }
}
 
Example 5
Source File: VideoStream.java    From libstreaming with Apache License 2.0 6 votes vote down vote up
/**
 * Opens the camera in a new Looper thread so that the preview callback is not called from the main thread
 * If an exception is thrown in this Looper thread, we bring it back into the main thread.
 * @throws RuntimeException Might happen if another app is already using the camera.
 */
private void openCamera() throws RuntimeException {
	final Semaphore lock = new Semaphore(0);
	final RuntimeException[] exception = new RuntimeException[1];
	mCameraThread = new Thread(new Runnable() {
		@Override
		public void run() {
			Looper.prepare();
			mCameraLooper = Looper.myLooper();
			try {
				mCamera = Camera.open(mCameraId);
			} catch (RuntimeException e) {
				exception[0] = e;
			} finally {
				lock.release();
				Looper.loop();
			}
		}
	});
	mCameraThread.start();
	lock.acquireUninterruptibly();
	if (exception[0] != null) throw new CameraInUseException(exception[0].getMessage());
}
 
Example 6
Source File: CircularEncoder.java    From pause-resume-video-recording with Apache License 2.0 6 votes vote down vote up
/**
 * Thread entry point.
 * <p>
 * Prepares the Looper, Handler, and signals anybody watching that we're ready to go.
 */
@Override
public void run() {
    Looper.prepare();
    mHandler = new EncoderHandler(this);    // must create on encoder thread
    Log.d(TAG, "encoder thread ready");
    synchronized (mLock) {
        mReady = true;
        mLock.notify();    // signal waitUntilReady()
    }

    Looper.loop();

    synchronized (mLock) {
        mReady = false;
        mHandler = null;
    }
    Log.d(TAG, "looper quit");
}
 
Example 7
Source File: DispatchQueue.java    From KrGallery with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void run() {
    Looper.prepare();
    handler = new Handler();
    syncLatch.countDown();
    Looper.loop();
}
 
Example 8
Source File: DecodeThread.java    From android-apps with MIT License 5 votes vote down vote up
@Override
public void run() {
  Looper.prepare();
  handler = new DecodeHandler(activity, hints);
  handlerInitLatch.countDown();
  Looper.loop();
}
 
Example 9
Source File: DecodeThread.java    From analyzer-of-android-for-Apache-Weex with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
  Looper.prepare();
  handler = new DecodeHandler(activity, hints);
  handlerInitLatch.countDown();
  Looper.loop();
}
 
Example 10
Source File: DecodeThread.java    From BarcodeEye with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
  Looper.prepare();
  handler = new DecodeHandler(activity, hints);
  handlerInitLatch.countDown();
  Looper.loop();
}
 
Example 11
Source File: UicdInstrumentation.java    From android-uiconductor with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
  Looper.prepare();
  looper = Looper.myLooper();
  startServer();
  Looper.loop();
}
 
Example 12
Source File: DecodeThread.java    From myapplication with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
    Looper.prepare();
    handler = new DecodeHandler(activity, hints);
    handlerInitLatch.countDown();
    Looper.loop();
}
 
Example 13
Source File: MainActivity.java    From AndroidDemo with MIT License 5 votes vote down vote up
@Override
public void run() {
    Log.i("MThread", "启动进程:"+Thread.currentThread().getName());
    Looper.prepare();
    childHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Log.i("MThread", msg.obj+",收到消息线程:"+Thread.currentThread().getName());
            Log.i("MThread", "============================================================");
        }

    };
    Looper.loop();
}
 
Example 14
Source File: DecodeThread.java    From BarcodeScanner with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
	Looper.prepare();
	handler = new DecodeHandler(activity, hints);
	handlerInitLatch.countDown();
	Looper.loop();
}
 
Example 15
Source File: DecodeThread.java    From zxing with MIT License 5 votes vote down vote up
@Override
public void run() {
    Looper.prepare();
    handler = new DecodeHandler(activity, hints);
    handlerInitLatch.countDown();
    Looper.loop();
}
 
Example 16
Source File: ImageLoader.java    From PhotoPicker with Apache License 2.0 4 votes vote down vote up
private void init() {
    initMemoryCache();
    initDiskCache();
    mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            ImageHolder holder = (ImageHolder) msg.obj;
            String path = holder.path;
            ImageView imageView = holder.imageView;
            Bitmap bitmap = holder.bitmap;
            if(imageView == null || bitmap == null) {
                return;
            }
            if (!TextUtils.isEmpty(path) && path.equals(imageView.getTag().toString())) {
                imageView.setImageBitmap(bitmap);
            }
        }
    };

    mPoolThread = new Thread() {
        @Override
        public void run() {
            Looper.prepare();
            mPoolThreadHander = new Handler() {
                @Override
                public void handleMessage(Message msg) {
                    try {
                        mPoolSemaphore.acquire();
                    } catch (InterruptedException e) {
                    }
                    BitmapLoadTask task = getTask();
                    if(task != null) {
                        task.executeOnExecutor(BITMAP_LOAD_EXECUTOR, mWidth, mWidth);
                    }
                }
            };
            // 释放一个信号量,告知mPoolThreadHander对象已经创建完成
            mSemaphore.release();
            Looper.loop();
        }
    };
    mPoolThread.start();

    mTaskQueue = new LinkedList<BitmapLoadTask>();
    mPoolSemaphore = new Semaphore(THREAD_POOL_SIZE);
}
 
Example 17
Source File: MagnetSensor.java    From Cardboard with Apache License 2.0 4 votes vote down vote up
public void run() {
	Process.setThreadPriority(-19);
	Looper.prepare();
	this.mSensorManager.registerListener(this, this.mMagnetometer, 0);
	Looper.loop();
}
 
Example 18
Source File: ServerMain.java    From Fairy with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) {
    Looper.prepareMainLooper();
    new FairyServer("fairy_server").start();
    Looper.loop();
}
 
Example 19
Source File: JsMain.java    From JsDroidCmd with Mozilla Public License 2.0 4 votes vote down vote up
public static void main(String[] args) {
	Looper.prepare();	
	init();
	Looper.loop();
}
 
Example 20
Source File: LoginActivity.java    From pe-protector-moe with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // 初始化界面
    loginAlertDialog = new SweetAlertDialog(this, SweetAlertDialog.PROGRESS_TYPE);
    loginAlertDialog.getProgressHelper().setBarColor(Color.parseColor("#A5DC86"));
    loginAlertDialog.setTitleText("正在登录,请稍候...");
    loginAlertDialog.setCancelable(false);
    // 透明
    setContentView(R.layout.activity_login);
    // 设置标题栏
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    // 读取帐号密码
    EditText ed_username = findViewById(R.id.ed_username);
    EditText ed_pwd = findViewById(R.id.ed_pwd);
    SharedPreferences preferences = getSharedPreferences("login", MODE_PRIVATE);
    ed_username.setText(preferences.getString("username", ""));
    ed_pwd.setText(preferences.getString("pwd", ""));

    // 设置按钮事件
    Button actionButton = findViewById(R.id.bt_login);
    actionButton.setOnClickListener((v -> firstLogin()));
    // 设置服务器
    NiceSpinner sp_server = findViewById(R.id.sp_server);
    sp_server.attachDataSource(Arrays.asList("安卓", "IOS", "台服", "国际"));
    sp_server.setSelectedIndex(preferences.getInt("server", 0));
    // 连接更新服务器
    final CheckVersionCallBack callBack = new CheckVersionCallBack() {
        @Override
        public void onFinish() {
        }

        @Override
        public void onUpgrade(String newVersion, String newData) {
            if (Config.hasLogin) {
                return;
            }
            Looper.prepare();
            new SweetAlertDialog(LoginActivity.this, SweetAlertDialog.WARNING_TYPE)
                    .setTitleText("发现新版本")
                    .setContentText(String.format("新版本:%s\n更新日志:\n%s", newVersion, newData))
                    .setConfirmText("去下载")
                    .setCancelText("下次再说")
                    .setCancelClickListener(SweetAlertDialog::cancel)
                    .setConfirmClickListener((sweetAlertDialog) -> {
                        Uri uri = Uri.parse("https://github.com/ProtectorMoe/pe-protector-moe/releases");
                        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                        startActivity(intent);
                        sweetAlertDialog.cancel();
                        setResult(RESULT_CANCELED);
                        finish();
                    })
                    .show();
            Looper.loop();
        }

        @Override
        public void onError(String errMsg) {
            if (Config.hasLogin) {
                return;
            }
            Looper.prepare();
            new SweetAlertDialog(LoginActivity.this, SweetAlertDialog.ERROR_TYPE)
                    .setTitleText("错误")
                    .setContentText("连接更新服务器失败")
                    .setConfirmText("确定")
                    .setConfirmClickListener(SweetAlertDialog::cancel)
                    .show();
            Looper.loop();
        }
    };

    // 检测软件更新
    new Thread(() -> {
        try {
            PackageManager manager = getApplicationContext().getPackageManager();
            PackageInfo info = manager.getPackageInfo(getApplicationContext().getPackageName(), 0);
            int versionCode = info.versionCode;
            CheckVersionBean bean = NetSender.getInstance().checkVersion();
            if (bean.versionCode > versionCode) {
                callBack.onUpgrade(bean.versionName, bean.history.get(String.valueOf(bean.versionCode)));
            } else {
                callBack.onFinish();
            }
        } catch (Exception e) {
            callBack.onError(e.getMessage() + e.getLocalizedMessage());
        }
    }).start();
}