Android Notifications-Android Tutorial 6
We have seen Activities and Intents. Now we need to move on to services. However, since services mostly interact with a user through notifications, I felt the need to introduce a simple program to deal with Notifications.
What are Notifications? The name itself implies their functionality. They are a way of alerting a user about an event that he needs to be informed about or even take some action on getting that information.
Notification on Android can be done in any of the following ways:
-->Status Bar Notification
-->Vibrate
-->Flash lights
-->Play a sound
From the Notification, you can allow the user to launch a new activity as well. Now we will look at status bar notification as this can be easily tested on the emulator.
To create a status bar notification, you will need to use two classes: Notification and NotificationManager.
-->Notification – defines the properties of the status bar notification like the icon to display, the test to display when the notification first appears on the status bar and the time to display.
-->NotificationManager is an android system service that executes and manages all notifications. Hence you cannot create an instance of the NotificationManager but you can retrieve a reference to it by calling the getSystemService() method.Notification – defines the properties of the status bar notification like the icon to display, the test to display when the notification first appears on the status bar and the time to display.
-->NotificationManager is an android system service that executes and manages all notifications. Hence you cannot create an instance of the NotificationManager but you can retrieve a reference to it by calling the getSystemService() method.
Once you procure this handle, you invoke the notify() method on it by passing the notification object created.
So far, you have all the information to display on the status bar. However, when the user clicks the notification icon on the status bar, what detailed information should you show the user? This is yet to be created. This is done by calling the method setLatestEventInfo() on the notification object. What needs to be passed to this method, we will see with an example.
You can download the code for a very simple Notification exampleHere
The code is explained below:
Step 1: Procure a handle to the NotificationManager:
private NotificationManager mNotificationManager;
mNotificationManager =
NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Step 2: Create a notification object along with properties to display on the status bar
final Notification notifyDetails =
new Notification(R.drawable.android,"New Alert, Click Me!",System.currentTimeMillis());
Step 3: Add the details that need to get displayed when the user clicks on the notification. In this case, I have created an intent to invoke the browser to show the website http://www.android.com
Context context = getApplicationContext();
CharSequence contentTitle = "Notification Details...";
CharSequence contentText = "Browse Android Official Site by clicking me";
Intent notifyIntent = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse("http://www.android.com"));
PendingIntent intent =
PendingIntent.getActivity(SimpleNotification.this, 0,
notifyIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
notifyDetails.setLatestEventInfo(context, contentTitle, contentText, intent);
Step 4: Now the stage is set. Notify.
mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);
Note that all of the above actions(except getting a handle to the NotificationManager) are done on the click of a button “Start Notification”. So all the details go into the setOnClickListener() method of the button.
Similarly, the notification, for the example sake is stopped by clicking a cancel notification button. And the code there is :
mNotificationManager.cancel(SIMPLE_NOTFICATION_ID);
Now, you may realize that the constant SIMPLE_NOTIFICATION_ID becomes the way of controlling, updating, stopping a current notification that is started with the same ID.
For more options like canceling the notification once the user clicks on the notification or to ensure that it does not get cleared on clicking the “Clear notifications” button, please see the android reference documents
Showing posts with label Activating component intents android. Show all posts
Showing posts with label Activating component intents android. Show all posts
Android Tutorials for Beginner Part-4(Pre-Packed applications)
Android Tutorials for Beginner Part-4(Android Pre-Packed applications)
There are many applications that can pre-packaged into the android platform that can be reused by custom built applications because of the power of the android design that is based on implicit intents.(See part 3 of this series for implicit intents).
Here we will explore how to invoke the pre-packaged applications from our own through implicit intents. Note that in none of the snippets below, we actually call the pre-packaged or system applications. We just declare intents and pass them to an activity while starting the activity through startActivity() method. However, for each of these intents, the android platform finds the most befitting activity and invokes the same:
1. Call a number
Intent callNumber = new Intent();
callNumber.setAction(android.content.Intent.ACTION_CALL);
callNumber.setData(Uri.parse("tel:9440012345"));
startActivity(callNumber);
This will call the number 9440012345. For calling custom numbers, you could provide the user with a edit text field from which you can access the number and set it to the data above instead of a hard-coded number.
2. Browse the web for a given url:
Intent searchGivenText = new Intent(Intent.ACTION_WEB_SEARCH);
searchGivenText.putExtra(SearchManager.QUERY, "Android Examples);
startActivity(searchGivenText);
This will invoke the Google search engine to search the string "Android Examples" and return the results to you. This too can be generalized to accept a string from the user and set to the intent before starting the activity.
3. View google maps for a given location
Intent searchAddress = new Intent(Intent.ACTION_VIEW,
Uri.parse("geo:0,0?q=Bangalore"));
startActivity(searchAddress);
This shows the location of Bangalore on Google Maps.
4. View Contacts on the phone
Intent contacts = new Intent();
contacts.setAction(android.content.Intent.ACTION_VIEW);
contacts.setData(People.CONTENT_URI);
startActivity(contacts);
I have created an Android eclipse project which showcases all of these examples by taking inputs from the end user. You can access the same here.
There are many applications that can pre-packaged into the android platform that can be reused by custom built applications because of the power of the android design that is based on implicit intents.(See part 3 of this series for implicit intents).
Here we will explore how to invoke the pre-packaged applications from our own through implicit intents. Note that in none of the snippets below, we actually call the pre-packaged or system applications. We just declare intents and pass them to an activity while starting the activity through startActivity() method. However, for each of these intents, the android platform finds the most befitting activity and invokes the same:
1. Call a number
Intent callNumber = new Intent();
callNumber.setAction(android.content.Intent.ACTION_CALL);
callNumber.setData(Uri.parse("tel:9440012345"));
startActivity(callNumber);
This will call the number 9440012345. For calling custom numbers, you could provide the user with a edit text field from which you can access the number and set it to the data above instead of a hard-coded number.
2. Browse the web for a given url:
Intent searchGivenText = new Intent(Intent.ACTION_WEB_SEARCH);
searchGivenText.putExtra(SearchManager.QUERY, "Android Examples);
startActivity(searchGivenText);
This will invoke the Google search engine to search the string "Android Examples" and return the results to you. This too can be generalized to accept a string from the user and set to the intent before starting the activity.
3. View google maps for a given location
Intent searchAddress = new Intent(Intent.ACTION_VIEW,
Uri.parse("geo:0,0?q=Bangalore"));
startActivity(searchAddress);
This shows the location of Bangalore on Google Maps.
4. View Contacts on the phone
Intent contacts = new Intent();
contacts.setAction(android.content.Intent.ACTION_VIEW);
contacts.setData(People.CONTENT_URI);
startActivity(contacts);
I have created an Android eclipse project which showcases all of these examples by taking inputs from the end user. You can access the same here.
Android Activating components :intents
Android Activating components :intents
Content providers are activated when they're targeted by a request from a ContentResolver. The other three components — activities, services, and broadcast receivers — are activated by asynchronous messages called intents. An intent is an Intent object that holds the content of the message. For activities and services, it names the action being requested and specifies the URI of the data to act on, among other things. For example, it might convey a request for an activity to present an image to the user or let the user edit some text. For broadcast receivers, the Intent object names the action being announced. For example, it might announce to interested parties that the camera button has been pressed.
There are separate methods for activiating each type of component:
a)An activity is launched (or given something new to do) by passing an Intent object to Context.startActivity() or Activity.startActivityForResult(). The responding activity can look at the initial intent that caused it to be launched by calling its getIntent() method. Android calls the activity's onNewIntent() method to pass it any subsequent intents.
One activity often starts the next one. If it expects a result back from the activity it's starting, it calls startActivityForResult() instead of startActivity(). For example, if it starts an activity that lets the user pick a photo, it might expect to be returned the chosen photo. The result is returned in an Intent object that's passed to the calling activity's onActivityResult() method.
b)A service is started (or new instructions are given to an ongoing service) by passing an Intent object to Context.startService(). Android calls the service's onStart() method and passes it the Intent object.
Similarly, an intent can be passed to Context.bindService() to establish an ongoing connection between the calling component and a target service. The service receives the Intent object in an onBind() call. (If the service is not already running, bindService() can optionally start it.) For example, an activity might establish a connection with the music playback service mentioned earlier so that it can provide the user with the means (a user interface) for controlling the playback. The activity would call bindService() to set up that connection, and then call methods defined by the service to affect the playback.
A later section, Remote procedure calls, has more details about binding to a service.
c)An application can initiate a broadcast by passing an Intent object to methods like Context.sendBroadcast(), Context.sendOrderedBroadcast(), and Context.sendStickyBroadcast() in any of their variations. Android delivers the intent to all interested broadcast receivers by calling their onReceive() methods.
Content providers are activated when they're targeted by a request from a ContentResolver. The other three components — activities, services, and broadcast receivers — are activated by asynchronous messages called intents. An intent is an Intent object that holds the content of the message. For activities and services, it names the action being requested and specifies the URI of the data to act on, among other things. For example, it might convey a request for an activity to present an image to the user or let the user edit some text. For broadcast receivers, the Intent object names the action being announced. For example, it might announce to interested parties that the camera button has been pressed.
There are separate methods for activiating each type of component:
a)An activity is launched (or given something new to do) by passing an Intent object to Context.startActivity() or Activity.startActivityForResult(). The responding activity can look at the initial intent that caused it to be launched by calling its getIntent() method. Android calls the activity's onNewIntent() method to pass it any subsequent intents.
One activity often starts the next one. If it expects a result back from the activity it's starting, it calls startActivityForResult() instead of startActivity(). For example, if it starts an activity that lets the user pick a photo, it might expect to be returned the chosen photo. The result is returned in an Intent object that's passed to the calling activity's onActivityResult() method.
b)A service is started (or new instructions are given to an ongoing service) by passing an Intent object to Context.startService(). Android calls the service's onStart() method and passes it the Intent object.
Similarly, an intent can be passed to Context.bindService() to establish an ongoing connection between the calling component and a target service. The service receives the Intent object in an onBind() call. (If the service is not already running, bindService() can optionally start it.) For example, an activity might establish a connection with the music playback service mentioned earlier so that it can provide the user with the means (a user interface) for controlling the playback. The activity would call bindService() to set up that connection, and then call methods defined by the service to affect the playback.
A later section, Remote procedure calls, has more details about binding to a service.
c)An application can initiate a broadcast by passing an Intent object to methods like Context.sendBroadcast(), Context.sendOrderedBroadcast(), and Context.sendStickyBroadcast() in any of their variations. Android delivers the intent to all interested broadcast receivers by calling their onReceive() methods.
Subscribe to:
Posts (Atom)