当前位置:首页 » 安卓系统 » androidrunnable

androidrunnable

发布时间: 2022-09-21 03:25:44

① android的runnable里可以写toast么

Android中提供一种简单的Toast消息提示框机制,可以在用户点击了某些按钮后,提示用户一些信息,提示的信息不能被用户点击,Toast的提示信息根据用户设置的显示时间后自动消失。Toast的提示信息可以在调试程序的时候方便的显示某些想显示的东西。 两种方法创建Toast 第一种方法的java代码: makeText(Context context, int resId, int ration) 参数:context是toast显示在哪个上下文,通常是当前Activity;resId指显示内容引用Resouce那条数据,就是从R类中去指定显示的消息内容;ration指定显示时间,Toast默认有LENGTH_SHORT和LENGTH_LONG两常量,分别表示短时间显示和长时间显示。 第二种方法的Java代码: makeText(Context context, CharSequence text, int ration) 参数context和ration与第一个方法相同,参数text可以自己写消息内容。 用上面任意方法创建Toast对象之后调用方法show()即可显示。 Java代码: Toast toast = Toast.makeText(ToastDemoActivity.this, "这是一个普通的Toast!", Toast.LENGTH_SHORT); toast.show();

② Android中为什么可以new Runnable

类似Thread类,重载run方法。
Runnable mRunnable = new Runnable() {

public void run() {

//自定义功能
}

};

然后一般通过Handler,发送到消息队列里面去运行,
如 Handler mHandler = new Handler();
mHandler.post(mRunnable);
mHandler.postDelayed (mRunnable);
一些原型如下:

public final boolean post(Runnable r)
{
return sendMessageDelayed(getPostMessage(r), 0);
}

public final boolean postDelayed(Runnable r, long delayMillis)
{
return sendMessageDelayed(getPostMessage(r), delayMillis);
}

public final boolean postAtTime(Runnable r, Object token, long uptimeMillis)
{
return sendMessageAtTime(getPostMessage(r, token), uptimeMillis);
}

③ Android Runnable运行在哪个线程

Android中的Runnable并不一定是新开的线程,比如下面调用的方法就是运行在UI主线程中

Hanlder handler = new Handler();

handler.post(new Runnable(){

public void run(){

}

});

官方文档对此的解释是:

The runnable will be run on the user interface thread. ”

boolean android.view.View .post(Runnable action)

Causes the Runnable to be added to the message queue. The runnable will be run on the user interface thread.

Parameters:
action The Runnable that will be executed.
Returns:
Returns true if the Runnable was successfully placed in to the message queue. Returns false on failure, usually because the looper processing the message queue is exiting.

我们可以通过handler的对象的post方法,把Runnable对象(一般是Runnable的子类)传过去,handler会在Looper中调用Runnable的run方法执行,Runnable是一个接口,不是一个线程,一般线程会实现Runnable接口

这里我们看代码handler.post(new Runnable(){好像是new了一个interface,其实是new一个实现Runnable的匿名内部类(Inner Anoymous Class)}) 这是一个简练的方法

Runnalbe是一个接口,不是一个线程,一般线程会实现Runnalbe接口,所以如果我们使用匿名内部类是运行在UI主线程的,如果我们使用实现这个Runnable接口的线程类,则是运行在对应的线程的。

具体来说这个函数的工作原理如下:

View.post(Runnalbe)方法,在post(Runanble action)方法中,View获得当前主线程(即UI线程)的handler,然后将action对象post到handler里面去,在Handler里,它将传递过来的action对象封装成一个Message(Message 的callback为action),然后将其投入到UI线程的消息循环中,在handler再次处理该Message时,有一条分支(未解释的那条)就是为它所设,直接调用runnable的run方法,而此时,已经路由到UI线程里,因此我们可以毫无顾虑来更新UI。

如下图,前面看到的代码,我们这里的Message的callback为一个Runnalbe的匿名内部类,这种情况下,由于不是在新的线程中使用,所以千万别做复杂的计算逻辑。

④ android中怎么用runnable

类似Thread类,重载run方法。
Runnable mRunnable = new Runnable() {

public void run() {

//自定义功能
}

};

然后一般通过Handler,发送到消息队列里面去运行,
如 Handler mHandler = new Handler();
mHandler.post(mRunnable);
mHandler.postDelayed (mRunnable);
一些原型如下:

public final boolean post(Runnable r)
{
return sendMessageDelayed(getPostMessage(r), 0);
}

public final boolean postDelayed(Runnable r, long delayMillis)
{
return sendMessageDelayed(getPostMessage(r), delayMillis);
}

public final boolean postAtTime(Runnable r, Object token, long uptimeMillis)
{
return sendMessageAtTime(getPostMessage(r, token), uptimeMillis);
}

⑤ Android: Runnable 中 getResources 崩溃

方法一:重写此类的构造方法,在其他页面调用的时候,传入Context,直接利用
Context.getResources()
方法二:通过全局的application来获取
//QDApplicationContext为自定义的application,getInstance()用来获取实例对象
QDApplicationContext.getInstance().getResources()

