7.4 Android Basic UI的布局 Notification&Toast
Notificaton和Toast演示
-
新建项目 ToastNotificationDemo,修改res/layout/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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Toast 和 Notification 演示"
/>
<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>
2. 在res/layout 下新建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="发声的notification" />
<Button
android:id="@+id/defaultVibrate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="振动的notification" />
<Button
android:id="@+id/defaultAll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="既发声又振动的notification" />
</LinearLayout>
<Button android:id="@+id/clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:text="清除notification" />
</LinearLayout>
</ScrollView>
3. 新建类NotificationActivity 继承自Activity:
public class NotificationActivity extends Activity {
private static int NOTIFICATIONS_ID = R.layout.notification;
private NotificationManager notificationManager;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.notification);
// get Notification
notificationManager= (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
button = (Button)findViewById(R.id.sun_1);
button.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
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) {
notificationManager.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,ToastNotificationDemo.class), 0);
notification.setLatestEventInfo(this, title, content, contentIntent);
notificationManager.notify(NOTIFICATIONS_ID, notification);
}
private void setDefault(int defaults){
String title = "天气预报";
String content="晴空万里晴空万里晴空万里";
Notification notification = new Notification(R.drawable.sun,content,System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this,ToastNotificationDemo.class), 0);
notification.setLatestEventInfo(this, title, content, contentIntent);
notification.defaults = defaults;
notificationManager.notify(NOTIFICATIONS_ID, notification);
}
}
使用getSystemService方法获得Notification
setWeather 方法实例化Notification,
Notification notification = new Notification(drawable,tickerText,System.currentTimeMillis());
第一个参数是要显示的图片id,第二个参数是显示的文字,第三个参数是显示的时间。System.currenttimeMillis()是获取系统的当前时间。
notificationManager.notify(NOTIFICATIONS_ID, notification);
使用notify方法显示Nofification.
setLastestEventInfo方法设置,当Notification列表的时候如何呈现Notification,另一方面设置当单击Notification时候,如何处理单击。
setDefault()函数里
Notification.DEFAULT_VIBRATE , 表示显示Notification的时候振动。
Notification.DEFAULT_SOUND, 表示显示Notification的时候播放音乐。
Notification.DEFAULT_ALL, 表示显示Notification的时候振动播放音乐。
notificationManager.cancel(NOTIFICATIONS_ID); 取消显示
4. Toast演示,在res/layout 新建toastactivity.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. 新建ToastActivity类继承自 Activity类:
public class ToastActivity extends Activity {
Button button1;
Button button2;
private static int NOTIFICATIONS_ID = R.layout.toastactivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.toastactivity);
button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
setTitle("短时间显示Toast");
showToast(Toast.LENGTH_SHORT);
}
});
button2 = (Button)findViewById(R.id.button2);
button2.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
setTitle("长时间显示Toast");
showToast(Toast.LENGTH_LONG);
showNotification();
}
});
}
private void showToast(int type){
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.toast, null);
TextView textView = (TextView)view.findViewById(R.id.content);
textView.setText("演示Toast的效果!演示Toast的效果!演示Toast的效果!");
Toast toast = new Toast(this);
toast.setDuration(type);
toast.setView(view);
toast.show();
}
private void showNotification(){
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
CharSequence title = "NotificationTitle";
CharSequence contents = "notification 内容内容";
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, ToastNotificationDemo.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);
}
}
6. 配置 AndroidManifest.xml
配置Activity
<activity android:name=".NotificationActivity"></activity>
<activity android:name=".ToastActivity"></activity>
配置振动
<uses-permission android:name="android.permission.VIBRATE"></uses-permission>
Toast虽然也是提醒用户的一种方式,Nofitication需要被NotificationManger管理。Toas是作为Android中的Widget存在的。
Toast toast = new Toast(this) 实例化toast。
每个Toast的实例都可以和一个View相关,使用setView方法。
Toast显示时间有长有短。toast.setDuration(Toast.LENGTH_LONG) toast.setDuration(Toast.LENGTH_SHORT)
使用show()方法显示。
设置振动的方式:
// 100ms延迟后,振动250ms,停止100ms后振动500ms
notification.vibrate = new long[] { 100, 250, 100, 500 };
im@xingquan.org