博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android通知的基本用法
阅读量:7015 次
发布时间:2019-06-28

本文共 2252 字,大约阅读时间需要 7 分钟。

新建一个Android的项目   通知栏的基本用法

修改activity_main.xml的代码,如下:

布局文件非常简单,里面只有一个Send Notice 按钮,用于发送一条通知。

 

新建一个Activity界面,并需要准备一个新的布局文件,notification_layout.xml

 

新建NotificationActivity继承Activity,在这里加载刚才定义的布局文件。

package com.example.tongzhi;import android.app.Activity;import android.app.NotificationManager;import android.os.Bundle;public class NotificationActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        // 设置点击通知栏信息得到的界面        setContentView(R.layout.notification_layout);        // 将通知栏的信息消失        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);        manager.cancel(1);    }}

 

 

接下来修改,MainActivity中的代码

package com.example.tongzhi;import android.app.Activity;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Intent;import android.os.Bundle;import android.view.View;public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }    public void click(View view) {        switch (view.getId()) {        case R.id.send_notice:            NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);            // 第三个参数指定通知信息被创建的具体时间            Notification notification = new Notification(                    R.drawable.ic_launcher, "This is ticker text",                    System.currentTimeMillis());            // 响应点击通知信息,PendingIntent            Intent intent = new Intent(this, NotificationActivity.class);            PendingIntent pi = PendingIntent.getActivity(this, 0, intent,                    PendingIntent.FLAG_CANCEL_CURRENT);            // 对通知的布局进行设定,下面的方法就可以给通知设置一个标准的布局            notification.setLatestEventInfo(this, "This is content title",                    "This is content text", pi);            // 调用NotificationManager的notify()方法让通知显示出来            manager.notify(1, notification);            break;        default:            break;        }    }}

 

转载于:https://www.cnblogs.com/zhangbaowei/p/4665390.html

你可能感兴趣的文章
投稿相关
查看>>
SQL取出 所有周六 周日的日期
查看>>
background的css 排列顺序写法?
查看>>
HTML-002
查看>>
应对刷新闪烁问题
查看>>
第十一周项目1-存储班长信息的学生类
查看>>
解决手机浏览器顶部下拉出现网页源或刷新的问题
查看>>
正则表达式
查看>>
python开发第一步
查看>>
Mybatis自动生成的配置实例
查看>>
kvm 基本运维命令
查看>>
PID 29364
查看>>
设计模式-状态模式(25)
查看>>
group by查询每组时间最新的一条记录
查看>>
struts2--前台数据通过参数传给后台,后台如何获取参数
查看>>
HDU 4054 Hexadecimal View【模拟】【字符串处理】
查看>>
配置cordova的android开发环境(无android studio)
查看>>
监控glusterfs
查看>>
tomcat 启动慢问题
查看>>
map-reduce流程图
查看>>