标签:
Notification分为普通通知和自定义的两种,由图标、ticker(提示信息)、标题、内容、时间、事件组成,
普通通知demo:
1 package com.android.hzynotification;
2
3 import android.app.Activity;
4 import android.app.Notification;
5 import android.app.NotificationManager;
6 import android.app.PendingIntent;
7 import android.content.Context;
8 import android.content.Intent;
9 import android.os.Bundle;
10 import android.view.View;
11
12 public class MainActivity extends Activity {
13
14 @Override
15 protected void onCreate(Bundle savedInstanceState) {
16 super.onCreate(savedInstanceState);
17 setContentView(R.layout.activity_main);
18 }
19
20 public void send(View v){ // 兼容老版本(2.3)
21 // 1.得到通知管理器
22 NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
23 // 2.构建通知 图标 闪过显示的内容 接收到的时间
24 Notification notification = new Notification(android.R.drawable.stat_notify_chat, "这是提示信息", System.currentTimeMillis());
25 // 3.设置通知点击事件
26 Intent intent = new Intent(this,MainActivity.class); // 创建显式意图
27 PendingIntent contentIntent = PendingIntent.getActivity(this, 100, intent, 0); // 点击后打开的Activity也可以是Server或BoardCast
28 notification.setLatestEventInfo(this, "通知的标题", "通知的内容", contentIntent);
29
30 notification.flags = Notification.FLAG_AUTO_CANCEL; // 点击通知后自动消失
31
32 // 4.发送通知
33 nm.notify(Process.myPid(), notification); // Process.myPid(); 获取当前进程的id
34 }
35 }
自定义通知的布局设置:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="horizontal" > 6 7 <ImageView 8 android:layout_width="wrap_content" 9 android:layout_height="wrap_content" 10 android:src="@drawable/ic_launcher" 11 /> 12 <LinearLayout 13 android:layout_marginLeft="3dp" 14 android:layout_width="match_parent" 15 android:layout_height="wrap_content" 16 android:orientation="vertical" 17 > 18 <TextView 19 android:layout_marginTop="2dp" 20 android:layout_width="wrap_content" 21 android:layout_height="wrap_content" 22 android:text="自定义的通知,专业拦截器" 23 /> 24 <ProgressBar 25 android:layout_marginTop="2dp" 26 android:layout_width="match_parent" 27 android:layout_height="wrap_content" 28 style="@android:style/Widget.ProgressBar.Horizontal" 29 /> 30 </LinearLayout> 31 32 33 </LinearLayout>
自定义通知代码如下:
1 package com.android.hzynotification;
2 import android.app.Activity;
3 import android.app.Notification;
4 import android.app.NotificationManager;
5 import android.app.PendingIntent;
6 import android.content.Context;
7 import android.content.Intent;
8 import android.os.Bundle;
9 import android.view.View;
10 import android.widget.RemoteViews;
11
12 public class MainActivity extends Activity {
13
14 @Override
15 protected void onCreate(Bundle savedInstanceState) {
16 super.onCreate(savedInstanceState);
17 setContentView(R.layout.activity_main);
18 }
19
20
21 public void costom(View v){
22 // 1.得到通知管理器
23 NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
24 // 2.构建通知
25 Notification notification = new Notification();
26 notification.icon = android.R.drawable.stat_notify_missed_call; // 图标
27 notification.tickerText = "有未接电话哦";
28
29 RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.content);
30 notification.contentView = contentView; // 设置通知显示的布局
31
32 // 设置通知点击的事件
33 Intent intent = new Intent(this,OtherActivity.class);
34 PendingIntent contentIntent = PendingIntent.getActivity(this, 100, intent, 0);
35 notification.contentIntent = contentIntent;
36
37 // 设置点击通知后自动消失
38 notification.flags = Notification.FLAG_AUTO_CANCEL;
39 // 发出通知
40 nm.notify(100, notification);
41
42 }
43 }

标签:
原文地址:http://www.cnblogs.com/youyu16/p/4398615.html