My Blog List

Monday, 28 November 2011

Android Broadcast Receiver Tutorial.


Hello friends.
I hope my last tutorial will be helpful for you.
Today i am going to post all about Broadcast Receiver in Android.So going through this post you will be able to understand all about broadcast in android.
So extend BroadcastReceiver in the class to which you want to make receiver class  and which is registered as a receiver in an Android Application via the AndroidManifest.xml file(or via code).This class will be able to receive intents. Intents can be generated via the Context.sendBroadcast() method. The class BroadcastReceiver defines the method onReceive() . Only during this method your broadcast receiver object will be valid, afterwards the Android system will consider your object as no longer active. Therefore you cannot perform any asynchronous operation. 

We will define a broadcast receiver which listens to airplane mode  changes. If the airplane mode changes  then our receiver will be notified and toast a message.
  •   Create a new project "com.bp.broadcastdemo". We do not need an activity. Create the following "AndroidManifest.xml".

  • Create the following class "AirplaneMode".
  •  Now install your app in emulator ,change the airplane mode(on/off) by long press on hardware button
  • Here is your o/p

 That's all
;-)
for any query mail me :bpsingh216@gmail.com

Friday, 25 November 2011

How to Handle Status Bar Notification in Android.

Hello friends,
Good Afternoon ;-)
This is another post for  android developer. This time we will notify user with Status Bar Notification .
So first of all some basics about notification in android.
With the help of Notification you can notify user for particular event occurrence, like incoming sms, mail, downloading data or some services are running .So for handle notification in your own app.  Android provide two classes :
1. NotificationManager
2.Notification
Notification class sets the properties like icon,ticker text,sound,vibration and all that.
Whereas
The NotificationManager is an Android system service that executes and manages all status bar notifications. You do not instantiate the NotificationManager directly. In order to give it your Notification, you must retrieve a reference to the NotificationManager with getSystemService() and then, when you want to notify the user, pass it your Notification with notify().
So here is the sample project for Notification.
1.Create new project  file->new->Android Project.
2.Create  your main.xml file.
3.Do some code  in your main.java file.


package com.bp.notifyme;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class NotifyMe extends Activity {
    private static final int HELLO_ID = 1;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    public void notifyMe(View view)
    {
        NotificationManager notificationmanager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification=new Notification(R.drawable.msg, "You have notification",System.currentTimeMillis());
        Intent notificationIntent=new Intent(this,Next.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        notification.setLatestEventInfo(getApplicationContext(), "MyNotification", "This is notification Demo", contentIntent);


        notificationmanager.notify(HELLO_ID, notification);
    }
}
4.Create one another Next.java class,do nothing in this class.
5.Run your project in emulator. here is the o/p from my emulator.














Thank you
good day ;-)
for any query feel free to mail me : bpsingh216@gmail.com