iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android中关于Notification及NotificationManger的详解
  • 359
分享到

Android中关于Notification及NotificationManger的详解

notificationAndroid 2022-06-06 10:06:56 359人浏览 薄情痞子
摘要

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

Android状态栏提醒

Android中提醒功能也可以用AlertDialog,但是我们要慎重的使用,因为当使用AlertDialog的时候,用户正在进行的操作将会被打断,因为当前焦点被AlertDialog得到。我们可以想像一下,当用户打游戏正爽的时候,这时候来了一条短信。如果这时候短信用AlertDialog提醒,用户必须先去处理这条提醒,从而才能继续游戏。用户可能会活活被气死。而使用Notification就不会带来这些麻烦事,用户完全可以打完游戏再去看这条短信。所以在开发中应根据实际需求,选择合适的控件。

步骤:

一、添加布局对象
代码如下:
<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" />

二、修改MianActivity继承处Activity并实现接口OnClickListener
代码如下:
public class MainActivity extends Activity implements OnClickListener {
 private Context mContext = this;
 private Button showbtn, calclebtn;
 private Notification noti;
 private NotificationManager notiManger;
 private static int NOTIFICATION_ID = 0x0001;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  setUpViews();
 }

 private void setUpViews() {
  showbtn = (Button) findViewById(R.id.showButton);
  calclebtn = (Button) findViewById(R.id.cancelButton);
  noti = new Notification(R.drawable.ic_launcher, "this is a notification", System.currentTimeMillis());
  noti.defaults = Notification.DEFAULT_SOUND;// 使用默认的提示声音
  noti.defaults |= Notification.DEFAULT_VIBRATE;// 添加震动
  notiManger = (NotificationManager) this.getSystemService(mContext.NOTIFICATION_SERVICE);//获取NofificationManger对象
  showbtn.setOnClickListener(this);//让Activity实现接口OnClickListener可以简单的通过此两行代码添加按钮点击响应事件
  calclebtn.setOnClickListener(this);
 }

 // 按钮点击事件响应
 @Override
 public void onClick(View v) {
  if (v == showbtn) {
   Intent intent = new Intent(this.getApplicationContext(),this.getClass());
   // 设置Intent.FLAG_ACTIVITY_NEW_TASK
   intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
   // noti.setLatestEventInfo(context, contentTitle, contentText, contentIntent)设置(上下文,标题,内容,PendingInteng)
   noti.setLatestEventInfo(this, "10086", "你从此以后免除所有话费", contentIntent);
   // 发送通知(消息ID,通知对象)
   notiManger.notify(NOTIFICATION_ID, noti);
  } else if (v == calclebtn) {
   // 取消通知(id)
   notiManger.cancel(NOTIFICATION_ID);
  }
 }
}

您可能感兴趣的文章:android notification 的总结分析Android界面 NotificationManager使用Bitmap做图标Android中通知Notification使用实例(振动、灯光、声音)Android中通过Notification&NotificationManager实现消息通知android中创建通知栏Notification代码实例Android编程实现拦截短信并屏蔽系统Notification的方法Android开发 -- 状态栏通知Notification、NotificationManager详解android使用NotificationListenerService监听通知栏消息Android中Notification 提示对话框Android自定义Notification添加点击事件


--结束END--

本文标题: Android中关于Notification及NotificationManger的详解

本文链接: https://www.lsjlt.com/news/27833.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作