Android Api Demos登顶之路(五十一)Notification-->Status Bar
来源:互联网 发布:双色球绝密算法 编辑:程序博客网 时间:2024/06/05 19:12
这个demo演示了notification的不同的显示方式,以及自定义消息的视图、设置默认的状态
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="5dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Icons only" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/icon_only" android:text="Icon Only"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Icons and marquee" android:layout_marginTop="10dp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/icon_marquee" android:text="Icon And Marquee"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Use remote views in balloon" android:layout_marginTop="10dp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/icon_remoteview" android:text="Use RemoteView"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Use default values where applicable" android:layout_marginTop="10dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/default_sound" android:text="Sound"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/default_vibrate" android:text="Vibrate"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/default_all" android:text="All"/> </LinearLayout> <Button android:layout_marginTop="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/clear" android:text="Clear Notification"/></LinearLayout>
RemoteView的布局文件:notification_view.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageView android:layout_width="60dp" android:layout_height="60dp" android:id="@+id/icon"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/text" android:layout_gravity="center_vertical" android:text="hello" android:layout_marginLeft="10dp"/></LinearLayout>
MainActivity
public class MainActivity extends Activity implements OnClickListener { private NotificationManager nm; private static final int NOTIFICATION_ID = 0x110; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button icon_only = (Button) findViewById(R.id.icon_only); Button icon_marquee = (Button) findViewById(R.id.icon_marquee); Button icon_remoteview = (Button) findViewById(R.id.icon_remoteview); Button default_sound = (Button) findViewById(R.id.default_sound); Button default_vibrate = (Button) findViewById(R.id.default_vibrate); Button default_all = (Button) findViewById(R.id.default_all); Button clear = (Button) findViewById(R.id.clear); icon_only.setOnClickListener(this); icon_marquee.setOnClickListener(this); icon_remoteview.setOnClickListener(this); default_sound.setOnClickListener(this); default_vibrate.setOnClickListener(this); default_all.setOnClickListener(this); clear.setOnClickListener(this); nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.icon_only: showNotification(R.drawable.ic_launcher, "摘要信息", false); break; case R.id.icon_marquee: showNotification(R.drawable.ic_launcher, "摘要信息", true); break; case R.id.icon_remoteview: showViewNotification(R.drawable.icon48x48_2, "摘要信息"); break; case R.id.default_sound: showNotificationDefalut(Notification.DEFAULT_SOUND); break; case R.id.default_vibrate: showNotificationDefalut(Notification.DEFAULT_VIBRATE); break; case R.id.default_all: showNotificationDefalut(Notification.DEFAULT_ALL); break; case R.id.clear: nm.cancel(NOTIFICATION_ID); break; } } private void showNotification(int iconId, String text, boolean showTricker) { PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent( this, NotificationDisplay.class), PendingIntent.FLAG_CANCEL_CURRENT); String trickerText=showTricker ? "显示提示信息":null; Notification.Builder builder=new Builder(this); builder.setTicker(trickerText) .setSmallIcon(iconId) .setContentIntent(contentIntent) .setContentTitle("Notification") .setContentText(text) .setWhen(System.currentTimeMillis()); Notification noti=builder.build(); nm.notify(NOTIFICATION_ID, noti); } private void showViewNotification(int iconId, String text) { PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent( this, NotificationDisplay.class), PendingIntent.FLAG_CANCEL_CURRENT); /*RemoteViews描述一个跨进程显示的View在对 notification的View进行自定义时就必须 * 使用这个类。因为notification的view进程与显示notification的进程是两个不同的进程。 * 使用RemoteViews必须首先定义一个layout的布局文件*/ RemoteViews rv=new RemoteViews(getPackageName(), R.layout.notification_view); //对RemoteViews中的控件进行简单的赋值 rv.setImageViewResource(R.id.icon, iconId); rv.setTextViewText(R.id.text, text); Notification.Builder builder=new Builder(this); builder.setTicker(text) .setSmallIcon(R.drawable.ic_launcher) .setContentIntent(contentIntent) .setContent(rv); Notification noti=builder.build(); nm.notify(NOTIFICATION_ID, noti); } private void showNotificationDefalut(int defaluts) { PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent( this, NotificationDisplay.class), PendingIntent.FLAG_CANCEL_CURRENT); String trickerText= "显示提示信息"; Notification.Builder builder=new Builder(this); builder.setTicker(trickerText) .setSmallIcon(R.drawable.ic_launcher) .setContentIntent(contentIntent) .setContentTitle("Notification") .setContentText("Summary info") .setWhen(System.currentTimeMillis()) .setDefaults(defaluts); Notification noti=builder.build(); nm.notify(NOTIFICATION_ID, noti); }}
在本例中只是使用了一个最简单的textView Activity,这里不再贴代码。
配置文件中配置这个Activity。
0 0
- Android Api Demos登顶之路(五十一)Notification-->Status Bar
- Android Api Demos登顶之路(二十五)Action Bar Mechanics
- Android Api Demos登顶之路(二十六)Action Bar Tabs
- Android Api Demos登顶之路(二十七)Action Bar Usage
- Android Api Demos登顶之路(四十九)Notification-->Incoming Message
- Android Api Demos登顶之路(五十)Notification-->Service Controller
- Android Api Demos登顶之路(二)
- Android Api Demos登顶之路(三)
- Android Api Demos登顶之路(四)
- Android Api Demos登顶之路(五)
- Android Api Demos登顶之路(六)
- Android Api Demos登顶之路(七)
- Android Api Demos登顶之路(八)
- Android Api Demos登顶之路(九)
- Android Api Demos登顶之路(十)Overscan
- Android Api Demos登顶之路(十一)Persistent state
- Android Api Demos登顶之路(十二)Presentation
- Android Api Demos登顶之路(十四)Quick Contacts
- 【LeetCode-面试算法经典-Java实现】【204-Count Primes(统计质数)】
- 【LeetCode-面试算法经典-Java实现】【205-Isomorphic Strings(同构字符串)】
- 【LeetCode-面试算法经典-Java实现】【206-Reverse Linked List(反转一个单链表)】
- [8月]总结
- PS学习笔记-----选区的作用即是:用来控制操作的范围
- Android Api Demos登顶之路(五十一)Notification-->Status Bar
- Spring的 depends-on
- Android布局特效(二)
- 短信里的陌生链接千万不要点! 点了就会中招
- 每天5道面试题(四)java基础
- watch postgres's source code commit
- PS学习笔记-----钢笔工具
- git使用(持续更新)
- 异常值