Notification通知栏的使用
来源:互联网 发布:武林外传 反派知乎 编辑:程序博客网 时间:2024/06/09 23:17
我们在用手机的时候,如果来了短信,而我们没有点击查看的话,是不是在手机的最上边的状态栏里有一个短信的小图标提示啊?你是不是也想实现这种功能呢?今天的Notification就是解决这个问题的。
- package cn.com.chenzheng_java;
- 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.net.Uri;
- import android.os.Bundle;
- import android.provider.MediaStore.Audio;
- import android.view.View;
- import android.widget.Button;
- /***
- * @description 状态栏通知相关
- * @author chenzheng_java
- *
- */
- public class NotificationActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.notification);
- Button button = (Button) findViewById(R.id.button);
- button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- addNotificaction();
- }
- });
- }
- /**
- * 添加一个notification
- */
- private void addNotificaction() {
- NotificationManager manager = (NotificationManager) this
- .getSystemService(Context.NOTIFICATION_SERVICE);
- // 创建一个Notification
- Notification notification = new Notification();
- // 设置显示在手机最上边的状态栏的图标
- notification.icon = R.drawable.excel;
- // 当当前的notification被放到状态栏上的时候,提示内容
- notification.tickerText = "注意了,我被扔到状态栏了";
- /***
- * notification.contentIntent:一个PendingIntent对象,当用户点击了状态栏上的图标时,该Intent会被触发
- * notification.contentView:我们可以不在状态栏放图标而是放一个view
- * notification.deleteIntent 当当前notification被移除时执行的intent
- * notification.vibrate 当手机震动时,震动周期设置
- */
- // 添加声音提示
- notification.defaults=Notification.DEFAULT_SOUND;
- // audioStreamType的值必须AudioManager中的值,代表着响铃的模式
- notification.audioStreamType= android.media.AudioManager.ADJUST_LOWER;
- //下边的两个方式可以添加音乐
- //notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");
- //notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");
- Intent intent = new Intent(this, Notification2Activity.class);
- PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
- // 点击状态栏的图标出现的提示信息设置
- notification.setLatestEventInfo(this, "内容提示:", "我就是一个测试文件", pendingIntent);
- manager.notify(1, notification);
- }
- }
点击按钮时候,状态栏会显示:
看到了吧,状态栏多出来一个excel图标,当我按住图标不放,往下拖动的时候,出来了这个页面
然后,当我们点击这个对话框之后,就会触发intent,跳转到Notification2Activity.java这个activity。
0 0
- Notification通知栏的使用
- Notification通知栏的使用
- Notification 通知栏的使用
- Notification通知的使用
- 通知栏Notification使用
- android中通知栏notification的使用
- adnroid,notification通知栏的使用
- android通知栏(Notification)的使用
- android通知栏之Notification的使用
- Notification通知栏的基本使用
- Android--Notification通知栏的使用
- 通知栏notification的一些使用代码
- 通知栏(Notification)的基本使用
- 【Android】使用Notification实现状态通知栏的通知
- android通知(Notification)的使用
- android Notification(通知) 的使用
- Notification(通知)的简单使用
- Notification(通知)的使用简介
- Leetcode: Word Break II
- 使用spring框架中的 RestTemplate访问rest接口
- 光荣的梦想
- lua 中pairs 与 ipairs
- vim 批量查找替换字符
- Notification通知栏的使用
- 两种方法(递归,非递归)实现单链表的逆转
- [面试] - Java方向问题1
- Servlet学习笔记(三):Servlet基础
- 数据结构学习(一)
- [面试] - java面试题2
- 解决TCP网络传输“粘包”问题
- 山东中医药大学《数据结构》实验1
- C. Modified GCD(二分加搜索约数)