方法三:不用重写构造方法,直接在你需要调用的方法里面传入Context即可,如:
public void setSomething(Context con){
String a = con.getResources().getString(R.String.activity_name);
...
}

⑥ android 怎么判断一个runnable是否被handler使用

/**
* Check if there are any pending posts of messages with callback r in
* the message queue.
*/
public final boolean hasCallbacks(@NonNull Runnable r) {
return mQueue.hasMessages(this, r, null);
}

⑦ android中Runnable的用法

只要java好就没有问题,这是3D魔方android的源码
package com.example.android.image3D;

import android.view.SurfaceView;
import android.view.SurfaceHolder;
import android.content.Context;
import android.util.AttributeSet;
import java.util.ArrayList;
import java.util.concurrent.Semaphore;

import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGL11;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLContext;
import javax.microedition.khronos.egl.EGLDisplay;
import javax.microedition.khronos.egl.EGLSurface;
import javax.microedition.khronos.opengles.GL;
import javax.microedition.khronos.opengles.GL10;

public class View3D extends SurfaceView implements SurfaceHolder.Callback {

private static final Semaphore sEglSemaphore = new Semaphore(1);
private boolean mSizeChanged = true;

private SurfaceHolder mHolder;
private GLThread mGLThread;
private GLWrapper mGLWrapper;

public View3D(Context context) {
super(context);
init();
}

public View3D(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}

private void init() {
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_GPU);
}

public SurfaceHolder getSurfaceHolder() {
return mHolder;
}

public void setGLWrapper(GLWrapper glWrapper) {
mGLWrapper = glWrapper;
}

public void setRenderer(Renderer renderer) {
mGLThread = new GLThread(renderer);
mGLThread.start();
}

public void surfaceCreated(SurfaceHolder holder) {
mGLThread.surfaceCreated();
}

public void surfaceDestroyed(SurfaceHolder holder) {
mGLThread.surfaceDestroyed();
}

public void surfaceChanged(SurfaceHolder holder,
int format, int w, int h) {
mGLThread.onWindowResize(w, h);
}

public void onPause() {
mGLThread.onPause();
}

public void onResume() {
mGLThread.onResume();
}

@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
mGLThread.onWindowFocusChanged(hasFocus);
}

public void queueEvent(Runnable r) {
mGLThread.queueEvent(r);
}

@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
mGLThread.requestExitAndWait();
}

public interface GLWrapper {
GL wrap(GL gl);
}

public interface Renderer {

int[] getConfigSpec();

void surfaceCreated(GL10 gl);
void sizeChanged(GL10 gl, int width, int height);
void drawFrame(GL10 gl);
}

private class EglHelper {

EGL10 mEgl;
EGLDisplay mEglDisplay;
EGLSurface mEglSurface;
EGLConfig mEglConfig;
EGLContext mEglContext;

public EglHelper() {

}

public void start(int[] configSpec){

mEgl = (EGL10) EGLContext.getEGL();
mEglDisplay = mEgl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
int[] version = new int[2];
mEgl.eglInitialize(mEglDisplay, version);

EGLConfig[] configs = new EGLConfig[1];
int[] num_config = new int[1];
mEgl.eglChooseConfig(mEglDisplay, configSpec, configs, 1,
num_config);
mEglConfig = configs[0];

mEglContext = mEgl.eglCreateContext(mEglDisplay, mEglConfig,
EGL10.EGL_NO_CONTEXT, null);

mEglSurface = null;
}

public GL createSurface(SurfaceHolder holder) {

if (mEglSurface != null) {

mEgl.eglMakeCurrent(mEglDisplay, EGL10.EGL_NO_SURFACE,
EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_CONTEXT);
mEgl.eglDestroySurface(mEglDisplay, mEglSurface);
}

mEglSurface = mEgl.eglCreateWindowSurface(mEglDisplay,
mEglConfig, holder, null);

mEgl.eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
mEglContext);

GL gl = mEglContext.getGL();
if (mGLWrapper != null) {
gl = mGLWrapper.wrap(gl);
}
return gl;
}

public boolean swap() {
mEgl.eglSwapBuffers(mEglDisplay, mEglSurface);
return mEgl.eglGetError() != EGL11.EGL_CONTEXT_LOST;
}

public void finish() {
if (mEglSurface != null) {
mEgl.eglMakeCurrent(mEglDisplay, EGL10.EGL_NO_SURFACE,
EGL10.EGL_NO_SURFACE,
EGL10.EGL_NO_CONTEXT);
mEgl.eglDestroySurface(mEglDisplay, mEglSurface);
mEglSurface = null;
}
if (mEglContext != null) {
mEgl.eglDestroyContext(mEglDisplay, mEglContext);
mEglContext = null;
}
if (mEglDisplay != null) {
mEgl.eglTerminate(mEglDisplay);
mEglDisplay = null;
}
}

}

