Example usage for android.os Handler Handler

List of usage examples for android.os Handler Handler

Introduction

In this page you can find the example usage for android.os Handler Handler.

Prototype

@UnsupportedAppUsage
public Handler(boolean async) 

Source Link

Document

Use the Looper for the current thread and set whether the handler should be asynchronous.

Usage

From source file:Main.java

/**
 * Long toast message//  w w w .j a  v a  2  s.  c o  m
 * (Predefined in AOS to 3500 ms = 3.5 sec)
 *
 * @param context Application Context
 * @param msg     Message to send
 */
public static void msgLong(final Context context, final String msg) {
    if (context != null && msg != null) {
        new Handler(context.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(context, msg.trim(), Toast.LENGTH_LONG).show();
            }
        });
    }
}

From source file:Main.java

/**
 * Short toast message// w ww . j  ava  2 s  . co  m
 * (Predefined in AOS to 2000 ms = 2 sec)
 *
 * @param context Application Context
 * @param msg     Message to send
 */
public static void msgShort(final Context context, final String msg) {
    if (context != null && msg != null) {
        new Handler(context.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(context, msg.trim(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}

From source file:com.doplgangr.secrecy.Util.java

public static void alert(final Context context, final String title, final String message,
        final DialogInterface.OnClickListener positive, final DialogInterface.OnClickListener negative) {
    new Handler(Looper.getMainLooper()).post(new Runnable() {

        @Override/*w w  w .  j a  va2s .  com*/
        public void run() {
            AlertDialog.Builder a = new AlertDialog.Builder(context);
            if (title != null)
                a.setTitle(title);
            if (message != null)
                a.setMessage(message);
            if (positive != null)
                a.setPositiveButton(context.getString(R.string.OK), positive);
            if (negative != null)
                a.setNegativeButton(context.getString(R.string.CANCEL), negative);
            a.setCancelable(false);
            a.show();
        }

    });
}

From source file:com.doplgangr.secrecy.utils.Util.java

public static void alert(final Context context, final String title, final String message,
        final DialogInterface.OnClickListener positive, final DialogInterface.OnClickListener negative) {
    new Handler(Looper.getMainLooper()).post(new Runnable() {

        @Override/*  ww  w .j av  a  2 s .co m*/
        public void run() {
            AlertDialog.Builder a = new AlertDialog.Builder(context);
            if (title != null)
                a.setTitle(title);
            if (message != null)
                a.setMessage(message);
            if (positive != null)
                a.setPositiveButton("OK", positive);
            if (negative != null)
                a.setNegativeButton("CANCEL", negative);
            a.setCancelable(false);
            a.show();
        }

    });
}

From source file:com.browsertophone.ShareLink.java

private ShareLink(Context context) {
    mContext = context;
    mHandler = new Handler(this);
}

From source file:com.nolanlawson.cordova.sqlite.SQLitePlugin.java

private Handler createBackgroundHandler() {
    HandlerThread thread = new HandlerThread("SQLitePlugin BG Thread");
    thread.start();// w w w . j a va2s . co m
    return new Handler(thread.getLooper());
}

From source file:com.android.volley.toolbox.ClearCacheRequest.java

@Override
public boolean isCanceled() {
    // This is a little bit of a hack, but hey, why not.
    mCache.clear();//from   w  ww .  j a  va2s.c o m
    if (mCallback != null) {
        Handler handler = new Handler(Looper.getMainLooper());
        handler.postAtFrontOfQueue(mCallback);
    }
    return true;
}

From source file:com.doplgangr.secrecy.utils.Util.java

public static void alert(final Context context, final String title, final String message,
        final DialogInterface.OnClickListener ok) {
    new Handler(Looper.getMainLooper()).post(new Runnable() {

        @Override//w w w  .j a v a2  s . c  o  m
        public void run() {
            AlertDialog.Builder a = new AlertDialog.Builder(context);
            if (title != null)
                a.setTitle(title);
            if (message != null)
                a.setMessage(message);
            if (ok != null)
                a.setPositiveButton("OK", ok);
            a.setCancelable(false);
            a.show();
        }

    });
}

From source file:me.willowcheng.makerthings.util.MjpegStreamer.java

public MjpegStreamer(String sourceUrl, String username, String password, Context ctx) {
    mSourceUrl = sourceUrl;/* www.j  a  v  a 2  s.com*/
    mUsername = username;
    mPassword = password;
    mCtx = ctx;
    mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            Bitmap bmp = (Bitmap) msg.obj;
            if (mTargetImageView != null)
                mTargetImageView.setImageBitmap(bmp);
            return false;
        }
    });
}

From source file:com.dragondevs.pubnubexample.DrawingActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = getIntent();//  w ww. j a v a2  s.  c  o m
    channelName = intent.getStringExtra("CHANNEL");

    //Create Handler to update screen from PubnubClient
    mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message inputMessage) {
            if (inputMessage.obj instanceof JSONObject) {
                JSONObject json = (JSONObject) inputMessage.obj;
                int x = -1;
                int y = -1;
                int state = -1;
                try {
                    x = json.getInt("X");
                    y = json.getInt("Y");
                    state = json.getInt("STATE");
                } catch (JSONException e) {
                    Log.d(TAG, "Error: " + e.toString());
                }
                Log.d(TAG, "Drawing coord at x, y, state: " + x + " " + y + " " + state);
                if (x >= 0 && y >= 0 && state >= 0)
                    myView.drawLine(x, y, state);
                else
                    Log.d(TAG, "Json values read was not valid");
            }
        }
    };

    myView = new MyView(this, mHandler);

    setContentView(myView);

    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setDither(true);
    mPaint.setColor(0xFFFF0000);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(12);

}