Monday, July 8, 2013

BOOT_COMPLETED BroadcastReceiver In Android

Android BootReciever


Many a time We need to perform some task when Device/Mobile finish it's booting process.
For Example giving notification that "SIM has been Changed" etc.

For this we need a Broadcast  receiver that should receive "BOOT_COMPLETED" broadcast. We must know  that when a device finishes booting Android System sends "BOOT_COMPLTED" broadcast.

Registering the BootReciever in android manifest file


<receiver android:name=".BootReceiver">
                        <intent-filter>
                                <action android:name="android.intent.action.BOOT_COMPLETED" />
                                <category android:name="android.intent.category.HOME" />
                        </intent-filter>
   </receiver>



and do not forget to add the following permission in manifest .

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

this permission is required to listen/reverie the BOOT_COMPLETED action



BootRecieAndroidvr In

BootReceiver.java  

 

public class BootReceiver extends BroadcastReceiver
{
      
            public void onReceive(Context context, Intent intent)
            {
               
                   // Your code to execute when Boot Completd

                   Toast.makeText(context, "Booting Completed", Toast.LENGTH_LONG).show();
            }
}
                     
           
The onRecieve() method of BootReceiver will execute when boot completes, so wee need to write the code inside onReceive() method.





Thursday, July 4, 2013

What is Android

What is Android

    Android is a  Linux-based operating system

•     Android is  designed primarily for touchscreen mobile devices such as smartphones and tablet computers.
•   Android is  currently developed by Google.
•    Android was Initially developed by Android Inc, whom Google financially backed and later purchased in 2005.
•     The Android Software Development Kit (Android SDK) provides all necessary tools to develop Android applications. This includes a compiler, debugger and a device emulator, as well as its own virtual machine to run Android programs

Android is open source and Google releases the code under the Apache License


Android applications run in a sandbox, an isolated area of the system that does not have access to the rest of the system's resources, unless access permissions are explicitly granted by the user when the application is installed. Before installing an application, the Play Store displays all required permissions
The source code for Android is available under free and open source software licenses. Google publishes most of the code (including network and telephony stacks) under the Apache License version 2.0 


The version history of the Android mobile operating system began with the release of the Android beta in November 2007. The first commercial version, Android 1.0, was released in September 2008

Below is List of Android Version and API Level

 
Version Code name Release date           API level
4.2     Jelly Bean November 13, 2012               17
4.1.x Jelly Bean July 9, 2012                      16
4.0.x Ice Cream Sandwich December 16, 2011       15
3.2 Honeycomb July 15, 2011       13
3.1 Honeycomb May 10, 2011       12
2.3.3–2.3.7 Gingerbread February 9, 2011      10
2.3–2.3.2 Gingerbread December 6, 2010       9
2.2 Froyo May 20, 2010        8
2.0–2.1 Eclair October 26, 2009       7
1.6 Donut September 15, 2009                4





The Android Development Tutorials blog contains Basic as well as Advanced android tutorials.Go to Android Development Tutorials to get list of all Android Tutorials.

 

 

More Android Topics

New Advance Topics:
Android ImageSwitcher                    Android TextSwitcher                                Android ViewFlipper
Android Gesture Detector               Handling/Detecting Swap Events                Gradient Drawable
Detecting Missed Calls                    Hide Title Bar                                           GridView Animation

 Beginning With Android
      Android : Introduction                                                              Configuring Eclipse for Android Development
     Creating Your First Android Project                                           Understanding Android Manifest File of your android app

 Advance Android Topics                                                              Customizing Android Views


Working With Layouts                                                                Working With Views

Understanding Layouts in Android                                                   Using Buttons and EditText in Android
Working with Linear Layout (With Example)                                     Using CheckBoxes in Android
Nested Linear Layout (With Example)                                              Using AutoCompleteTextView in Android                                                                                          Grid View
Relative Layout In Android                                                               ListView
Table Layout                                                                                   Android ProgressBar
Frame Layout(With Example)                                                          Customizing ProgressBar
Absolute Layout                                                                             Customizing Radio Buttons
Grid Layout                                                                                    Customizing Checkboxes In Android

Android Components                                                                 Dialogs In Android

Activity In Android                                                                    Working With Alert Dialog
Activity Life Cycle                                                                    Adding Radio Buttons In Dialog
Starting Activity For Result                                                       Adding Check Boxes In Dialog
Sending Data from One Activity to Other in Android                    Creating Customized Dialogs in Android
Returning Result from Activity                                                   Creating Dialog To Collect User Input
Android : Service                                                                     DatePicker and TimePickerDialog
BroadcastReceiver                                                                   Using TimePickerDialog and DatePickerDialog In android

