Android ApiDemos示例解析(29):App->Notification->Status Bar

這個例子的Icons Only 和 Icons and marquee 沒有什麼特別好說明的。

而Use Remote views in balloon 介紹瞭可以自定義在Extended Status bar顯示Notification的Layout,Extended Status Bar缺省顯示Notification 是一個圖標後接文字,對應大多數情況是夠用瞭。但如果有需要也可以使用自定義的Layout在Extented Status bar顯示Notification,方法是通過RemoteView:

[java] 
private void setMoodView(int moodId, int textId) { 
  
 Notification notif = new Notification(); 
  
 notif.contentIntent = makeMoodIntent(moodId); 
  
 CharSequence text = getText(textId); 
 notif.tickerText = text; 
  
 // the icon for the status bar  
 notif.icon = moodId; 
  
 // our custom view  
RemoteViews contentView = new RemoteViews(getPackageName(), 
 R.layout.status_bar_balloon); 
 contentView.setTextViewText(R.id.text, text); 
 contentView.setImageViewResource(R.id.icon, moodId); 
 contentView.setImageViewResource(R.id.icon1, moodId); 
 notif.contentView = contentView; 
  
 mNotificationManager.notify(R.layout.status_bar_notifications, 
 notif); 

private void setMoodView(int moodId, int textId) {
 
 Notification notif = new Notification();
 
 notif.contentIntent = makeMoodIntent(moodId);
 
 CharSequence text = getText(textId);
 notif.tickerText = text;
 
 // the icon for the status bar
 notif.icon = moodId;
 
 // our custom view
RemoteViews contentView = new RemoteViews(getPackageName(),
 R.layout.status_bar_balloon);
 contentView.setTextViewText(R.id.text, text);
 contentView.setImageViewResource(R.id.icon, moodId);
 contentView.setImageViewResource(R.id.icon1, moodId);
 notif.contentView = contentView;
 
 mNotificationManager.notify(R.layout.status_bar_notifications,
 notif);
}

為瞭和缺省的Status Bar Layout有所區別,我們在/res/status_bar_balloon.xml 在增加一個ImageView:左右各顯示一個圖標,中間為文字。

<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”horizontal”
android:baselineAligned=”false”
android:gravity=”center_vertical”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”>

<ImageView android:id=”@+id/icon”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginRight=”10dip” />
<TextView android:id=”@+id/text”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:textColor=”#ffffffff” />
<ImageView android:id=”@+id/icon1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginRight=”10dip” />

</LinearLayout>

 

Use default values where applicable 介紹使用缺省聲音,震動或是兩者的方法:

 

[java] 
int default = Notification.DEFAULT_SOUND; 
//Notification.DEFAULT_SOUND  
//Notification.DEFAULT_VIBRATE  
//Notification.DEFAULT_ALL  
  
notification.defaults = defaults; 
int default = Notification.DEFAULT_SOUND;
//Notification.DEFAULT_SOUND
//Notification.DEFAULT_VIBRATE
//Notification.DEFAULT_ALL
 
notification.defaults = defaults;

作者:mapdigit
 

 

發佈留言