Showing posts with label thread in android. Show all posts
Showing posts with label thread in android. Show all posts

Sunday, 17 November 2013

Difference between Android AsyncTask,Thread,IntentService and Service


Service
Thread
IntentService
AsyncTask
When to use ?
Task with no UI, but shouldn't be too long. Use threads within service for long tasks.
- Long task in general.

- For tasks in parallel use Multiple threads (traditional mechanisms)
- Long task usually with no communication to main thread.
(Update)- If communication is required, can use main thread handler or broadcast intents

- When callbacks are needed (Intent triggered tasks). 
- Small task having to communicate with main thread.

- For tasks in parallel use multiple instances OR Executor
 
Trigger
Call to method
onStartService()
Thread start() method
Intent
Call to method execute()
Triggered From (thread)
Any thread
Any Thread
Main Thread (Intent is received on main thread and then worker thread is spawed)
Main Thread
Runs On (thread)
Main Thread
Its own thread
Separate worker thread
Worker thread. However, Main thread methods may be invoked in between to publish progress.
Limitations /
Drawbacks
May block main thread
- Manual thread management

- Code may become difficult to read
- Cannot run tasks in parallel.

- Multiple intents are queued on the same worker thread.
- one instance can only be executed once (hence cannot run in a loop) 

- Must be created and executed from the Main thread

Content from : techtej

Thursday, 21 March 2013

Android Splash Screen Example

Here we can See how to create splash screen in Android Application.
Some times the app need to show some company logo and some loading progress before starting Application at this time Splash Screen is main role.

UI Source(splash_layout.xml)


<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"
    tools:context=".SplashActivity" >

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Please wait Loading....!" />

    <ImageView
        android:id="@+id/logo"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_above="@+id/textview"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="22dp"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>

Android Activity(SplashActivity.java)

This is splash screen activity here we are using theads to wait some time with in this screen.
after finishing sleep time SplashActivity will be  navigate to HomeActivity using Intents.

package com.androidsurya.splashscreenexample;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;

public class SplashActivity extends Activity {
    private static int SLEEP_TIME = 5500;// no of mille seconds sleep

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_layout);
        // The thread to wait for splash screen events
        Thread splashThread = new Thread() {
            @Override
            public void run() {
                try {
                    synchronized (this) {
                        // Wait given period of time or exit on touch
                        wait(SLEEP_TIME);
                    }
                } catch (InterruptedException ex) {
                }

                finish();

                // Run next activity
                Intent intent = new Intent();
                intent.setClass(SplashActivity.this, HomeActivity.class);
                startActivity(intent);
            }
        };

        splashThread.start();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.splash_layout, menu);
        return true;
    }

}

UI layout(home_layout.xml)

<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"
    tools:context=".SplashActivity" >

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

</RelativeLayout>

Android Activity(HomeActivity.java)

package com.androidsurya.splashscreenexample;

import android.app.Activity;
import android.os.Bundle;

public class HomeActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_layout);
    }

}

Register both Activity's in AndroidManifest file

<activity
            android:name="com.androidsurya.splashscreenexample.SplashActivity"
            android:label="@string/app_name" >
          
        </activity>
        <activity
            android:name="com.androidsurya.splashscreenexample.HomeActivity"
            android:label="@string/app_name" >

Output Screenshot






















Android SQLite Database Viewer or Debuging with Stetho

Every Android Developer uses SQLite Database to store data into the Android Application data. But to view the data in SQLite have a lot of...