Friday, December 31, 2010

Showing Notification in Android




When we talk about notification we need to consider 3 major aspects.

1. What is Notification
2. Why Notification
3. How to create Notification

What is Notification:
Notification are the messages which are produced at top in the status bar in your android phones. Its a service which is used to show data of your application to the users even when the application is not running on the screen.


Why Notification
Something similar to you SMS Application. As soon as you recieve a SMS you recieve a notification at the top of your screen. The same results can be obtained by your application when using Notification.
Hence, multiple number of times when you are doing some background processes using your Services (e.g. Server request to check for new data on server) and you want to tell user that you have obtained new data and he should launch your application, at this point you will show them Notification message stating that new data has arrived.


How To Use
/**
* Copyright [2010] [Abhinava Srivastava]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package abhi.blog.notification;

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

/**
* Notification Example class
*
* @author Abhinava Srivastava
*
*/
public class NotificationCaller extends Activity {

// PASSED AS PAREMETER TO NOTIFICATION
private final int NOTIFICATION_ID = 0;


// NotificationManager Reference Holder
private NotificationManager mNotificationManager = null;

// NOtification Reference Holder
private Notification mNotification = null;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

/**
* Need to use NotificationManager Instance of NotificationManger is
* available in our SystemServices which is available in our context.
*/
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

/**
* To clear any existing NOtification with same ID
*/
mNotificationManager.cancel(NOTIFICATION_ID);
}

/**
* Function to handle click event generated by our button
*
* @param v
*/
public void launchNotification(View v) {



/**
* After we have obtained instance of notification manager we need to
* create the notification that we need to show The Constructor of
* Notification takes in 3 Parameters 1. Resource id of icon you want to
* show. 2. Text that should be shown 3. When do you wish to shoe this
* Notification
*/
mNotification = new Notification(R.drawable.icon,
"abhinavasblog.blogspot.com", System.currentTimeMillis());

/**
* Need to setup the view that you need show when user expands his/her
* Status bar.
*/
mNotification.contentView = new RemoteViews("abhi.blog.notification",
R.layout.special_notification);

/**
* Setting up the PendingIntent (Pending Intent is a special kind of
* intent which can be invoked at any specific event) Here we launch our
* application again as soon as we click on the Notification
*/
mNotification.contentIntent = PendingIntent.getActivity(
NotificationCaller.this, 0, new Intent(NotificationCaller.this,
NotificationCaller.class),
PendingIntent.FLAG_CANCEL_CURRENT);

/**
* Finally, we set the created Notification in NotificationManagers
* instance.
*/
mNotificationManager.notify(NOTIFICATION_ID, mNotification);

}
}


main.xml


android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>




special_notification.xml


xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:orientation="vertical" android:layout_height="wrap_content">












Wish you all a Very Happy New Year

Monday, December 13, 2010

Andorid 2.3 Gingerbread

Hey there,
well it has been quite a news for everyone that Android has launched its new 2.3 code name Gingerbread.
had been looking around the updates we gonna get with this new OS update, thought about sharing some info's about the same...

There had been addition of couple of new kool features... like

SIP-based VoIP and NFC(Near Field Communicator)

Whats SIP-based VoIP?

SIP (Session Initiated Protocol) is a protocol which had been defined by IETF (Internet Engineering Task Force).
Sip is majorly used to controll multimedia communication such as Voice or Video communication over IP also know as VoIP (Voice Over Internet Protocol).
Till 2.2 if you wish to develop application based on VoIP you had to implement SIP and VoIP stack on your own. Hence as we move with 2.3 the advantages of pre-implementations will always prove beneficial to the developers also to the users who are looking for some alternative in same domain.
With Sip based VoIP install in your new device you can directly make VoIP calls from your default calling application in Gingerbread.

NFC (Near Field Communications)

NFC is a short range high frequency wireless data communication technology, which enables user to communicate with any NDEF enabled devices, these devices can be posters, car locks or anything that you can imagine, to work with NFC you need to place your device in proximity of 10 cms. ie around 4 inches. With the addition of this new for of communication people now can extract information with more and more different kind of places/devices.


Well that was a quick update about what i came accross, yes there are LOT more in new Andorid,
I will be posting updates as soon as i find some time! :)

Hope you got to learn something new today!

@nJoy!
Abhinava