android 开发 沉降式状态栏_Android状态栏提醒

194708f40a4d84c6c7065c56b4469b06.png

大家好今天简单讲一下Android状态栏提醒,这个在开发中也会经常使用,当我们插上USB会有状态栏提醒,来短信时也会有状态栏的提醒。

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

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

好了我今天又简单写了一个Demo, 教大家如何使用Notification,大致分以下几个步骤:

第一步:新建一个Android工程命名为NotificationDemo.

第二步:修改main.xml代码如下:

<?xml version="1.0" encoding="utf-8"?>

第三步:修改NotificationDemo.java代码如下:

package com.tutor.notification;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;  @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);   setupViews();  } //这里是初始化一些操作,可以看到onCreate()方法里代码非常简洁。 public void setupViews(){ mContext = NotificationDemo.this; showButton = (Button)findViewById(R.id.showButton); cancelButton = (Button)findViewById(R.id.cancelButton);  mNotification = new Notification(R.drawable.icon,"This is a notification.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值