android 状态栏提醒 Notification 的使用!
在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!收工!
更多相关文章
- android 读取本地超大图片
- Android 图片加水印
- Android gallery实现图片的左右循环旋转源码分享
- 通过 http post 方式上传多张图片
- Android 上传图片到服务器(多文件上传)
- android zxing 解析二维码图片与生成二维码图片
- Android之glide加载图片圆角效果
- Android通过共享用户ID来实现多Activity进程共享
- Android多点触控(图片的缩放Demo)