Android Notification通知

来源:互联网 发布:网络机顶盒能收多少台 编辑:程序博客网 时间:2024/06/06 01:46

Android Notification通知

Notification可以在通知栏中显示一个通知。

1、通知服务

NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

2、创建通知

Notification.Builder类可以创建一个通知。
通知包含两部分,一个状态条图标和一个通知栏信息。

Notification.Builder builder = new Notification.Builder(this);builder.setSmallIcon(R.drawable.icon_marka);builder.setTicker("您有条新短消息");builder.setContentTitle("标题");builder.setContentText("内容");builder.setContentInfo("消息");builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher));nm.notify(R.drawable.icon_marka, builder.build());

显示状态条


显示通知栏


3、取消通知

nm.cancel(R.drawable.icon_marka);

4、事件

Builder setContentIntent(PendingIntent intent) // 点击通知栏Builder setDeleteIntent(PendingIntent intent) // 删除通知栏
点击通知栏,广播ACTION_CLICK_EVENT
Intent intent = new Intent(ACTION_CLICK_EVENT);PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);builder.setContentIntent(pendingIntent);
删除通知栏,广播ACTION_DELETE_EVENT
Intent intent = new Intent(ACTION_DELETE_EVENT);PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);builder.setDeleteIntent(pendingIntent);
注册广播接收器
private static final String ACTION_DELETE_EVENT = "com.blog.demo.DELETE_EVENT";private static final String ACTION_CLICK_EVENT = "com.blog.demo.CLICK_EVENT";private BroadcastReceiver mReciver = new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {if (ACTION_CLICK_EVENT.equals(intent.getAction())) {Toast.makeText(NotificationActivity.this, "click event",Toast.LENGTH_LONG).show();} else if (ACTION_DELETE_EVENT.equals(intent.getAction())) {Toast.makeText(NotificationActivity.this, "delete event",Toast.LENGTH_LONG).show();}}};@Overrideprotected void onResume() {super.onResume();IntentFilter filter = new IntentFilter();filter.addAction(ACTION_DELETE_EVENT);filter.addAction(ACTION_CLICK_EVENT);registerReceiver(mReciver, filter);}@Overrideprotected void onPause() {super.onPause();unregisterReceiver(mReciver);}

5、通知栏方法

(1) 时间显示
Builder.setWhen(long when) // 设置显示的时间Builder.setShowWhen(boolean show) // 是否显示时间
(2) 携带效果
Builder.setSound(Uri sound) // 声音效果Builder.setVibrate(long[] pattern) // 震动效果Builder.setLights(@ColorInt int argb, int onMs, int offMs) // 灯光效果
(3) 其他
Builder.setOngoing(boolean ongoing) // 持续存在

6、自定义remoteview

RemoteViews remoteViews = new RemoteViews("com.blog.demo",R.layout.listview_item_customdapter);remoteViews.setTextViewText(R.id.tv_name, "Jack");remoteViews.setTextViewText(R.id.tv_address, "BeiJing");builder.setContent(remoteViews);
布局文件
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="40dp"    android:background="#ffa6a5aa">    <TextView        android:id="@+id/tv_name"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerVertical="true"        android:layout_alignParentLeft="true"        android:layout_marginLeft="15dp"        android:textSize="16sp"        android:textColor="#ffffffff" />    <TextView        android:id="@+id/tv_address"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerVertical="true"        android:layout_alignParentRight="true"        android:layout_marginRight="15dp"        android:textSize="16sp"        android:textColor="#ffcccccc" /></RelativeLayout>

显示如下

0 0
原创粉丝点击