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

public Handler() 

Source Link

Document

Default constructor associates this handler with the Looper for the current thread.

Usage

From source file:com.klinker.android.dream.DreamSceneService.java

@Override
public void onAttachedToWindow() {
    super.onAttachedToWindow();

    // add initial options
    setInteractive(false);/*from   ww  w. j a v a 2 s .c o m*/
    setFullscreen(true);
    setScreenBright(true);

    // show the view on the screen
    setContentView(R.layout.daydream_service);

    // set up the background image
    background = (ImageView) findViewById(R.id.imageView);

    // set the initial background
    handler = new Handler();
    initBackgrounds();
}

From source file:com.dedipower.portal.android.ManagedServerLanding.java

public void onCreate(Bundle savedInstanceState) {
    API.SessionID = getIntent().getStringExtra("sessionid");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.managedserverlanding);
    //temp// w  ww.j a va  2s . c  o  m
    list = (ListView) findViewById(R.id.ManagedServerList);
    final ProgressDialog dialog = ProgressDialog.show(this, "DediPortal", "Please wait: loading data....",
            true);
    final Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            dialog.dismiss();
            if (Success.equals("true")) {
                UpdateErrorMessage(ErrorMessage);
                ManagedServerAdaptor adapter = new ManagedServerAdaptor(ManagedServerLanding.this,
                        listOfManagedServers, API.SessionID);
                list.setAdapter(adapter);
            } else {
                UpdateErrorMessage(ErrorMessage);
            }
        }
    };

    Thread dataPreload = new Thread() {
        int ServerCount = 0;

        public void run() {
            try {
                Servers = API.PortalQuery("servers", "none");
                Success = Servers.getString("success");
            } catch (JSONException e) {
                //UpdateErrorMessage("An unrecoverable JSON Exception occured.");
                ErrorMessage = "An unrecoverable JSON Exception occured.";
            }

            if (Success.equals("false")) {
                try {
                    //UpdateErrorMessage(Servers.getString("msg"));
                    ErrorMessage = Servers.getString("msg");
                } catch (JSONException e) {
                    //UpdateErrorMessage("An unrecoverable JSON Exception occured.");
                    ErrorMessage = "An unrecoverable JSON Exception occured.";
                }
            } else {
                Log.i("APIFuncs", Servers.toString());
                try {
                    ManagedServers = Servers.getJSONArray("servers");
                    //UpdateErrorMessage("");
                    ErrorMessage = "";
                } catch (JSONException e) {
                    //UpdateErrorMessage("There are no Managed servers in your account.");
                    ErrorMessage = "There are no Managed servers in your account.";
                }

                try {
                    ServerCount = ManagedServers.length();
                } catch (Exception e) {
                    ServerCount = 0;
                }

                if (ServerCount == 0) {
                    ErrorMessage = "There are no Managed servers for your account.";
                    handler.sendEmptyMessage(0);
                    return;
                }

                for (int i = 0; i < ServerCount; i++) {

                    try {
                        CurrentServer = ManagedServers.getJSONObject(i).getJSONObject("Server");
                    } catch (JSONException e1) {
                        Log.e("APIFuncs", e1.getMessage());
                    }

                    //Log.e("APIFuncs",CurrentServer.toString(3));
                    listOfManagedServers.add(new ManagedServer(GetString("ip"), GetString("servercode"),
                            GetString("description"), GetString("facility"), GetInt("bandwidth"),
                            GetString("bandwidthTotal"), GetBool("monitored"), GetInt("transferlimit"),
                            GetString("software"), GetString("state"), GetBool("managedbackupenabled")));
                }
            }
            handler.sendEmptyMessage(0);
        }
    };

    dataPreload.start();
    //temp
    /*try 
    {
      Servers = API.PortalQuery("servers", "none");
      Success = Servers.getString("success");
    } 
    catch (JSONException e) 
    {
       UpdateErrorMessage("An unrecoverable JSON Exception occured.");
    }
            
    if(Success == "false")
    {
       try 
       {
    UpdateErrorMessage(Servers.getString("msg"));
      } 
       catch (JSONException e) 
       {
     UpdateErrorMessage("An unrecoverable JSON Exception occured.");
      }
    }
    else
    {
       Log.i("APIFuncs",Servers.toString());
       try
        {
     ManagedServers = Servers.getJSONArray("servers");
      UpdateErrorMessage("");
        }
        catch (JSONException e) 
        {
      UpdateErrorMessage("There are no Managed servers in your account.");
        }
                
        //OK lets actually do something useful
        ListView list = (ListView)findViewById(R.id.ManagedServerList);
        List<ManagedServer> listOfManagedServers = new ArrayList<ManagedServer>();
        int ServerCount = ManagedServers.length();
        for(int i = 0; i < ServerCount; i++)
        {
              
    try 
    {
       CurrentServer = ManagedServers.getJSONObject(i).getJSONObject("Server");
    } 
    catch (JSONException e1) 
    {
       Log.e("APIFuncs",e1.getMessage());
    }
            
      //Log.e("APIFuncs",CurrentServer.toString(3));
      listOfManagedServers.add(new ManagedServer(GetString("ip"),
            GetString("servercode"),
            GetString("description"),
            GetString("facility"),
            GetInt("bandwidth"),
            GetString("bandwidthTotal"),
            GetBool("monitored"),
            GetInt("transferlimit"),
            GetString("software"),
            GetString("state"),
            GetBool("managedbackupenabled")));
        }
                
        ManagedServerAdaptor adapter = new ManagedServerAdaptor(this, listOfManagedServers, API.SessionID);
                
        list.setAdapter(adapter);
    }*/

}

