Android API Demos学习 - Notification部分

来源:互联网 发布:linux 查cpu核心数 编辑:程序博客网 时间:2024/06/03 17:37
1. NotifyWithText
展示了Toast的使用方法:
 Toast.makeText(NotifyWithText.this, R.string.short_notification_text,                    Toast.LENGTH_SHORT).show();Toast.makeText(NotifyWithText.this, R.string.long_notification_text,                    Toast.LENGTH_LONG).show();

第一个显示时间短一些,第二个显示时间长一点。

2. IncomingMessage
展示了怎么发送一个持久和短暂的提醒,并且用xml定义提醒的样式。
protected void showToast() {        View view = inflateView(R.layout.incoming_message_panel);        TextView tv = (TextView)view.findViewById(R.id.message);        tv.setText("khtx. meet u for dinner. cul8r");        Toast toast = new Toast(this);        toast.setView(view);        toast.setDuration(Toast.LENGTH_LONG);        toast.show();    }

显示一个Toast,先创建一个视图,然后使用这个视图显示toast。
protected void showNotification() {        NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);        CharSequence from = "Joe";        CharSequence message = "kthx. meet u for dinner. cul8r";        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,                new Intent(this, IncomingMessageView.class), 0);        String tickerText = getString(R.string.imcoming_message_ticker_text, message);        Notification notif = new Notification(R.drawable.stat_sample, tickerText,                System.currentTimeMillis());        notif.setLatestEventInfo(this, from, message, contentIntent);        notif.vibrate = new long[] { 100, 250, 100, 500};        nm.notify(R.string.imcoming_message_ticker_text, notif);    }

先获得NotificationManager 对象,然后创建Notification 对象。
setLatestEventInfo设置通知的标题,内容,Intent信息。
vibrate 是震动信息,第一个100表示100毫秒后开始震动,250表示震动的时间,第三个和第一个一样,第四个和第二个一样,依次类推。
notify的第一个参数是这个提示信息的ID。可以使用它来开始一个提示或者关闭一个提示。比如在IncomingMessageView.java中:
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);nm.cancel(R.string.imcoming_message_ticker_text);


3. Status Bar
3.1 Icons only和 Icons and marquee一个是自显示图标,一个是显示图标和文字。
3.2 Use Remote views in balloon 介绍了可以自定义显示Notification的contentView。
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.status_bar_balloon);contentView.setTextViewText(R.id.text, text);contentView.setImageViewResource(R.id.icon, moodId);notif.contentView = contentView;

status_bar_balloon中定义了一个图片和一个文本:
<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" />    </LinearLayout>

3.3 Use default values where applicable 介绍使用缺省声音,震动或是两者的方法。
原创粉丝点击