您的位置:首页 > 移动开发 > Android开发

Android NotificationManager 和Notification的使用总结

2013-03-12 16:47 483 查看
//1、创建一个自 定义的消息布局 view.xml

<?xml
version=
"1.0"
encoding=
"utf-8"
?>


<LinearLayout
xmlns:android=
"http://schemas.android.com/apk/res/android"


android:layout_width=
"fill_parent"
android:layout_height=
"fill_parent"
>


<ImageView
android:id=
"@+id/image"
android:layout_width=
"wrap_content"


android:layout_height=
"fill_parent"
android:layout_marginRight=
"10dp"
/>


<TextView
android:id=
"@+id/text"
android:layout_width=
"wrap_content"


android:layout_height=
"fill_parent"
android:textColor=
"#000"
/>


</LinearLayout>

//2、 在程序代码中使用RemoteViews的方法来定义image和text。然后把RemoteViews对象传到contentView字段

RemoteViews
contentView =
new
RemoteViews(getPackageName(),R.layout.view);


contentView.setImageViewResource(R.id.image,R.drawable.icon);

contentView.setTextViewText(R.id.text,”Hello,
this
message
is in a custom expanded view”);


notification.contentView = contentView;

//3、 为Notification的contentIntent字段定义一个Intent(注意,使用自定义View不需要
setLatestEventInfo()方法)

Intent
notificationIntent =
new
Intent(
this
,Main.
class
);


PendingIntent
contentIntent = PendingIntent.getActivity(
this
,
0
,notificationIntent,
0
);


notification.contentIntent = contentIntent;

//4、发送通知

mNotificationManager.notify(
2
,notification);


// 以下是全部示例代码

//创建一个 NotificationManager的引用

String ns = Context.NOTIFICATION_SERVICE;

NotificationManager mNotificationManager =
(NotificationManager)getSystemService(ns);

// 定义Notification的各种属性

int
icon
= R.drawable.icon;
//通知图标


CharSequence
tickerText =
"Hello"
;
//状态栏显示的通知文本提示


long
when
= System.currentTimeMillis();
//通知产生的时间,会在通知信息里显示


//用上面的属性初始化 Nofification

Notification
notification =
new
Notification(icon,tickerText,when);


RemoteViews
contentView =
new
RemoteViews(getPackageName(),R.layout.view);


contentView.setImageViewResource(R.id.image,
R.drawable.iconempty);

contentView.setTextViewText(R.id.text,
"Hello,this
is JC"
);


notification.contentView = contentView;

Intent
notificationIntent =
new
Intent(
this
,Main.
class
);


PendingIntent
contentIntent = PendingIntent.getActivity(
this
,
0
,notificationIntent,
0
);


notification.contentIntent = contentIntent;

//把Notification传递给NotificationManager

mNotificationManager.notify(
0
,notification);


Java代码


private void initNotification()

{

int notificationID = 10;

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

// Create the notification

Notification notification = new Notification(R.drawable.clock, "TestActivity已经起动",

System.currentTimeMillis());

notification.flags |= Notification.FLAG_ONGOING_EVENT; // 将此通知放到通知栏的"Ongoing"即"正在运行"组中

notification.flags |= Notification.FLAG_NO_CLEAR; // 表明在点击了通知栏中的"清除通知"后,此通知不清除,经常与FLAG_ONGOING_EVENT一起使用

notification.flags |= Notification.FLAG_SHOW_LIGHTS; // set LED on

// notification.defaults = Notification.DEFAULT_LIGHTS; //默认Notification lights;

notification.ledARGB = R.color.blue; // LED 颜色;

notification.ledOnMS = 5000; // LED 亮时间

// Create the notification expanded message

// When the user clicks on it, it opens your activity

Intent intent = new Intent(this, TestActivity.class);

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,

intent, 0);

notification

.setLatestEventInfo(this, "TestActivity", "17:50执行下个任务", pendingIntent);

// Show notification

notificationManager.notify(notificationID, notification);

}

Java代码

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

initNotification();

iniTimePick();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: