还是直接看例子程序吧.....注释写的很清楚!

Activity =======>NotificationDemoAct_02.java

package csdn.NotificationDemo_02; 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.graphics.Color; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class NotificationDemoAct_02 extends Activity { //常量声明 public static final int NOTIFY_ID = 0x01; public static final String SHOWTITLE = "The Apple PC"; public static long thisTime = System.currentTimeMillis(); //控件声明 private Button btnShow = null; private Button btnCancel = null; private TextView textTipInfo = null; private BtnListener mListener = null; //声明Notification相关引用类型变量 Notification mNotification = null; NotificationManager mManager = null; Intent mIntent = null; //PendingIntent,它可以看做是Intent这封信的一个信封。 //PendingIntent基本上是Intent的包装和描述,对象收到PendingIntent后,可以得到其中的Intent再发出去 PendingIntent mPIntent = null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mListener = new BtnListener(); textTipInfo = (TextView)findViewById(R.id.text_info); //创建控件对象并设置监听器 btnShow = (Button)findViewById(R.id.btn_show); btnCancel = (Button)findViewById(R.id.btn_cancel); btnShow.setOnClickListener(mListener); btnCancel.setOnClickListener(mListener); //初始化 prepare(); } /** * 名称:prepare()方法 * 功能:创建对象,初始化 */ private void prepare() { // TODO Auto-generated method stub Context mContext = NotificationDemoAct_02.this; mManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); mIntent = new Intent(mContext, NotificationDemoAct_02.class); //FLAG_ACTIVITY_CLEAR_TOP, FLAG_ACTIVITY_NEW_TASK者两个FLAG表示优先寻找已经打开的应用,如果应用没有打开那么启动它。 mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); //FLAG_UPDATE_CURRENT是指后来的PendingIntent会更新前面的 mPIntent = PendingIntent.getActivity(this, NOTIFY_ID, mIntent, PendingIntent.FLAG_UPDATE_CURRENT); mNotification = new Notification(); } /** * 名称:setNotificationDemo()方法 * 功能:设置相关属性 * 说明:这个一定要在setLatestEventInfo()方法之前调用 */ public void setNotificationDemo() { //设置显示图标 mNotification.icon = R.drawable.apple; //设置显示标题 mNotification.tickerText = SHOWTITLE; //设置时间 mNotification.when = thisTime; //表示这个Notification代表几个事件 mNotification.number = 3; //设置LED闪烁,需要设置相关Flags并且需要硬件支持 mNotification.ledARGB = Color.BLUE; mNotification.ledOnMS = 300; mNotification.ledOffMS = 1000; //表示显示LED,按位或运算 mNotification.flags |= Notification.FLAG_SHOW_LIGHTS; //设置震动,这个需要在manifest文件中声明权限 /*================================================================================= long[] vibrate = new long[] {10, 50, 100, 200, 300}; mNotification.vibrate = vibrate; //等效下面代码 ==================================================================================*/ mNotification.vibrate = new long[] {10, 50, 100, 200, 300}; //设置声音 mNotification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3"); /*================================================================================== * 设置Flags,API要求按位或运算(bitwise-ored) * 还有其他Flags,可以参阅文档 ==================================================================================*/ //表示用户查看Notification点击一次,就取消该图标和想关信息 //等效于 mManager.cancel(NOTIFY_ID); //mNotification.flags |= Notification.FLAG_AUTO_CANCEL; /*=================================================================================== * 我们也可以使用系统默认的资源,但是图标没有默认需要自己指定 //表示DEFAULT_LIGHTS、DEFAULT_SOUND、DEFAULT_VIBRATE //mNotification.defaults = Notification.DEFAULT_ALL; * =======或者这样做====================================== //mNotification.defaults |= Notification.DEFAULT_LIGHTS; //mNotification.defaults |= Notification.DEFAULT_SOUND; //mNotification.defaults |= Notification.DEFAULT_VIBRATE; =====================================================================================*/ } class BtnListener implements OnClickListener { public void onClick(View v) { // TODO Auto-generated method stub switch(v.getId()) { case R.id.btn_show: showNotification(); textTipInfo.setText("you can look the Notification!"); break; case R.id.btn_cancel: cancelNotification(); break; default: break; } } } public void showNotification() { // TODO Auto-generated method stub setNotificationDemo(); //调用该方法之前先设置相关属性 mNotification.setLatestEventInfo(this, "Apple Company", "The Apple's PC has sent to you!" + "/r/n" + "Please check!", mPIntent); mManager.notify(NOTIFY_ID, mNotification); } public void cancelNotification() { // TODO Auto-generated method stub //取消该Notification mManager.cancel(NOTIFY_ID); } }


布局文件很简单,顺便看看....

文件名称:main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/text_info" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Notification" /> <TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:stretchColumns="0,1" > <TableRow> <Button android:id="@+id/btn_show" android:layout_width="70dip" android:layout_height="50dip" android:layout_marginTop="20dip" android:layout_marginLeft="50dip" android:layout_marginRight="15dip" android:text="Show" /> <Button android:id="@+id/btn_cancel" android:layout_width="70dip" android:layout_height="50dip" android:layout_marginTop="20dip" android:layout_marginLeft="15dip" android:layout_marginRight="50dip" android:text="Cancel" /> </TableRow> </TableLayout> </LinearLayout>

最后,提醒大家不要忘记权限...

<uses-permission android:name="android.permission.VIBRATE"> </uses-permission>


好了,看看效果图吧...

图1 界面布局

图2 点击show按钮

图3 Notification内容

大家看到效果图之后,是不是有办法将Notification中的内容换行显示呢,欢迎批评指正!

更多相关文章

  1. android EditText设置不可写
  2. android“设置”里的版本号
  3. 在Fragment中设置控件点击方法,执行失败。
  4. Android(安卓)闹钟管理类的使用
  5. Android设置通知栏/状态栏透明改变通知栏颜色和app最上部分颜色
  6. android 设置中划线 下划线等
  7. Andorid Dialog 示例【慢慢更新】
  8. android图表ichartjs
  9. Android(安卓)闹钟管理类的使用

随机推荐

  1. Android组件化导致的代码不生效
  2. java回调C++
  3. 一个 Android 简易的新闻客户端
  4. Android Service Framework分析
  5. Android API教程:人脸检测(上)
  6. Android Studio 从入门到精通(Windows &
  7. Lgame引擎搭建和第一个例子
  8. Android基础控件——ProgressBar自定义的
  9. 【Android】TextView动态设置Drawable资
  10. 解决 “android New package not yet reg