5.7.1 Toast提醒你
2011-12-30 11:34 徐娜子 電子工業出版社 我要評論(0) 字号: T | T
綜合評級:
想讀(0) 在讀(0) 已讀(0) 品書齋鑒(0) 已有0人發表書評
《Android江湖》第5章系出名門,在本章内容中講解了Android中基本控件的基本使用知識。首先介紹了UI等常用布局控件的基本知識和用法,然後詳細講解了友好界面控件和清單控件的的基本知識,最後講解了Intent、Activity、Toast和Notification控件的基本知識。本節為大家介紹Toast提醒你。
AD:51CTO雲計算架構師峰會 搶票進行中!
5.7 Toast和Notification控件實作提醒
生活中有提醒,Android中也有提醒。在Android中,可以通過Toast和Notification控件來實作提醒功能。和Dialog相比,此類型提醒更加友好和溫馨,并且不會打斷使用者的目前操作。在本節内容中将詳細講解Toast和Notification控件的具體使用方法。
5.7.1 Toast提醒你
Toast是Android 中用來顯示資訊的一種機制,和Dialog不同的是,Toast沒有焦點,而且Toast 顯示的時間有限,經過一定的時間後就會自動消失。
5.7.2 Notification提醒你
2011-12-30 11:34 徐娜子 電子工業出版社 我要評論(0) 字号: T | T
綜合評級:
想讀(0) 在讀(0) 已讀(0) 品書齋鑒(0) 已有0人發表書評
《Android江湖》第5章系出名門,在本章内容中講解了Android中基本控件的基本使用知識。首先介紹了UI等常用布局控件的基本知識和用法,然後詳細講解了友好界面控件和清單控件的的基本知識,最後講解了Intent、Activity、Toast和Notification控件的基本知識。本節為大家介紹Notification提醒你。
AD:51CTO雲計算架構師峰會 搶票進行中!
5.7.2 Notification提醒你
看名字就知道,Notification和提醒有關,它通常和NotificationManager同時使用。具體來說,其主要功能如下:
1.NotificationManager和Notification用來設定通知
通知的設定等操作相對比較簡單,基本的使用方式就是用建立一個Notification對象,然後設定好通知的各項參數,然後使用系統背景運作的NotificationManager服務将通知發出來。基本步驟如下:
(1)得到NotificationManager:
- String ns = Context.NOTIFICATION_SERVICE;
- NotificationManager mNotificationManager = (NotificationManager)
- getSystemService(ns);
(2)建立一個新的Notification對象:
- Notification notification = new Notification();
- notification.icon = R.drawable.notification_icon;
也可以使用稍微複雜一些的方式建立Notification:
- int icon = R.drawable.notification_icon; //通知圖示
- CharSequence tickerText = "Hello"; //狀态欄(Status Bar)顯示的通知文本提示
- long when = System.currentTimeMillis(); //通知産生的時間,會在通知資訊裡顯示
- Notification notification = new Notification(icon, tickerText, when);
(3)填充Notification的各個屬性:
- Context context = getApplicationContext();
- CharSequence contentTitle = "My notification";
- CharSequence contentText = "Hello World!";
- Intent notificationIntent = new Intent(this, MyClass.class);
- PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
- notificationIntent, 0);
- notification.setLatestEventInfo(context, contentTitle, contentText,
- contentIntent);
Notification提供了如下幾種手機提示方式:
在狀态欄(Status Bar)顯示的通知文本提示,例如:notification.tickerText = "hello";
發出提示音,例如:
- notification.defaults |= Notification.DEFAULT_SOUND;
- notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");
- notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_
- URI, "6");
手機振動,例如:
- notification.defaults |= Notification.DEFAULT_VIBRATE;
- long[] vibrate = {0,100,200,300};
- notification.vibrate = vibrate;
LED燈閃爍,例如:
- notification.defaults |= Notification.DEFAULT_LIGHTS;
- notification.ledARGB = 0xff00ff00;
- notification.ledOnMS = 300;
- notification.ledOffMS = 1000;
- notification.flags |= Notification.FLAG_SHOW_LIGHTS;
(4)發送通知,代碼如下:
- private static final int ID_NOTIFICATION = 1;
- mNotificationManager.notify(ID_NOTIFICATION, notification);
2.更新通知
如果需要更新一個通知,則隻需要在設定好notification之後,再調用setLatestEventInfo,然後重新發送一次通知即可。
為了更新一個已經觸發過的Not i f icat ion,傳入相同的ID,此時可以傳入相同的Notification對象,也可以是一個全新的對象。隻要ID相同,新的Notification對象會替換狀态條圖示和擴充的狀态視窗的細節。
另外,還可以使用ID來取消Notification,通過調用NotificationManager的cancel方法,例如:
- notificationManager.cancel(notificationRef);
當取消一個Notification時,會移除它的狀态條圖示以及清除在擴充的狀态視窗中的資訊。
5.7.3 練習Toast和Notification(1)
2011-12-30 11:34 徐娜子 電子工業出版社 我要評論(0) 字号: T | T
綜合評級:
想讀(0) 在讀(0) 已讀(0) 品書齋鑒(0) 已有0人發表書評
《Android江湖》第5章系出名門,在本章内容中講解了Android中基本控件的基本使用知識。首先介紹了UI等常用布局控件的基本知識和用法,然後詳細講解了友好界面控件和清單控件的的基本知識,最後講解了Intent、Activity、Toast和Notification控件的基本知識。本節為練習Toast和Notification。
AD:51CTO雲計算架構師峰會 搶票進行中!
5.7.3 練習Toast和Notification(1)
練習12:使用Toast和Notification實作提醒功能效果的的具體使用流程源碼路徑:“第5章\widges”檔案夾
(1)建立一個Android工程檔案,然後編寫main.xml布局檔案,具體代碼如下所示:
- <?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">
- <Button Android:id="@+id/button1"
- Android:layout_width="wrap_content"
- Android:layout_height="wrap_content" Android:text="介紹Notification"
- />
- <Button Android:id="@+id/button2"
- Android:layout_width="wrap_content"
- Android:layout_height="wrap_content" Android:text="介紹Toast" />
- </LinearLayout>
通過上述代碼插入了兩個Button按鈕,執行後的效果如圖5-40所示。
圖5-40 插入兩個Button |
(2)編寫處理檔案ActivityMain.java,具體代碼如下所示:
- public class ActivityMain extends Activity {
- OnClickListener listener1 = null;
- OnClickListener listener2 = null;
- Button button1;
- Button button2;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- listener1 = new OnClickListener() {
- public void onClick(View v) {
- setTitle(“這是Notification");
- Intent intent = new Intent(ActivityMain.this,
- ActivityMainNotification.class);
- startActivity(intent);
- }
- };
- listener2 = new OnClickListener() {
- public void onClick(View v) {
- setTitle("這是Toast");
- Intent intent = new Intent(ActivityMain.this,
- ActivityToast.class);
- startActivity(intent);
- }
- };
- setContentView(R.layout.main);
- button1 = (Button) findViewById(R.id.button1);
- button1.setOnClickListener(listener1);
- button2 = (Button) findViewById(R.id.button2);
- button2.setOnClickListener(listener2);
- }
- }
在上述代碼中,對兩個Button綁定了單擊監聽器OnClickListener,當單擊這兩個Button時,會跳轉到新的Activity上。
(3)編寫第一個Button的處理程式,即單擊圖5-21中的“介紹Notification”按鈕後,執行ActivityMainNotification.java,其主要代碼如下所示:
- public class ActivityMainNotification extends Activity {
- private static int NOTIFICATIONS_ID = R.layout.activity_notification;
- private NotificationManager mNotificationManager;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_notification);
- Button button;
- mNotificationManager = (NotificationManager) getSystemService
- (NOTIFICATION_ SERVICE);
- button = (Button) findViewById(R.id.sun_1);
- button.setOnClickListener(new Button.OnClickListener() {
- public void onClick(View v) {
- setWeather("好", "天氣", "好", R.drawable.sun);
- }
- });
- button = (Button) findViewById(R.id.cloudy_1);
- button.setOnClickListener(new Button.OnClickListener() {
- public void onClick(View v) {
- setWeather("還行", "天氣", "還行", R.drawable.cloudy);
- }
- });
- button = (Button) findViewById(R.id.rain_1);
- button.setOnClickListener(new Button.OnClickListener() {
- public void onClick(View v) {
- setWeather("不好", "天氣", "不好", R.drawable.rain);
- }
- });
- button = (Button) findViewById(R.id.defaultSound);
- button.setOnClickListener(new Button.OnClickListener() {
- public void onClick(View v) {
- setDefault(Notification.DEFAULT_SOUND);
- }
- });
- button = (Button) findViewById(R.id.defaultVibrate);
- button.setOnClickListener(new Button.OnClickListener() {
- public void onClick(View v) {
- setDefault(Notification.DEFAULT_VIBRATE);
- }
- });
- button = (Button) findViewById(R.id.defaultAll);
- button.setOnClickListener(new Button.OnClickListener() {
- public void onClick(View v) {
- setDefault(Notification.DEFAULT_ALL);
- }
- });
- button = (Button) findViewById(R.id.clear);
- button.setOnClickListener(new Button.OnClickListener() {
- public void onClick(View v) {
- mNotificationManager.cancel(NOTIFICATIONS_ID);
- }
- });
- }
- private void setWeather(String tickerText, String title, String content,
- int drawable) {
- Notification notification = new Notification(drawable, tickerText,
- System.currentTimeMillis());
- PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
- new Intent(this, ActivityMain.class), 0);
- notification.setLatestEventInfo(this, title, content, contentIntent);
- mNotificationManager.notify(NOTIFICATIONS_ID, notification);
- }
- private void setDefault(int defaults) {
- PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
- new Intent(this, ActivityMain.class), 0);
- String title = "天氣預報";
- String content = "晴";
- final Notification notification = new Notification(R.drawable.sun,
- content, System.currentTimeMillis());
- notification.setLatestEventInfo(this, title, content, contentIntent);
- notification.defaults = defaults;
- mNotificationManager.notify(NOTIFICATIONS_ID, notification);
- }
- }
5.7.3 練習Toast和Notification(2)
2011-12-30 11:34 徐娜子 電子工業出版社 我要評論(0) 字号: T | T
綜合評級:
想讀(0) 在讀(0) 已讀(0) 品書齋鑒(0) 已有0人發表書評
《Android江湖》第5章系出名門,在本章内容中講解了Android中基本控件的基本使用知識。首先介紹了UI等常用布局控件的基本知識和用法,然後詳細講解了友好界面控件和清單控件的的基本知識,最後講解了Intent、Activity、Toast和Notification控件的基本知識。本節為練習Toast和Notification。
AD:51CTO雲計算架構師峰會 搶票進行中!
5.7.3 練習Toast和Notification(2)
因為所有的Notification都是通過NotificationManager來管理的,是以應該首先得到NotificationManager執行個體,以便管理這個Activity中的跳轉服務。擷取NotificationManager執行個體的代碼如下所示:
mNotificationManager=(NotificationManager)getSystemService(NOTIFICATION_
SERVICE);
函數setWeather是ActivityMainNotification中的一個重要函數,它執行個體化了一個Notification,并将這個Notification顯示出來。在下面的代碼中:
Notification notification = new Notification(drawable, tickerText,
System.currentTimeMillis());
包含了3個參數,具體說明如下:
第1個:要顯示的圖檔的ID;
第2個:顯示的文本文字;
第3個: N o t i f i c a t i o n 顯示的時間, 一般是立即顯示, 時間就是S y s t e m .currentTimeMillis()。
函數setDefault也是ActivityMainNotification.java中的一個重要函數,在此函數中初始化了一個Notification,在設定Notification時使用了其預設值的形式,即:
notification.defaults = defaults;
另外,在上述程式中還用到了以下幾種表現形式:
Notification.DEFAULT_VIBRATE:表示目前的Notification
顯示出來時手機會發出震動;
Notification.DEFAULT_SOUND:表示目前的Notification顯示出來時手機會伴随音樂;
Notification.DEFAULT_ALL:表示目前的Notification顯示出來時手機既會震動,也會伴随音樂。
這樣當單擊第一個Button後會執行上述處理程式,來到對應的新界面,新界面的布局檔案是由activity_notification.xml實作的,其主要代碼如下所示:
- <?xml version="1.0" encoding="utf-8"?>
- <ScrollView xmlns:Android="http://schemas.Android.com/apk/res/Android"
- Android:layout_width="fill_parent"
- Android:layout_height="fill_parent">
- <LinearLayout
- Android:orientation="vertical"
- Android:layout_width="fill_parent"
- Android:layout_height="wrap_content">
- <LinearLayout
- Android:orientation="vertical"
- Android:layout_width="fill_parent"
- Android:layout_height="wrap_content">
- <Button
- Android:id="@+id/sun_1"
- Android:layout_width="wrap_content"
- Android:layout_height="wrap_content"
- Android:text="适合" />
- <Button
- Android:id="@+id/cloudy_1"
- Android:layout_width="wrap_content"
- Android:layout_height="wrap_content"
- Android:text="一般" />
- <Button
- Android:id="@+id/rain_1"
- Android:layout_width="wrap_content"
- Android:layout_height="wrap_content"
- Android:text="一點也不适合" />
- </LinearLayout>
- <TextView
- Android:layout_width="wrap_content"
- Android:layout_height="wrap_content"
- Android:layout_marginTop="20dip"
- Android:text="進階notification" />
- <LinearLayout
- Android:orientation="vertical"
- Android:layout_width="fill_parent"
- Android:layout_height="wrap_content">
- <Button
- Android:id="@+id/defaultSound"
- Android:layout_width="wrap_content"
- Android:layout_height="wrap_content"
- Android:text="有聲音的提示" />
- <Button
- Android:id="@+id/defaultVibrate"
- Android:layout_width="wrap_content"
- Android:layout_height="wrap_content"
- Android:text="振動的提示" />
- <Button
- Android:id="@+id/defaultAll"
- Android:layout_width="wrap_content"
- Android:layout_height="wrap_content"
- Android:text="聲音+振動的提示" />
- </LinearLayout>
- <Button Android:id="@+id/clear"
- Android:layout_width="wrap_content"
- Android:layout_height="wrap_content"
- Android:layout_marginTop="20dip"
- Android:text="清除提示" />
- </LinearLayout>
- </ScrollView>
5.7.3 練習Toast和Notification(3)
2011-12-30 13:44 徐娜子 電子工業出版社 我要評論(0) 字号: T | T
綜合評級:
想讀(0) 在讀(0) 已讀(0) 品書齋鑒(0) 已有0人發表書評
《Android江湖》第5章系出名門,在本章内容中講解了Android中基本控件的基本使用知識。首先介紹了UI等常用布局控件的基本知識和用法,然後詳細講解了友好界面控件和清單控件的的基本知識,最後講解了Intent、Activity、Toast和Notification控件的基本知識。本節為大家介紹練習Toast和Notification。
AD:51CTO雲計算架構師峰會 搶票進行中!
5.7.3 練習Toast和Notification(3)
程式執行後新界面的效果如圖5-41所示。
當單擊圖5-41所示的新界面中的Button後,會根據上述處理檔案而實作某種效果,例如,單擊“有聲音的提示”按鈕後,會發出聲音。
(4)編寫第二個Button的處理程式,即單擊圖5-41中的“振動的提示”按鈕後,執行ActivityToast.java,其主要代碼如下所示:
圖5-41 運作效果 |
- public class ActivityToast extends Activity {
- OnClickListener listener1 = null;
- OnClickListener listener2 = null;
- Button button1;
- Button button2;
- private static int NOTIFICATIONS_ID = R.layout.activity_toast;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- listener1 = new OnClickListener() {
- public void onClick(View v) {
- setTitle("短時提醒");
- showToast(Toast.LENGTH_SHORT);
- }
- };
- listener2 = new OnClickListener() {
- public void onClick(View v) {
- setTitle("長時提醒");
- showToast(Toast.LENGTH_LONG);
- showNotification();
- }
- };
- setContentView(R.layout.activity_toast);
- button1 = (Button) findViewById(R.id.button1);
- button1.setOnClickListener(listener1);
- button2 = (Button) findViewById(R.id.button2);
- button2.setOnClickListener(listener2);
- }
- protected void showToast(int type) {
- View view = inflateView(R.layout.toast);
- TextView tv = (TextView) view.findViewById(R.id.content);
- tv.setText("歡迎來到濟南,");
- /*執行個體化Toast*/
- Toast toast = new Toast(this);
- toast.setView(view);
- toast.setDuration(type);
- toast.show();
- }
- private View inflateView(int resource) {
- LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_
- INFLATER_SERVICE);
- return vi.inflate(resource, null);
- }
- protected void showNotification() {
- NotificationManager notificationManager = (NotificationManager)
- getSystemService(NOTIFICATION_SERVICE);
- CharSequence title = "省會";
- CharSequence contents = "齊魯大地";
- PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
- new Intent(this, ActivityMain.class), 0);
- Notification notification = new Notification(R.drawable.default_icon,
- title, System.currentTimeMillis());
- notification.setLatestEventInfo(this, title, contents, contentIntent);
- // 100ms延遲後振動250ms,停止100ms後振動500ms
- notification.vibrate = new long[] { 100, 250, 100, 500 };
- notificationManager.notify(NOTIFICATIONS_ID, notification);
- }
- }
5.7.3 練習Toast和Notification(4)
2011-12-30 13:44 徐娜子 電子工業出版社 我要評論(0) 字号: T | T
綜合評級:
想讀(0) 在讀(0) 已讀(0) 品書齋鑒(0) 已有0人發表書評
《Android江湖》第5章系出名門,在本章内容中講解了Android中基本控件的基本使用知識。首先介紹了UI等常用布局控件的基本知識和用法,然後詳細講解了友好界面控件和清單控件的的基本知識,最後講解了Intent、Activity、Toast和Notification控件的基本知識。本節為大家介紹練習Toast和Notification。
AD:51CTO雲計算架構師峰會 搶票進行中!
5.7.3 練習Toast和Notification(4)
上述代碼是通過Toast實作的,它不需要用NotificationManager來管理,上述代碼的處理流程如下:
執行個體化Toast,每個Toast和一個View相關;
設定Toast的長短,通過“showToast(Toast.LENGTH_SHORT);”來設定Toast短時間顯示,通過“showToast(Toast.LENGTH_LONG);”來設定Toast長時間顯示;
通過函數showToast來顯示Toast,當單擊“長時顯示”按鈕後,程式會長時間顯示提醒,并用Notification在狀态欄中提示使用者;當單擊“短時顯示”按鈕後,程式會短時間顯示提醒。
這樣單擊第二個Button後會執行上述處理程式,來到對應的新界面,新界面的布局檔案是由activity_toast.xml實作的,其主要代碼如下所示:
- <?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">
- <Button Android:id="@+id/button1"
- Android:layout_width="wrap_content"
- Android:layout_height="wrap_content" Android:text="短時Toast" />
- <Button Android:id="@+id/button2"
- Android:layout_width="wrap_content"
- Android:layout_height="wrap_content" Android:text="長時Toast" />
- </LinearLayout>
程式執行後新界面的效果如圖5-42所示。
圖5-42 新界面的效果 |
當單擊圖5-42所示的新界面中的Button後,會根據上述處理檔案而實作某種效果,例如,單擊“短時Toast”按鈕後,會短時間顯示一個提醒;當單擊“長時Toast”按鈕後,會長時間顯示一個提醒。這兩種方式的提醒界面都是相同的,具體效果如圖5-43所示。