Learn Android for Free,issues for developers, mobengineers website provide complete solutions for developers issues posts.It also provides online training for android development

Saturday, September 29, 2012

Hibernate Architecture

Hibernate Architecture:



TelephonyManager

TelephonyManager


java.lang.object
android.telephony.TelephonyManager

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


context.getSystemService(context.TELEPHONY_SERVICE);


SOME METHODS:

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:

ACCESS_COARSE_LOCATION
ACCESS_FINE_LOCATION

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.
Framework:
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:
Hibernate invented by Gavin King.
Hibernate is a open source framework.
It’s an ORM implementation.



Thursday, September 27, 2012

WifiManager in Android

WifiManager:


java.lang.object

-  android.net.wifi.wifimanager

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

get the instance of this class by calling

contaxt.getSystemService(context.WIFI_Service);

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


check wifi status:

ConnectivityManager conMgr;
NetworkInfo netInfo;
WifiManager wifiMgr;

conMgr=(ConnectivityManager)getSystemService(context.WIFI_Service);
netInfo=conMgr.getActiveNetworkInfo();
if(!(netInfo==null))
{
if(WifiMgr.isWifiEnabled())
{
//wifi  enabled
}
else
{
//wifi disabled i.e not available
}
}






Services And BroadcastReceivers in Android

Seo Directory List

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


Service:

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.
onStart()



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.

ex:




 MainActivity.java
package com.example.serviceexample;

import android.os.Bundle;
import android.app.Activity;
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;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
         b1=(Button)findViewById(R.id.button1);
         b2=(Button)findViewById(R.id.button2);
        b1.setOnClickListener(this);
        b2.setOnClickListener(this);   
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

     public void onClick(View v) {
                        // TODO Auto-generated method stub
                        switch(v.getId())
                        {
                        case R.id.button1:
                                    startService(new Intent(this,ServiceDemo.class));
                                    break;
                        case R.id.button2:
                                    startService(new Intent(this,ServiceDemo.class));
                                   
                        }
            }
}


 ServiceDemo.java
package com.example.serviceexample;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;

public class ServiceDemo extends Service{
     
      MediaPlayer mediaplayer;

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

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

      @Override
      public void onDestroy() {
            // TODO Auto-generated method stub
            super.onDestroy();
            mediaplayer.stop();
      }
      @Override
      public void onStart(Intent intent, int startId) {
            // TODO Auto-generated method stub
            super.onStart(intent, startId);
            mediaplayer.start();
      }
}

Declare manifest permission for service

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



you can download source code here

http://www.4shared.com/rar/O39SukJ_/ServiceExample.html?







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.


ex: 

public class PhoneReceiver extends BroadcastReciever
{

@override
public void onReceive()
{
}
}



Monday, September 17, 2012

Data Storage in Android

Data Storage:

Android stores data in 5 ways depends on the data storage

sharedpreferences
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 


https://www.4shared.com/zip/AEe4gg63/SharedPreferences.html?

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);

fos.write(string.getBytes());

fos.close();



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;
}
else
if(Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)){

mExternalStorageAvilable=true;
mExternalStorageWritable= false;











 









SqliteOpenHelper



public class namedir extends SqliteOpenHelper
{

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



@override


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) {
Super.onCreate(savedInstanceState);
}
Protected void onStart() {
Super.onStart();
// The activity is about to become visible.
}
Protected void onResume(){
Super.onResume();
// the activity has become visible
}
Protected void onPause(){
Super.onPause();
// Another activity is taking focus
}
Protected void onStop(){
Super.onStop();
//the activity is no longer visible
}
Protected void onDestroy(){
Super.onDestroy();
//the activity is about to be destroyed
}
}
The entire lifetime of an activity happends between the call to onCreate() and call to onDestroy().



20% off

Online Training

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

NDTV Gadgets360

Powered by Blogger.

Recent Posts

Find Us On Facebook

Popular Posts