Wednesday, 29 July 2015

How to create Handler and Runnable in android Activity?

This article teaches how to use handler and Runnable in Android.Before that we would see what is a handler and runnable in android.

What is Handler ...?
              A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue.Each Handler instance is associated with a single thread and that thread's message queue. When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it -- from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.
 
Uses of Handler...?
There are two main uses for a Handler:
 (1) to schedule messages and runnables to be executed as some point in the future;
 (2) to enqueue an action to be performed on a different thread than your own.

What is Runnable ...?

Runnable is just an interface you need to instantiate a thread to contain it. Whereas thread already contains the ability to spawn a thread.If you extend Thread you can't extend anything else (Java doesn't support multiple inheritance). You can have multiple interfaces on a class, therefore you could have Runnable.
The main use of Runnable is run code in a different Thread.

Example :
This Example Displaying Toast with the use of Handler and Runnable Simultaneously.

package com.example.simple_handler_example;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Toast;

public class MainActivity extends Activity {
Handler handler;
Runnable runnable;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Runnable Starts
        runnable = new Runnable() {

              public void run() {
              Toast.makeText(getApplicationContext(),"HAI",Toast.LENGTH_LONG).show();
        handler.postDelayed(runnable, 10000);//Run Simultaneously each 10000 milliseconds
             
                }
              };
              handler = new Handler();
              handler.postDelayed(runnable, 1000);//Starts After 1000 milliseconds
    } 

    @Override
        protected void onStop() {
            super.onStop();
           // To stop the handler
            handler.removeCallbacks(runnable);
        }
  
}
Output
Screen Shot :









Friday, 26 June 2015

What is IOIO..?

The IOIO is a board that provides a host machine the capability of interfacing with external hardware over a variety of commonly used protocols. The original IOIO board has been specifically designed to work with Android devices.
The newer IOIO-OTG ("on the go") boards work with both Android devices and PC's .
The IOIO board can be connected to its host over USB or Bluetooth, and provides a high-level Java API on the host side for using its I/O functions as if they were an integral part of the client.

How to Remove appcompat_v7 Support Library from Eclipse..?

With out the use of appcompat_v7 support library we are adding these lib with our project. If you want to  Remove simply follow these steps:


1.Firstly in project,Right click->properties->Android.There you can see the red marked appcompat placed in Reference. Click that and Remove it.


2.Edit and change your activity_main.xml like these:


<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>




3.In res/values/styles.xml:

<resources>

    <style name="AppBaseTheme" parent="android:style/Theme.Light">

    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">

    </style>

</resources>

4.In res/values-v11/styles.xml 
<resources>

    <style name="AppBaseTheme" parent="@android:style/Theme.Light">
    </style>

</resources>

5.In res/values-v14/styles.xml 
  <resources>

    <style name="AppBaseTheme" parent="@android:style/Theme.Light">    </style>

 </resources>


6.Change your menu/main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="@string/action_settings"/>
 </menu> 

7.Finally change your MainActivity.java


import android.app.Activity;
import android.os.Bundle;
 
 
public class MainActivity extends Activity {
 
 
    @Override 
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
} 
 
}

What is Context in Android..?

              Context is an interface to global information about an application environment. The Context class itself is declared as abstract class, whose implementation is provided by the Android OS. The documentation further provides that Context “…allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.”

Definition of Context::
  • Context represents environment data
  • It provides access to things such as databases
Simpler terms ::
  • Consider Person-X is the CEO of a start-up software company
  • There is a lead architect present in the company, this lead architect does all the work in the company which involves such as database, UI etc
  • Now the CEO Hires a new Developer
  • It is the Architect who tells the responsibility of the new hired person based on the skills of the new person that whether he will work on Database or UI etc
Simpler terms ::
  • It's like access of android-activity to app's resource
  • It's similar to a when you visit a hotel, you want breakfast, lunch & dinner in suitable timings right ?
  • There are many other things you like during the time of stay. How you get these things?
  • You ask the room-service person to bring the things for you
  • Here room-service person is the context considering you are the single activity and the hotel to be your app, finally the breakfast, lunch & dinner to be the resources

Things that involve context are:
  1. Loading a resource.
  2. Launching a new activity.
  3. Creating views.
  4. obtaining system service.
  5. Context is the base class for Activity, Service, Application .... etc

Another way to describe is, consider context as remote of a TV & channel's in the television are resources, services, using intents etc - - - Here remote acts as an access to get access to all the different resources into foreground.
  • So, Remote has access to channels such as resources, services, using intents etc ....
  • Likewise ..... Whoever has access to remote naturally has access to all the things such as resources, services, using intents etc

Different invoking methods by which you can get context

  • getApplicationContext()
  • getContext()
  • getBaseContext()
  • or this (when in the activity class)

Example:TextView TV=new TextView(this);


this -> refers to the context of the current activity.

Thursday, 25 June 2015

What is Android..?

  • Android is a software package and linux based operating system for mobile devices such as tablet computers and smartphones.
  • Android Android is a mobile operating system (OS) based on the Linux kernel and currently developed by Google.
  • Android is designed primarily for touchscreen mobile devices such as smartphones and tablet computers, with specialized user interfaces for televisions (Android TV), cars (Android Auto), and wrist watches (Android Wear).
  • Java language is mainly used to write the android code even though other languages can be used.