在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工程,效果如下图所示:

OK!收工!

更多相关文章

  1. Android(安卓)NDK 知识系列(五)
  2. JSONArray的使用
  3. 面向忙碌开发者的 Android(安卓)知识点收录
  4. 使用NDK开发SQLite3
  5. Android(安卓)RecyclerView使用(二) -给Item添加点击事件
  6. Android系统在超级终端下必会的命令大全(adb shell命令大全)
  7. 箭头函数的基础使用
  8. NPM 和webpack 的基础使用
  9. Python list sort方法的具体使用

随机推荐

  1. 2015年Android开发新技术小结
  2. 在Android下有类似于session的东西,叫做Ap
  3. Android Studio的安装步骤及设置.
  4. Android(安卓)ViewDragHelper使用介绍
  5. Andriod使用Intent实现拨号
  6. [Android]Android MVP&依赖注入&单元测试
  7. android下sqlite操作详解
  8. 第13天android:向sd卡写文件
  9. Android采取BroadcastReceiver方式自动获
  10. sharedPreferences保存数据和TextWatcher