在Android中有提醒功能的可以用AlertDialog,但是我们要审重的使用,因为当使用AlertDialog 的时候,用户正在进行的操作将会被打断。

而使用Notification就不会

<?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:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Welcome to Mr Wei's blog" /> <Button android:id="@+id/showButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="showNotification" /> <Button android:id="@+id/cancelButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="cancelNotification" /> </LinearLayout>

java代码如下:

package com.my; 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.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class NotificationDemo extends Activity implements OnClickListener { private Context mContext; private Button showButton, cancelButton; private Notification mNotification; private NotificationManager mNotificationManager; private final static int NOTIFICATION_ID = 0x0001;//用这个id来识别Notification @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); setupViews(); } public void setupViews() { mContext = NotificationDemo.this; showButton = (Button) findViewById(R.id.showButton); cancelButton = (Button) findViewById(R.id.cancelButton); showButton.setOnClickListener(this); cancelButton.setOnClickListener(this); /** * 使用Notification */ mNotification = new Notification(R.drawable.icon, "This is a notification.", System.currentTimeMillis()); mNotification.defaults = Notification.DEFAULT_SOUND;//将使用默认的声音来提醒用户 mNotificationManager = (NotificationManager) this .getSystemService(NOTIFICATION_SERVICE); } //按钮点击事件响应 public void onClick(View v) { if (v == showButton) { /** * 使用Notification的方法 */ Intent mIntent = new Intent(mContext, NotificationDemo.class); mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//这里需要设置Intent.FLAG_ACTIVITY_NEW_TASK属性 PendingIntent mContentIntent = PendingIntent.getActivity(mContext, 0, mIntent, 0); //这里必需要用setLatestEventInfo(上下文,标题,内容,PendingIntent)不然会报错. mNotification.setLatestEventInfo(mContext, "10086", "您的当前话费不足,请充值.哈哈~", mContentIntent); mNotificationManager.notify(NOTIFICATION_ID, mNotification);//这里发送通知(消息ID,通知对象) } else if (v == cancelButton) { //取消只要把通知ID传过来就OK了. mNotificationManager.cancel(NOTIFICATION_ID); } } }

运行Android工程,效果如下图所示:

android 状态栏提醒 Notification 的使用!_第1张图片android 状态栏提醒 Notification 的使用!_第2张图片

android 状态栏提醒 Notification 的使用!_第3张图片

android 状态栏提醒 Notification 的使用!_第4张图片

OK!收工!

更多相关文章

  1. android 读取本地超大图片
  2. Android 图片加水印
  3. Android gallery实现图片的左右循环旋转源码分享
  4. 通过 http post 方式上传多张图片
  5. Android 上传图片到服务器(多文件上传)
  6. android zxing 解析二维码图片与生成二维码图片
  7. Android之glide加载图片圆角效果
  8. Android通过共享用户ID来实现多Activity进程共享
  9. Android多点触控(图片的缩放Demo)

随机推荐

  1. mysql 8.0.13手动安装教程
  2. MySQL中无过滤条件的count详解
  3. MySQL中int最大值深入讲解
  4. Mysql主键和唯一键的区别点总结
  5. MySQL按时间统计数据的方法总结
  6. 实例讲解MySQL中乐观锁和悲观锁
  7. SQL和NoSQL之间的区别总结
  8. mysql蠕虫复制基础知识点
  9. Mysql数据表中的蠕虫复制使用方法
  10. MySQL limit性能分析与优化