Notification(一)
来源:互联网 发布:unity3d vr 室内场景 编辑:程序博客网 时间:2024/05/17 03:00
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.notification.MainActivity" > <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="57dp" android:layout_marginTop="51dp" android:text="创建一个提示" /></RelativeLayout>
MainActivity
package com.example.notification;import android.app.Activity;import android.app.Notification;import android.app.NotificationManager;import android.content.Context;import android.os.Bundle;import android.support.v4.app.NotificationCompat;import android.support.v4.app.NotificationCompat.Builder;import android.view.View;import android.widget.Button;public class MainActivity extends Activity {public static final int NOTIFICATION_ID = 1200;private Button button;private int count;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button = (Button) findViewById(R.id.button);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {count++;//import android.support.v4.app.NotificationCompatBuilder builder = new NotificationCompat.Builder(MainActivity.this);builder.setSmallIcon(R.drawable.ic_launcher);builder.setContentTitle("有" + count + "个未读消息");builder.setContentText("你好,我叫wyf");//创建即可Notification notification = builder.build();NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);//管理器更新通知manager.notify(NOTIFICATION_ID, notification);}});}}
**************************************************添加意图*****************************
布局文件有2个button--发送通知、取消通知
package com.imooc.notificationdemo;import android.annotation.SuppressLint;import android.app.Activity;import android.app.Notification;import android.app.Notification.Builder;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;public class MainActivity extends Activity implements OnClickListener {// 通知控制类private NotificationManager manager;// 通知的id值private int notification_ID;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 获取通知的管理类对象manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);//获取控件,添加点击事件findViewById(R.id.btn_send).setOnClickListener(this);findViewById(R.id.btn_cancle).setOnClickListener(this);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.btn_send:sendNotification();break;case R.id.btn_cancle:manager.cancel(notification_ID);break;}}/** * 构造notification并发送到通知栏 */@SuppressLint("NewApi")private void sendNotification() {/** * intent英文意思是意图,pending表示即将发生或来临的事情。 * PendingIntent这个类用于处理即将发生的事情。比如在通知Notification中用于跳转页面,但不是马上跳转。 */Intent intent = new Intent(this, MainActivity.class);PendingIntent pintent = PendingIntent.getActivity(this, 0, intent, 0);// 创建builder,设置各种参数信息Builder builder = new Notification.Builder(this);builder.setSmallIcon(R.drawable.ic_launcher);// 设置图标builder.setTicker("hello");// 手机状态栏的提示;builder.setWhen(System.currentTimeMillis());// 设置时间builder.setContentTitle("通知栏通知");// 设置标题builder.setContentText("我来自NotificationDemo");// 设置通知内容builder.setDefaults(Notification.DEFAULT_SOUND);// 设置提示声音builder.setDefaults(Notification.DEFAULT_ALL);// 设置震动builder.setContentIntent(pintent);// 点击后的意图Notification notification = builder.build();// 4.1以上manager.notify(notification_ID, notification);}}
0 0
- Notification(一)
- 一、Notification简介
- notification基本用法(一)
- Notification集合(一)
- Remote Notification (一)
- 自定义Notification(一)
- Notification 一 普通视图
- Android Notification 基础(一)
- 关于Notification的一系列问题(一)
- Android Notification(一)使用通知
- 王学岗Notification通知详解(一)
- Notification的使用(一)
- Android Notification(一) Notification 的 requestCode 与 id
- Notification
- Notification
- Notification
- Notification
- notification
- js对象笔记
- 链表指定值清除(C++)
- 课时3第二章:标识符-关键字-数据类型2
- 每天C语言总结
- 检查em集群配置报错:ORA-28000
- Notification(一)
- 出来混总是会忘记的,不过偶尔能记起来就好了,大家还记得当初敲HelloWorld 时的心情吗?
- 【蓝桥第二周】和尚挑水
- GiftGenerator:标准可读格式选择题解析为GIFT格式(用于Moodle)
- 关于mysql的自增id测试,innodb和myisam下的不同表现
- HBase集群安装-0 环境安装
- 同时在工程中引入了多个第三方jar包,导致的DEX错误
- Scala 正则表达式
- 创建SQLAlchemy的ORM类的基类(二)