下面列出了android.os.Looper#loop ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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;
}
}
/**
* 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");
}
/**
* 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());
}
/**
* 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;
}
}
/**
* 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;
}
}
@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();
}
@Override
public void run() {
Looper.prepare();
handler = new DecodeHandler(activity, hints);
handlerInitLatch.countDown();
Looper.loop();
}
@Override
public void run() {
Looper.prepare();
handler = new Handler();
syncLatch.countDown();
Looper.loop();
}
@Override
public void run() {
Looper.prepare();
handler = new DecodeHandler(activity, hints);
handlerInitLatch.countDown();
Looper.loop();
}
@Override
public void run() {
Looper.prepare();
handler = new DecodeHandler(activity, hints);
handlerInitLatch.countDown();
Looper.loop();
}
@Override
public void run() {
Looper.prepare();
looper = Looper.myLooper();
startServer();
Looper.loop();
}
@Override
public void run() {
Looper.prepare();
handler = new DecodeHandler(activity, hints);
handlerInitLatch.countDown();
Looper.loop();
}
@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();
}
@Override
public void run() {
Looper.prepare();
handler = new DecodeHandler(activity, hints);
handlerInitLatch.countDown();
Looper.loop();
}
@Override
public void run() {
Looper.prepare();
handler = new DecodeHandler(activity, hints);
handlerInitLatch.countDown();
Looper.loop();
}
@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();
}
public void run() {
Process.setThreadPriority(-19);
Looper.prepare();
this.mSensorManager.registerListener(this, this.mMagnetometer, 0);
Looper.loop();
}
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);
}
public static void main(String[] args) {
Looper.prepareMainLooper();
new FairyServer("fairy_server").start();
Looper.loop();
}
public static void main(String[] args) {
Looper.prepare();
init();
Looper.loop();
}