简单的通知栏效果

来源:互联网 发布:淘宝儿童摇摆车扭扭车 编辑:程序博客网 时间:2024/05/21 07:48

很简单的一个小例子,当通知来了,获取通知的内容并显示在文本框中。

写了两个按钮,一个发送通知,一个是取消通知。

活动:

/** * 测试通知栏 */public class TestNotify extends Activity implements OnClickListener {private TextView notifyTxt;private Button notifyBtn;private Button cancleBtn;private NotificationManager nManager;private Notification notify;private String myMsg= "";@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.acy_notify);initView();// 创建通知notify = new Notification();// 通知管理器nManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);// 为通知设图标,该图标显示在状态栏notify.icon = R.drawable.ic_com_sina_weibo_sdk_logo;// 设置默认// notify.defaults=Notification.DEFAULT_ALL;// 设置声音notify.defaults = Notification.DEFAULT_SOUND;// 设置震动notify.defaults = Notification.DEFAULT_VIBRATE;// 设置闪光灯notify.defaults = Notification.DEFAULT_LIGHTS;}public void initView() {notifyTxt = (TextView) findViewById(R.id.tv_settxt);notifyBtn = (Button) findViewById(R.id.btn_notify);cancleBtn = (Button) findViewById(R.id.btn_cancel);notifyBtn.setOnClickListener(this);cancleBtn.setOnClickListener(this);}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubif (v == notifyBtn) {myMsg="灰色尘埃";// 通知文本notify.tickerText =myMsg;Intent intent = new Intent(getApplicationContext(),TestNotify.class);PendingIntent conteIntent = PendingIntent.getActivity(getApplicationContext(), 100, intent, 0);notify.setLatestEventInfo(getApplicationContext(), "标题", myMsg, conteIntent);notify.flags = Notification.FLAG_AUTO_CANCEL;nManager.notify(100, notify);notifyTxt.setText(notify.tickerText);} else if (v == cancleBtn) {nManager.cancel(100);}}}

布局:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <TextView        android:id="@+id/tv_settxt"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="显示通知效果" />    <Button        android:id="@+id/btn_notify"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="发送通知" />    <Button        android:id="@+id/btn_cancel"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="取消通知s" /></LinearLayout>

效果:

通知的图标是用的新浪的log,独家记忆是通知里面的内容,点击通知获取的通知内容,显示在文本中。


注意:

1.如果你想加入闪光灯,震动或者声音的效果,那么首先你得添加使用的权限:

 <uses-permission android:name="android.permission.FLASHLIGHT"/>    <uses-permission android:name="android.permission.VIBRATE"/>
然后,你在添加效果,可以使用默认的也可以自定义。

默认

// 设置声音notify.defaults = Notification.DEFAULT_SOUND;// 设置震动notify.defaults = Notification.DEFAULT_VIBRATE;// 设置闪光灯notify.defaults = Notification.DEFAULT_LIGHTS;

2.标识:

LAG_AUTO_CANCEL  该通知能被状态栏的清除按钮给清除掉
FLAG_NO_CLEAR     该通知能被状态栏的清除按钮给清除掉
FLAG_ONGOING_EVENT 通知放置在正在运行
FLAG_INSISTENT 是否一直进行,比如音乐一直播放,知道用户响应

0 0