Menus In Android                                                                ListView:
Creating Option Menu                                                               Populating ListView With DataBase
Creating Context Menu In Android                                              Populating ListView with ArrayList
                                                                                               ListView with Custom Adapter

Toast                                                                                      Working With SMS
Customizing Toast In Android                                                       How to Send SMS in Android
Customizing the Display Time of Toast                                        How To Receive SMS
Customizing Toast At Runtime                                                  Accessing Inbox In Android
Adding Image in Toast
Showing Toast for Longer Time


TelephonyManager                                                            Storage: Storing Data In Android
Using Telephony Manager In Android                                          SharedPreferences In Android
                                                                                              Reading and Writing files to Internal Stoarage
Working With Incoming Calls                                             DataBase
How To Handle Incoming Calls in Android                                Working With Database in Android
How to Forward an Incoming Call In Android                            Creating Table In Android
CALL States In Android                                                          Inserting, Deleting and Updating Records In Table in Android


Miscellaneous
Notifications In Android
How To Vibrate The Android Phone
Sending Email In Android
Opening a webpage In Browser
How to Access PhoneBook In Android
Prompt User Input with an AlertDialog





Tuesday, July 2, 2013

Android Live Wallpaper Tutorial

Live wallpapers   - are  richer, animated, interactive backgrounds on Homescreen.

In this post I will describe step by step "How to Create LiveWallpaper " .

  The Android Development Tutorials blog contains Basic as well as Advanced android tutorials.Go to Android Development Tutorials to get list of all Android Tutorials.

 

Android Live Wallpaper Example


In this example I have created a LiveWallpaper in which a Fish  is moving left to right.
The example is too simple and has been described in manner that you can easily  understand.

Android Live  Wallpaper


How to create a Live Wallpaper

Step 1: create a xml file which will describe your wallpaper.
Step 2: edit your manifest file add service and feature.
Step 3: create a service class which extends WallpaperService class.


Step 1:  create a xml folder inside res folder.
              inside xml folder create mywallpaper.xml file

             <?xml version="1.0" encoding="UTF-8"?>
            <wallpaper
                     xmlns:android="http://schemas.android.com/apk/res/android"
                     android:thumbnail="@drawable/fish"
                     android:description="@string/wallpaper_description" 
            />

here thumbnail is the image that will be shown in list of livewallpapers and description attribute is the description of your live wallpaper.

Also add following in your string.xml file inside values folder
<string name="wallpaper_description">Fish Aquarium</string>

Step 2: Edit your Manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.livewallpapertutorial"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

     <uses-feature
        android:name="android.software.live_wallpaper"
        android:required="true" >
    </uses-feature>


    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <service
            android:name="LiveWallpaperService"
            android:enabled="true"
            android:label="Wallpaper Example "
            android:permission="android.permission.BIND_WALLPAPER" >
            <intent-filter>
                <action android:name="android.service.wallpaper.WallpaperService" >
                </action>
            </intent-filter>

            <meta-data
                android:name="android.service.wallpaper"
                android:resource="@xml/mywallpaper" >
            </meta-data>

        </service>

    </application>

</manifest>



Step 3: create a service class which extends  WallpaperService.

this class is the base class for all live wallpapers in the system. You must implement the   onCreateEngine()   method .
The Engine class defines the life cycle methods, as for example onCreate(), onSurfaceCreated(), onVisibilityChanged(), onOffsetsChanged(), onTouchEvent() and onCommand()


public class LiveWallpaperService extends WallpaperService
{
                int x,y;
               
                public void onCreate()
                {
                        super.onCreate();
                }

                public void onDestroy()
                {
                        super.onDestroy();
                }

                public Engine onCreateEngine()
                {
                        return new MyWallpaperEngine();
                }

                class MyWallpaperEngine extends Engine
                {

                        private final Handler handler = new Handler();
                        private final Runnable drawRunner = new Runnable() {
                            @Override
                            public void run() {
                                draw();
                            }
                        };
                        private boolean visible = true;
                        public Bitmap image1,backgroundImage;

                        MyWallpaperEngine()
                        {
                                 // get the fish and background image references
                                image1 = BitmapFactory.decodeResource(getResources(),R.drawable.fish);
                                backgroundImage = BitmapFactory.decodeResource(getResources(),R.drawable.background);
                                x=-130; // initialize x position
                                y=200;  // initialize y position
                               
                        }


                        public void onCreate(SurfaceHolder surfaceHolder)
                        {
                                super.onCreate(surfaceHolder);
                        }

                        @Override
                        public void onVisibilityChanged(boolean visible)
                        {
                                this.visible = visible;
                                // if screen wallpaper is visible then draw the image otherwise do not draw
                                if (visible)
                                {
                                    handler.post(drawRunner);
                                }
                                else
                                {
                                    handler.removeCallbacks(drawRunner);
                                }
                        }

