We provide complete mobile and web apps development solutions

Friday, February 8, 2013

Loaders in Android

Loaders providing Asynchronous loading data into an activity or fragment.When the data changes, it automatically update the new results.It automatically reconnect last loader when its recreated. So, no need to re query.

Starting the Loader:

we can initilize loader in onCreate() method.

getLoaderManager().initLoader(0,null, this);


initLoader takes the following parameters

unique id. In the above unique id is 0.
argument to supply to loader at construct (optional).
LoaderManager.LoaderCallbacks

LoaderManager calls to report loader events.

Thursday, February 7, 2013

Internal Flow of Threads or Thread Model

    whenever we write multi threading application, it contains multiple flow of controls.
All the flow controls follow the sequence steps at execution time.


Java programs starts execution by loading .class file

JVM creates a background thread and will reside in main.

Background thread creates various foreground threads and dispatches to their run to execute and result back to background and will handover to garbage.
Background thread gives back to program.
JVM collects background thread and will handover to Garbage collector.
Java program stops its execution.

Monday, February 4, 2013

Difference between StartActivityForResult, startActivity

Many of them  have confution about startActivity and startActivityForResult() methods.
startActivity() is to start another activity
StartActivityForResult() is to receive a result. The activity has to return a result.
When it returns a result, it sends as another intent object and activity recives result in onActivityResult() and here we can handle.
We need to pass an integer arg to startActivityForResult() method. So that request code will identifies the request.
When you get the result intent, callback provides the same request code to identify the result.

see this example:
  Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, 1);
@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1){           
        }       


Sunday, February 3, 2013

Tabs like with ActionBar


 TabActivity is depreciated  in Api 13. Tabs are presented in latest versions with ActionBar.
ActionBar.newTab().

So, prefer to not to use depreciated classes and methods. So use latest classes and methods.

Here is the simple example of creating Tab like activity using fragments.



 MobEngineersActivity.java

package com.srinivas.mobengineers;

import android.app.ActionBar;
import android.app.Activity;
import android.app.ActionBar.Tab;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.os.Bundle;
import android.widget.Toast;


public class MobEngineersActivity extends Activity {
    public static Context appContext;
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        appContext = getApplicationContext();

 
        ActionBar actionbar = getActionBar();       // initilize actionBar
        actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);     // set navigation mode
       
        ActionBar.Tab Category1Tab = actionbar.newTab().setText("FOOD");
        ActionBar.Tab Category2Tab = actionbar.newTab().setText("BEVERAGES");
       
        Fragment FoodFragment = new FoodFragment();
        Fragment BeveragesFragment = new BeveragesFragment();
       CategoryTab.setTabListener(new MyTabsListener(FoodFragment));
      BeveragesTab.setTabListener(new MyTabsListener(BeveragesFragment));

        actionbar.addTab(FoodTab);
        actionbar.addTab(BeveragesTab);
    }
Food.java

public class FoodFragment extends Fragment {
   
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.afragment, container, false);
    }
  }

Beverages.java
public class FoodFragment extends Fragment {
   
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.afragment, container, false);
    }
  }

 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="@+id/ll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center">
    <LinearLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

 </LinearLayout>
</LinearLayout>



Tuesday, January 29, 2013

Bluetooth -Android Programming

  Android supports Bluetooth for wireless connectivity. It comes with API of android bluetooth.


We have different classes in the API

BluetoothAdapter: It represents local Bluetooth Adapter.Using this class, we can discover the devices near by.Uisng this we can create BluetoothServerSocket for listening from other devices.

 BluetoothDevice: By using this we can request connection to remote device through BluetoothSocket.


BluetoothSocket: We can exchange the data with other Bluetooth Device.

BluetoothServerSocket: it listens the incoming request. To connect any device, one device  must open a server socket.


BluetoothProfile:It is an interface for wireless devices.

Bluetooth: Using this class, we can connect to Bluetooth Headset.

BluetoothHealth: It represents a health device profile that controls Bluetooth service.
BluetoothA2dp(Advanced Audio Distribution Profile): It tells how audio quality streamed from one mobile to other .

BluetoothProfile.ServiceListener: It gives notification when the client connects or disconnects.


To use Bluetooth in Android, we need to declare permissions in manifest.xml


 <uses-permission android:name="android.permission.BLUETOOTH" />
 <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />



How to check whether the device support Bluetooth or not?

BluetoothAdapter btAdapter=BluetoothAdapter.getDefaultAdapter();
if( btAdapter==null){
//device doesnot support Bluetooth.
 }
else{ //device support bluetooth.


How to enable Bluetooth:
 //Prompt user to turn on Bluetooth
Intent enableI = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(enableI, REQUEST_ENABLE_BT);




        // Set up a pointer to  remote node using address.
        BluetoothDevice device = btAdapter.getRemoteDevice(address);
 



/ Establish connection. 
  bltSocket.connect();

Monday, January 28, 2013

What is Bluetooth?

-->
        Bluetooth is a wireless technology for connecting shortest distance clients(creates personnel area network) within the range of 10 meters with the frequency of 2.4 GHZ of unlicensed band. To connect to the other client, the client also have Bluetooth. It is the greatest technology for replacing the cables for short distances.
     If we compare to the other systems, Bluetooth file transfer is faster packet transfer. It uses fastest acknowledgement, frequency hopping and unlicensed ISM band.

  At Present, most of the electronic goods coming with Bluetooth technology like Tv, mouse, audio devices, mobiles, remotes, computers etc.

cost of the Bluetooth device is cheaper than any other wireless technology.


How to use the Bluetooth and how to connect to other Bluetooth device programatically we can see in later topics.


  
 

Tuesday, January 22, 2013

Fragments- ActionBar android

public class StartActivity extends Activity {
    public static Context appContext;
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        appContext = getApplicationContext();

          ActionBar actionbar = getActionBar();
        actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
       
        ActionBar.Tab PlayerTab = actionbar.newTab().setText("SONGS");
        ActionBar.Tab StationsTab = actionbar.newTab().setText("VIDEOS");


       ActionBar.Tab PhotosTab = actionbar.newTab().setText("PHOTOS");       
        Fragment sFragment = new SongsFragment();
        Fragment vFragment = new VideosFragment();

        sTab.setTabListener(new MyTabsListener(sFragment));
        vTab.setTabListener(new MyTabsListener(vFragment));

        actionbar.addTab(sTab);
        actionbar.addTab(vTab);

}



 

SongsFragment.java

public class SongsFragment extends Fragment {
   
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.songsfragment, container, false);
    }
   
}







public class VideosFragment extends Fragment {
   
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.videosfragment, container, false);
    }





PhotosFragment.java



 
public class PhotosFragment extends Fragment {
   
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.photosfragment, container, false);
    }
   
}



xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
   android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_gravity="center">
    <LinearLayout
        android:id="@+id/fragment_tab"
        android:layout_gravity="bottom"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

 </LinearLayout>
</LinearLayout>
 





Online Training

Your Name :
Your Email: (required)
Your Message: (required)

Powered by Blogger.

Recent Posts

Find Us On Facebook

Popular Posts