Saturday, September 29, 2012




it provides access to the info about telephony services.
we can register services to receive notification of telephony state changes.



public int getCallState()-    get the call state

public cellLocation getCellLocation()- returns current location of the device.. returns null if the current location not available.

this requires permission in the manifest file.

what permissions we need to declare:


some more methods:

we can get device id

public string getDeviceId()

we can get network operator

public string getNetworkOperator()

we can get sim operator name

public string getSimOperatorName()

Hibernate Introduction

Limitations of Traditional Approach:

Application portability
Manual Operation on result set.
Caching should implement manually.
Mismatch between Object Oriented data representation and relational  data.

Traditional Approach:
It is Object Relational Mapping (ORM).
It’s a technique for mapping objects with relational data.
A framework is a semi finished application that can be customized to develop a specific application.

For example:
If anybody wants to establish a company, they can build their own building as per requirement. It takes time. They have other option that; they can buy which is already built. As per requirement they can modify the building like dividing into partitions etc. Here readymade building is framework.

Hibernate invented by Gavin King.
Hibernate is a open source framework.
It’s an ORM implementation.

Thursday, September 27, 2012

WifiManager in Android




WifiManager class provides primary api for managing all aspects of wifi connectivity.

get the instance of this class by calling


it deals with list of configured networks. this can be viewed, updated.

check wifi status:

ConnectivityManager conMgr;
NetworkInfo netInfo;
WifiManager wifiMgr;

//wifi  enabled
//wifi disabled i.e not available

Services And BroadcastReceivers in Android

Seo Directory List

Activity: it is single screen application. it is a foreground contain user interaction.


a service is a component which runs in the background without interacting with user.

Android provides many predefined services exposed via Manager class.

Own services must be declared in the manifest file..

Some methods:

An activity can start a service via startService() method.

stop the service by using stopService() method.

bindService() - this is for activity to interact with service.

it needs "ServiceConnection" object.

Once service is started    onCreate()  is called.

onBind()- the system calls when other component want to bind the service.

onStop() - for stopping the service.

OnDestroy()- called when service is no longer used. and it is destroyed.

package com.example.serviceexample;

import android.os.Bundle;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {
            Button b1;
            Button b2;
    public void onCreate(Bundle savedInstanceState) {
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(, menu);
        return true;

     public void onClick(View v) {
                        // TODO Auto-generated method stub
                                    startService(new Intent(this,ServiceDemo.class));
                                    startService(new Intent(this,ServiceDemo.class));
package com.example.serviceexample;

import android.content.Intent;
import android.os.IBinder;

public class ServiceDemo extends Service{
      MediaPlayer mediaplayer;

      public IBinder onBind(Intent arg0) {
            // TODO Auto-generated method stub
            return null;

      public void onCreate() {
            // TODO Auto-generated method stub
            mediaplayer=MediaPlayer.create(this, R.raw.newsong);

      public void onDestroy() {
            // TODO Auto-generated method stub
      public void onStart(Intent intent, int startId) {
            // TODO Auto-generated method stub
            super.onStart(intent, startId);

Declare manifest permission for service

 <service android:name="ServiceDemo"></service>

you can download source code here

Own services must be declared in manifest file.

how to declare own service:

<service android:name="your class"></service>
implementing class must extend the class   " service or one of its subclasses.

Ex: AsyncTask:
using services in android allows to have app running in the background. 


if u want to do a simple task run in the background, use AsyncTask.

AsyncTask class:

private class someclassname extends AsyncTask<x,y,z>

protected void onPreExecute()
// starting new thread i.e    initilizing

protected void doInBackground()

// perform the action or do the task

protected void onPostExecute(){
// closing the process

BroadCast Receiver:

It class which extends BroadCastReceiver.  
which is registered in the android app via manifest file.

This class can be able to recieve intents via sendBroadcast()

BroadCast Receiver defines the method  onReceive().  During this method , BroadCast Receiver object will be valid.


public class PhoneReceiver extends BroadcastReciever

public void onReceive()

Monday, September 17, 2012

Data Storage in Android

Data Storage:

Android stores data in 5 ways depends on the data storage

internal storage
external storage
sqlite database
network connection

sharedpreferences: it will store the data as a key value pairs

to get SharedPreferences of ur app, use these

getSharedPreferences()  - use this if u need multiple preferences

getPreferences()- use this if u need single preference.

you can find shared preferences example here

Internal Storage:

InputStream()- for reading the data
OutputStream()- for writing the data to the stream

1.openFileOutput() - we call the particular file.

2. write to the file with  write()
3. close the file after writing -   close()

String FILENAME= "hello_file";
Strig string="helloworld";

FileOutputStream fos=openFileOutput(FILENAME, Context.MODE_PRIVATE);



getDir()- create directory in the internal storage

deleteFile()- deletes the directory

External Storage-

boolean mExternalStorageAvilable= false;
boolean mExternalStorageWritable= false;

if(Environment.MEDIA_MOUNTED.equals(state)) {

mExternalStorageAvilable=mExternalStorageWritable= true;

mExternalStorageWritable= false;



public class namedir extends SqliteOpenHelper

private static final int DATABASE_VERSION=2;
private static final String NAME_TABL="namedir";


public void onCreate(SqliteDatabase db)
db.execSQL("CREATE TABLE NAMEDIR(id varchar(20), image BLOB, caption varchar(30), description varchar(100))");
private void VersionUpdation(SQLiteDatabase db)
public boolean CheckDataBase(String db)
SQLiteDatabase checkDB=null;

Monday, September 3, 2012

Activity Life Cycle

Activities in the life cycle are managed as an activity stack. When a new activity is started, it is placed on the top of the stack.  It becomes the running activity.
The previous activity always remains below it in the stack, and will not come to the foreground until the new activity exits.
4       Stages of Activity:
1.       If an activity in the foreground of the screen (at the top of the stack), it is active or running.
  1. If an activity has lost focus but is still visible, it is paused. It saves the activity state, but can be killed by the system in extreme low memory situations.
  2. If an activity is completely occupied by another activity, it is stopped. It  retains all state and member information.
  3. If an activity is paused or stopped, the system can drop the activity from memory by either asking it to finish or simply killing the process.

Public class MainActivity extends Activity {
Public void onCreate(Bundle savedInstanceState) {
Protected void onStart() {
// The activity is about to become visible.
Protected void onResume(){
// the activity has become visible
Protected void onPause(){
// Another activity is taking focus
Protected void onStop(){
//the activity is no longer visible
Protected void onDestroy(){
//the activity is about to be destroyed
The entire lifetime of an activity happends between the call to onCreate() and call to onDestroy().