Temporary Text using Toast and Notifications ("status" bar)
Using Toast to create Temporary Text
- android.widget.Toast
- used to send temporary messages that appear on the GUI for a few seconds and then disappears.
Example:
Toast.makeText(this, "the string", option).show(); //makeText creates the test and show makes it visible
See example in Try it Out: Linking Activities Using Intents p.g 53-58 (Books/Android/Source Code/UsingIntents)
Notifications
- message in "status" bar at top of the Application window.
- Example Try it Out: Displaying Notifications p.g 98-100 (Books/Android/Source Code/Notifications)
After Application Launch displaying NotificationsActivity GUI
|
After button hit, a Notification is created and displayed (nm.notify) and an Intent is generated and used to start the NotivicationView Activity
|
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.learn2develop.Notifications"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="14" /> <uses-permission android:name="android.permission.VIBRATE"/> <application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".NotificationsActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".NotificationView"
android:label="Details of notification">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest> |
main.xml --GUI for NotificationsActivity <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button
android:id="@+id/btn_displaynotif"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Display Notification"
android:onClick="onClick"/>
</LinearLayout> |
NotificationsActivity package net.learn2develop.Notifications; 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;
public class NotificationsActivity extends Activity {
int notificationID = 1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void onClick(View view) { //when click button call displayNotification
displayNotification();
}
protected void displayNotification()
{
//---PendingIntent to launch activity if the user selects
// this notification---
Intent i = new Intent(this, NotificationView.class);
i.putExtra("notificationID", notificationID);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, i, 0);
//grab NotificationManager from the system
NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); //create instance of Notification with information with logo in res/drawable-*/ic_launcher.png (R.drawable.ic_launcher) Notification notif = new Notification( R.drawable.ic_launcher, "Reminder: Meeting starts in 5 minutes", System.currentTimeMillis()); //Setup Notification details that will be displayed whenever the user clicks on the
//---100ms delay, vibrate for 250ms, pause for 100 ms and // then vibrate for 500ms--- notif.vibrate = new long[] { 100, 250, 100, 500}; nm.notify(notificationID, notif); //this displays the notification //NOTE: when user clicks on the Notification in the GUI, it will bring up the pending // NotificationView Activity that was set at start of this method....that activity will // cancel the notification. } } |
notification.xml - GUI for NotificationView <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Here are the details for the notification..." /> </LinearLayout> |
NotificationView package net.learn2develop.Notifications; import android.app.Activity;
import android.app.NotificationManager;
import android.os.Bundle;
public class NotificationView extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.notification);
//---look up the notification manager service---
NotificationManager nm = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
//---cancel the notification that we started---
nm.cancel(getIntent().getExtras().getInt("notificationID"));
}
}
|