From source file:gxu.software_engineering.market.android.service.SyncService.java

@Override
public void onCreate() {
    super.onCreate();
    app = MarketApp.marketApp();
    handler = new Handler();
}

From source file:com.noah.lol.network.RequestNetwork.java

protected void asyncRequestGet(final String url, final NetworkListener<String> listener) {

    if (url == null) {
        return;/* w w w.j  av  a  2s. com*/
    }

    try {
        environmentCheck();
    } catch (EnvironmentException e) {
        if (listener != null) {
            e.setStatus(BAD_REQUEST_ENVIRONMENT_CONFIG);
            listener.onNetworkFail(BAD_REQUEST_ENVIRONMENT_CONFIG, e);
        }
        return;
    }

    final Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            Bundle bundle = msg.getData();
            switch (msg.what) {
            case REQUEST_SUCCESS:
                String responseBody = bundle.getString(RESPONSE_KEY);

                if (listener != null && responseBody != null) {
                    listener.onSuccess(responseBody);
                }
                break;
            case REQUEST_FAIL:
                NetworkException e = (NetworkException) bundle.getSerializable(EXCEPTION_KEY);
                if (listener != null) {
                    listener.onNetworkFail(e.getStatus(), e);
                }
                break;
            }
        }
    };

    new Thread(new Runnable() {

        @Override
        public void run() {

            String responseBody = null;
            Message message = new Message();

            try {
                responseBody = syncRequestGet(url);
                Bundle bundle = new Bundle();
                bundle.putString(RESPONSE_KEY, responseBody);
                message = Message.obtain(handler, REQUEST_SUCCESS);
                message.setData(bundle);
                handler.sendMessage(message);
            } catch (NetworkException e) {
                Bundle bundle = new Bundle();
                bundle.putSerializable(EXCEPTION_KEY, e);
                message = Message.obtain(handler, REQUEST_FAIL);
                message.setData(bundle);
                handler.sendMessage(message);
                e.printStackTrace();
            }

        }
    }).start();

}

From source file:org.jnegre.android.osmonthego.service.ExportService.java

public ExportService() {
    super("ExportService");
    this.handler = new Handler();
}

From source file:net.noviden.android.shoutout.MainScreen.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_screen);

    mHandler = new Handler();
    mJsonPostsArray = null;/*from www .j  a  va 2  s. co m*/
    mStringPostsArray = null;

    refreshPosts();

    startRepeatingTask();

}