                        @Override
                        public void onSurfaceDestroyed(SurfaceHolder holder)
                        {
                                super.onSurfaceDestroyed(holder);
                                this.visible = false;
                                handler.removeCallbacks(drawRunner);
                        }

                        public void onOffsetsChanged(float xOffset, float yOffset, float xStep, float yStep, int xPixels, int yPixels)
                        {
                                draw();
                        }

                        void draw()
                        {
                                final SurfaceHolder holder = getSurfaceHolder();
                 
                                Canvas c = null;
                                try
                                {
                                        c = holder.lockCanvas();
                                        // clear the canvas
                                        c.drawColor(Color.BLACK);
                                        if (c != null)
                                        {
                                                // draw the background image
                                                c.drawBitmap(backgroundImage, 0, 0, null);
                                                // draw the fish
                                                c.drawBitmap(image1, x,y, null);
                                                // get the width of canvas
                                                int width=c.getWidth();
                                               
                                                // if x crosses the width means  x has reached to right edge
                                                if(x>width+100)
                                                {  
                                                        // assign initial value to start with
                                                        x=-130;
                                                }
                                                // change the x position/value by 1 pixel
                                                x=x+1;
                                        }
                                 }
                                finally
                                {
                                        if (c != null)
                                               holder.unlockCanvasAndPost(c);
                                }

                                handler.removeCallbacks(drawRunner);
                                if (visible)
                                {
                                          handler.postDelayed(drawRunner, 10); // delay 10 mileseconds
                                }   

                        }
                }
}


Run the application and set the Wallpaper.( Long Press on Screen ->Set Wallpaper -> HomeScreen -> Live Wallpapers -> select your Live wall paper)

Android Live  Wallpaper




 

More Android Topics

New Advance Topics:
Android ImageSwitcher                    Android TextSwitcher                                Android ViewFlipper
Android Gesture Detector               Handling/Detecting Swap Events                Gradient Drawable
Detecting Missed Calls                    Hide Title Bar                                           GridView Animation

 Beginning With Android
      Android : Introduction                                                              Configuring Eclipse for Android Development
     Creating Your First Android Project                                           Understanding Android Manifest File of your android app

 Advance Android Topics                                                              Customizing Android Views


Working With Layouts                                                                Working With Views

Understanding Layouts in Android                                                   Using Buttons and EditText in Android
Working with Linear Layout (With Example)                                     Using CheckBoxes in Android
Nested Linear Layout (With Example)                                              Using AutoCompleteTextView in Android                                                                                          Grid View
Relative Layout In Android                                                               ListView
Table Layout                                                                                   Android ProgressBar
Frame Layout(With Example)                                                          Customizing ProgressBar
Absolute Layout                                                                             Customizing Radio Buttons
Grid Layout                                                                                    Customizing Checkboxes In Android

Android Components                                                                 Dialogs In Android

Activity In Android                                                                    Working With Alert Dialog
Activity Life Cycle                                                                    Adding Radio Buttons In Dialog
Starting Activity For Result                                                       Adding Check Boxes In Dialog
Sending Data from One Activity to Other in Android                    Creating Customized Dialogs in Android
Returning Result from Activity                                                   Creating Dialog To Collect User Input
Android : Service                                                                     DatePicker and TimePickerDialog
BroadcastReceiver                                                                   Using TimePickerDialog and DatePickerDialog In android

Menus In Android                                                                ListView:
Creating Option Menu                                                               Populating ListView With DataBase
Creating Context Menu In Android                                              Populating ListView with ArrayList
                                                                                               ListView with Custom Adapter

Toast                                                                                      Working With SMS
Customizing Toast In Android                                                       How to Send SMS in Android
Customizing the Display Time of Toast                                        How To Receive SMS
Customizing Toast At Runtime                                                  Accessing Inbox In Android
Adding Image in Toast
Showing Toast for Longer Time


TelephonyManager                                                            Storage: Storing Data In Android
Using Telephony Manager In Android                                          SharedPreferences In Android
                                                                                              Reading and Writing files to Internal Stoarage
Working With Incoming Calls                                             DataBase
How To Handle Incoming Calls in Android                                Working With Database in Android
How to Forward an Incoming Call In Android                            Creating Table In Android
CALL States In Android                                                          Inserting, Deleting and Updating Records In Table in Android


Miscellaneous
Notifications In Android
How To Vibrate The Android Phone
Sending Email In Android
Opening a webpage In Browser
How to Access PhoneBook In Android
Prompt User Input with an AlertDialog