class GLThread extends Thread {

private boolean mDone;
private boolean mPaused;
private boolean mHasFocus;
private boolean mHasSurface;
private boolean mContextLost;
private int mWidth;
private int mHeight;
private Renderer mRenderer;
private ArrayList<Runnable>
mEventQueue = new ArrayList<Runnable>();
private EglHelper mEglHelper;

GLThread(Renderer renderer) {
super();
mDone = false;
mWidth = 0;
mHeight = 0;
mRenderer = renderer;
setName("GLThread");
}

@Override
public void run() {

try {
try {
sEglSemaphore.acquire();
} catch (InterruptedException e) {
return;
}
guardedRun();
} catch (InterruptedException e) {

} finally {
sEglSemaphore.release();
}
}

private void guardedRun() throws InterruptedException {
mEglHelper = new EglHelper();
int[] configSpec = mRenderer.getConfigSpec();
mEglHelper.start(configSpec);

GL10 gl = null;
boolean tellRendererSurfaceCreated = true;
boolean tellRendererSurfaceChanged = true;

while (!mDone) {

int w, h;
boolean changed;
boolean needStart = false;
synchronized (this) {
Runnable r;
while ((r = getEvent()) != null) {
r.run();
}
if (mPaused) {
mEglHelper.finish();
needStart = true;
}
if(needToWait()) {
while (needToWait()) {
wait();
}
}
if (mDone) {
break;
}
changed = mSizeChanged;
w = mWidth;
h = mHeight;
mSizeChanged = false;
}
if (needStart) {
mEglHelper.start(configSpec);
tellRendererSurfaceCreated = true;
changed = true;
}
if (changed) {
gl = (GL10) mEglHelper.createSurface(mHolder);
tellRendererSurfaceChanged = true;
}
if (tellRendererSurfaceCreated) {
mRenderer.surfaceCreated(gl);
tellRendererSurfaceCreated = false;
}
if (tellRendererSurfaceChanged) {
mRenderer.sizeChanged(gl, w, h);
tellRendererSurfaceChanged = false;
}
if ((w > 0) && (h > 0)) {

mRenderer.drawFrame(gl);
mEglHelper.swap();
}
}
mEglHelper.finish();
}

private boolean needToWait() {
return (mPaused || (! mHasFocus) || (! mHasSurface) || mContextLost)
&& (! mDone);
}

public void surfaceCreated() {
synchronized(this) {
mHasSurface = true;
mContextLost = false;
notify();
}
}

public void surfaceDestroyed() {
synchronized(this) {
mHasSurface = false;
notify();
}
}

public void onPause() {
synchronized (this) {
mPaused = true;
}
}

public void onResume() {
synchronized (this) {
mPaused = false;
notify();
}
}

public void onWindowFocusChanged(boolean hasFocus) {
synchronized (this) {
mHasFocus = hasFocus;
if (mHasFocus == true) {
notify();
}
}
}

public void onWindowResize(int w, int h) {
synchronized (this) {
mWidth = w;
mHeight = h;
mSizeChanged = true;
}
}

public void requestExitAndWait() {
synchronized(this) {
mDone = true;
notify();
}
try {
join();
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}

public void queueEvent(Runnable r) {
synchronized(this) {
mEventQueue.add(r);
}
}

private Runnable getEvent() {
synchronized(this) {
if (mEventQueue.size() > 0) {
return mEventQueue.remove(0);
}

}
return null;
}
}
}

⑧ 怎么判断android里一个runnable是否在运行

th = new Thread(this);
if (!th.isAlive()) { // 假如线程不存在 线程清空running=false
if (th != null) {

}

⑨ android Thread和Runnable的区别

Thread和Runnable都是java里面的东西,并非android特有
1,区别,Thread是一个类,它实现了在另一个线程上执行一段代码的功能;Runnable是一个接口
2,联系:Thread实现了Runnable接口
3,Runnable在android中独有的用法:可以给handler post 一个runnable对象,指定一个delay时间来执行。

热点内容
网络登录服务器需要获取什么信息 发布:2025-01-12 12:17:32 浏览:890
mac终端打开文件夹 发布:2025-01-12 12:17:31 浏览:295
第一次安装如何设置mysql密码 发布:2025-01-12 12:09:02 浏览:280
如何删除微信服务器上收藏 发布:2025-01-12 12:08:20 浏览:102
吃鸡游戏安卓区转苹果区怎么转 发布:2025-01-12 11:34:00 浏览:880
网页版c语言 发布:2025-01-12 11:21:01 浏览:864
安卓怎么更改排位常用英雄 发布:2025-01-12 11:10:33 浏览:561
拆迁的100万如何配置 发布:2025-01-12 11:08:52 浏览:575
如何配置ph值为次氯酸钠的ph值 发布:2025-01-12 11:08:52 浏览:437
pythonarraynumpy 发布:2025-01-12 11:01:47 浏览:293