androidrunnable
① 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時間來執行。