package?com.webabcd.notification;
import?android.app.Activity;
import?android.app.Notification;
import?android.app.NotificationManager;
import?android.app.PendingIntent;
import?android.content.Context;
import?android.content.Intent;
import?android.os.Bundle;
import?android.util.Log;
import?android.view.LayoutInflater;
import?android.view.View;
import?android.widget.Button;
import?android.widget.TextView;
import?android.widget.Toast;
public?class?Main?extends?Activity?{
????/**?Called?when?the?activity?is?first?created. */
????@Override
????public?void?onCreate(Bundle?savedInstanceState) {
????????super.onCreate(savedInstanceState);
????????setContentView(R.layout.main);
????????//通过?Tost.makeText().show()?来实现提示性的通知效果
????????//短时间的提示性通知的?Demo
????????Button?btn1?=?(Button)?this.findViewById(R.id.btn1);
????????btn1.setText("短时间提示");
????????btn1.setOnClickListener(new?Button.OnClickListener() {
????????????public?void?onClick(View?v) {
????????????????Toast.makeText(Main.this,?"我是短时间提示",?Toast.LENGTH_SHORT).show();
????????????}
????????});
????????//长时间的提示性通知的?Demo
????????Button?btn2?=?(Button)?this.findViewById(R.id.btn2);
????????btn2.setText("长时间提示");
????????btn2.setOnClickListener(new?Button.OnClickListener() {
????????????public?void?onClick(View?v) {
????????????????Toast.makeText(Main.this,?"我是长时间提示",?Toast.LENGTH_LONG).show();
????????????}
????????});
????????//以一个?View?作为提示性通知的?Demo
????????Button?btn3?=?(Button)?this.findViewById(R.id.btn3);
????????btn3.setText("以一个?View?做提示");
????????btn3.setOnClickListener(new?Button.OnClickListener() {
????????????public?void?onClick(View?v) {
????????????????View?view?=?inflateView(R.layout.view);
????????????????TextView?txtMsg?=?(TextView)?view.findViewById(R.id.txtMsg);
????????????????txtMsg.setText("提示内容");
????????????????Toast?toast?=?new?Toast(Main.this);
????????????????toast.setView(view);
????????????????toast.setDuration(Toast.LENGTH_LONG);
????????????????toast.show();
????????????}
????????});
????????Button?btn4?=?(Button)?this.findViewById(R.id.btn4);
????????btn4.setText("发出一个通知(Notification)");
????????btn4.setOnClickListener(new?Button.OnClickListener() {
????????????public?void?onClick(View?v) {
????????????????//实例化通知管理器
????????????????NotificationManager?nm?=?(NotificationManager)?getSystemService(NOTIFICATION_SERVICE);
????????????????//指定单击通知后所打开的详细的通知页面(单击通知后打开?NotificationView)
????????????????PendingIntent?contentIntent?=?PendingIntent.getActivity(
????????????????????????Main.this,?0,?new?Intent(Main.this,????NotificationView.class),?0);
????????????????//实例化一个通知,并指定其图标和标题(在提示栏上显示)
????????????????Notification?n?=?new?Notification(R.drawable.icon01,?"我是滚动的通知信息我是滚动的通知信息我是滚动的通知信息",?System.currentTimeMillis());
????????????????//设置通知的发送人和通知的详细内容(打开提示栏后在通知列表中显示)
????????????????n.setLatestEventInfo(Main.this,?"通知发送人",?"我是详细的通知信息我是详细的通知信息我是详细的通知信息",?contentIntent);
????????????????//100?毫秒延迟后,震动?250?毫秒,暂停?100?毫秒后,再震动?500?毫秒
????????????????n.vibrate?=?new?long[]?{?100,?250,?100,?500?};
????????????????//发出通知(其中第一个参数为通知标识符)
????????????????nm.notify(0,?n);
????????????}
????????});
????}
????//将指定的?xml?资源转换为一个?View
????private?View?inflateView(int?resource) {
????????LayoutInflater?vi?=?(LayoutInflater)?getSystemService(Context.LAYOUT_INFLATER_SERVICE);
????????return?vi.inflate(resource,?null);
????}
????//打开详细通知页后此?Activity?会被?Pause,从详细通知页返回后此?Activity?会被?Resume
????@Override
????protected?void?onPause() {
????????//TODO?Auto-generated?method?stub
????????super.onPause();
????????Log.d("MyDebug",?"onPause");
????}
????@Override
????protected?void?onResume() {
????????//TODO?Auto-generated?method?stub
????????super.onResume();
????????Log.d("MyDebug",?"onResume");
????}
}