From source file:com.pusher.PusherModule.java

@Kroll.method(runOnUiThread = true)
public void connect() {
    mHandler = new Handler() {
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            Bundle bundleData = msg.getData();
            if (bundleData.getString("type").contentEquals("pusher")) {
                try {
                    JSONObject message = new JSONObject(bundleData.getString("message"));
                    Log.d("Pusher Message", message.toString());

                    if (message.getString("event").equals("connection_established")) {
                        PusherModule.this.fireEvent("connected", null);
                    }//w w w.  ja v  a2s.c o  m

                    if (PusherModule.this.hasListeners(message.getString("event"))) {
                        KrollDict event = new KrollDict();
                        event.put("name", message.getString("event"));

                        JSONObject data = new JSONObject(message.getString("data"));
                        event.put("data", KrollDict.fromJSON(data));

                        PusherModule.this.fireEvent(message.getString("event"), event);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            if (bundleData.getString("type").contentEquals("pusher:disconnected")) {
                PusherModule.this.fireEvent("disconnected", null);
            }
        }
    };

    mPusher = new Pusher(mReconnectAutomatically, mReconnectDelay);
    mPusher.addHandler(mHandler);
    mPusher.connect(mPusherKey);
}

From source file:com.SmartDial.ForegroundService.java

/**
 * Put the service in a foreground state to prevent app from being killed
 * by the OS./*  www .j  a  v  a 2  s  .  co m*/
 */
public void keepAwake() {
    final Handler handler = new Handler();

    if (!this.inSilentMode()) {
        startForeground(NOTIFICATION_ID, makeNotification());
    } else {
        Log.w("BackgroundMode", "In silent mode app may be paused by OS!");
    }

    BackgroundMode.deleteUpdateSettings();

    keepAliveTask = new TimerTask() {
        @Override
        public void run() {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    // Nothing to do here
                    // Log.d("BackgroundMode", "" + new Date().getTime());
                }
            });
        }
    };

    scheduler.schedule(keepAliveTask, 0, 1000);
}

From source file:it.fdev.utils.DrawableManager.java

public void fetchDrawableOnThread(final String urlString, final DrawableManagerListener listener) {
    if (drawableMap.containsKey(urlString)) {
        listener.onLoadingComplete(drawableMap.get(urlString));
    }//from w w w  .  ja v  a2  s. com
    final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message message) {
            listener.onLoadingComplete((Drawable) message.obj);
        }
    };
    final Handler errorHandler = new Handler() {
        @Override
        public void handleMessage(Message message) {
            listener.onLoadingError();
        }
    };
    Thread thread = new Thread() {
        @Override
        public void run() {
            //TODO : set imageView to a "pending" image
            Drawable drawable = fetchDrawable(urlString);
            if (drawable != null) {
                Message message = handler.obtainMessage(1, drawable);
                handler.sendMessage(message);
            } else {
                Message message = new Message();
                errorHandler.sendMessage(message);
            }
        }
    };
    thread.start();
}

From source file:com.dedipower.portal.android.ViewTicket.java

public void onCreate(Bundle savedInstanceState) {
    API.SessionID = getIntent().getStringExtra("sessionid");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.viewticket);
    list = (ListView) findViewById(R.id.TicketUpdatesList);

    final ProgressDialog dialog = ProgressDialog.show(this, "DediPortal", "Please wait: loading data....",
            true);/*  w w  w  .  j a v a2 s  .  c o  m*/
    final Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            dialog.dismiss();
            if (Success.equals("true")) {
                UpdateErrorMessage(ErrorMessage);
                TicketUpdatesAdaptor adapter = new TicketUpdatesAdaptor(ViewTicket.this, listOfUpdates,
                        API.SessionID);
                list.setAdapter(adapter);
            } else {
                UpdateErrorMessage(ErrorMessage);
            }
        }
    };

    Thread dataPreload = new Thread() {
        public void run() {
            try {
                TicketAPI = API.PortalQuery("updates", getIntent().getStringExtra("ticketID"));
                Success = TicketAPI.getString("success");
            } catch (JSONException e) {
                ErrorMessage = "An unrecoverable JSON Exception occured. Error Code: 1";
                Log.e("APIFuncs", e.getMessage());
            }

            if (Success == "false") {
                try {
                    Log.i("APIFuncs", TicketAPI.toString(3));
                    ErrorMessage = TicketAPI.getString("msg");
                } catch (JSONException e) {
                    Log.e("APIFuncs", e.getMessage());
                }
            } else {
                Log.i("APIFuncs", TicketAPI.toString());
                try {
                    Updates = TicketAPI.getJSONArray("updates");
                    ErrorMessage = "";
                } catch (JSONException e) {
                    ErrorMessage = "There are no updates for this ticket. (Strange!)";
                }

                int TicketCount = Updates.length();

                if (TicketCount == 0) {
                    ErrorMessage = "There are no updates for this ticket.";
                    return;
                }

                for (int i = 0; i < TicketCount; i++) {
                    try {
                        CurrentTicket = Updates.getJSONObject(i);

                    } catch (JSONException e1) {
                        Log.e("APIFuncs", e1.getMessage());
                    }

                    if (CurrentTicket != null) {
                        listOfUpdates.add(new TicketUpdates(GetString("type"), GetString("name"),
                                GetInt("time"), GetString("content")));
                    }
                }
                handler.sendEmptyMessage(0);
            }

        }
    };

    dataPreload.start();

    Button AddReply = (Button) findViewById(R.id.AddReplyButton);
    AddReply.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent CreateTicketReplyIntent = new Intent(ViewTicket.this, AddTicketReply.class);
            CreateTicketReplyIntent.putExtra("sessionid", API.SessionID);
            CreateTicketReplyIntent.putExtra("ticketid", getIntent().getStringExtra("ticketID"));
            ViewTicket.this.startActivity(CreateTicketReplyIntent);
        }
    });

    /*
    try 
    {
       TicketAPI = API.PortalQuery("updates", getIntent().getStringExtra("ticketID"));
      Success = TicketAPI.getString("success");   
    } 
    catch (JSONException e) 
    {
       UpdateErrorMessage("An unrecoverable JSON Exception occured. Error Code: 1");
       Log.e("APIFuncs",e.getMessage());
       //Toast.makeText(ColoLanding.this, "An unrecoverable JSON Exception occured.", Toast.LENGTH_LONG).show();
    }
            
    if(Success == "false")
    {
       try 
       {
     Log.i("APIFuncs",TicketAPI.toString(3));
    UpdateErrorMessage(TicketAPI.getString("msg"));
      } 
       catch (JSONException e) 
       {
     Log.e("APIFuncs",e.getMessage());
     UpdateErrorMessage("An unrecoverable JSON Exception occured.  Error Code: 2");
      }
    }
    else
    {
       Log.i("APIFuncs",TicketAPI.toString());
       try
        {
     Updates = TicketAPI.getJSONArray("updates");
      UpdateErrorMessage("");
        }
        catch (JSONException e) 
        {
      UpdateErrorMessage("There are no updates for this ticket. (Strange!)");
        }
                
        //OK lets actually do something useful
        List<TicketUpdates> listOfUpdates = new ArrayList<TicketUpdates>();
        int TicketCount = Updates.length();
                
        if(TicketCount == 0)
        {
      UpdateErrorMessage("There are no updates for this ticket.");
      return;
        }
                
        for(int i = 0; i < TicketCount; i++)
        {
    try 
    {
       CurrentTicket = Updates.getJSONObject(i);
            
    } 
    catch (JSONException e1) 
    {
       Log.e("APIFuncs",e1.getMessage());
    }
            
    if(CurrentTicket != null)
    {
       listOfUpdates.add(new TicketUpdates(GetString("type"),
            GetString("name"), 
            GetInt("time"), 
            GetString("content")));
    }
        }
        TicketUpdatesAdaptor adapter = new TicketUpdatesAdaptor(this, listOfUpdates, API.SessionID);
                
        list.setAdapter(adapter);
    }*/
}