Android简易实战教程--第三十八话《自定义通知NotifiCation》
上一篇小案例,完成了一个普通的通知,点击通知启动了一个活动。但是那里的通知没有加入些“靓点”,这一篇就给它加入自定义的布局,完成自定义的通知。
应用:比如QQ音乐为例,当点击音乐播放的时候,手机屏幕上方就会展示播放音乐的通知,这个通知不仅仅拥有布局,而且响应点击事件,能完成上一曲下一曲的切换。今天这个小案例,就以此为背景展开。
首先,主活动布局不需要改变,还是放置两个按钮用于开启、关闭服务。
主活动中的代码做了了较大改变,如下:
package com.example.notification; 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.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.Builder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RemoteViews; public class MainActivity extends Activity { private Button btShow;
private Button btCancel;
private NotificationManager manager; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btShow = (Button) findViewById(R.id.show);
btCancel = (Button) findViewById(R.id.cancel); btShow.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// 展示通知
showNotificationNewAPI();
}
}); btCancel.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// 关闭通知
cancelNotification();
}
});
} /**新API展示通知*/
public void showNotificationNewAPI(){
/**获取通知对象*/
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); //自定义布局的通知
Notification notification = showCustomViewNotification(); //开启通知,第一个参数类似代表该通知的id,标记为1
manager.notify(1, notification);
} /**自定义布局的通知
* @return */
private Notification showCustomViewNotification() {
NotificationCompat.Builder builder = new Builder(getApplicationContext()); builder.setSmallIcon(R.drawable.notification_music_playing)//设置通知显示的图标
.setTicker("ticker")//设置通知刚刚展示时候瞬间展示的通知信息
.setWhen(System.currentTimeMillis())//设置通知何时出现,System.currentTimeMillis()表示当前时间显示
/**以上三种方式必须设置,少一项都不发展示通知*/
.setOngoing(true)//设置滑动通知不可删除
.setContent(getRemoteView());//给通知设置内容,这个内容为自定义的布局 return builder.build();
} /**获取自定义通知使用的布局View*/
private RemoteViews getRemoteView(){
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification_layout); //设置标题
remoteViews.setTextViewText(R.id.tv_title, "爱你一万年");
//设置歌手名
remoteViews.setTextViewText(R.id.tv_arties, "刘德华"); //设置点击事件
remoteViews.setOnClickPendingIntent(R.id.ll_root, getPaddingIntent());
remoteViews.setOnClickPendingIntent(R.id.ivnext, getNextPaddingIntent());
remoteViews.setOnClickPendingIntent(R.id.ivpre, getPrePaddingIntent()); return remoteViews;
} /**获取PendingIntent*/
private PendingIntent getPaddingIntent() {
//真正的意图
Intent intent = new Intent(this, DemoActivity.class);
intent.putExtra("msg", "我是从通知栏启动的!");
// 延迟意图,用于启动活动、服务、发送广播等。携带真正的意图对象
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
return pendingIntent;
} /**获取下一曲的Paddingintent*/
private PendingIntent getNextPaddingIntent() {
// 真正的意图
Intent intent = new Intent(this, DemoActivity.class);
intent.putExtra("msg", "我是从通知栏启动的!点击了下一曲按钮");
// 延迟意图,用于启动活动、服务、发送广播等。携带真正的意图对象
PendingIntent pendingIntent = PendingIntent.getActivity(this, 1,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
return pendingIntent;
} /**获取上一曲的paddingintent*/
private PendingIntent getPrePaddingIntent() {
// 真正的意图
Intent intent = new Intent(this, DemoActivity.class);
intent.putExtra("msg", "我是从通知栏启动的!点击了上一曲按钮");
// 延迟意图,用于启动活动、服务、发送广播等。携带真正的意图对象
PendingIntent pendingIntent = PendingIntent.getActivity(this, 2,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
return pendingIntent;
} /**取消通知*/
public void cancelNotification(){
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//1表示我要取消标记的通知
manager.cancel(1);
}
}
代码已经写得很想详细了。。。很显然主要是下面这个方法为核心:
private RemoteViews getRemoteView(){
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification_layout);
//设置标题
remoteViews.setTextViewText(R.id.tv_title, "爱你一万年");
//设置歌手名
remoteViews.setTextViewText(R.id.tv_arties, "刘德华");
//设置点击事件
remoteViews.setOnClickPendingIntent(R.id.ll_root, getPaddingIntent());
remoteViews.setOnClickPendingIntent(R.id.ivnext, getNextPaddingIntent());
remoteViews.setOnClickPendingIntent(R.id.ivpre, getPrePaddingIntent());
return remoteViews;
}
它让我们自定义布局,并且对布局中的数据做了数据展示与事件处理。相信它的意思很简单的就能体会。
我们把自定义的布局也要整理出来:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:gravity="center_vertical"
android:padding="8dp"
android:id="@+id/ll_root"
android:layout_height="wrap_content"
android:orientation="horizontal" > <!-- icon -->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/notification_music_playing"/> <!-- 歌曲信息 -->
<LinearLayout
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- 歌曲名 -->
<TextView
android:id="@+id/tv_title"
android:singleLine="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="歌曲名"
android:textColor="@color/white"/>
<!-- 歌手名 -->
<TextView
android:id="@+id/tv_arties"
android:singleLine="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="歌手名"
android:textColor="@color/halfwhite"/>
</LinearLayout>
<!-- 上一曲 -->
<ImageView
android:id="@+id/ivpre"
android:layout_marginRight="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon_notification_pre"/>
<!-- 下一曲 -->
<ImageView
android:id="@+id/ivnext"
android:layout_marginRight="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon_notification_next"/>
</LinearLayout>
还差一点,我们给通知上边的按钮添加了点击事件,事件目的是启动另一个Activity,所以要到这个activity获取这些数据,做一个土司处理。
另一个活动中的代码:
package com.example.notification; import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast; public class DemoActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo);
String content = getIntent().getStringExtra("msg");
Toast.makeText(getApplicationContext(), content, 0).show();
}
}
事件结果就是打印显示一行土司。
运行看看自定义通知吧!效果图如下:
Android简易实战教程--第三十八话《自定义通知NotifiCation》的更多相关文章
- Android简易实战教程--第四十八话《Android - Timer、TimerTask和Handler实现倒计时》
之前本专栏文章中的小案例有写到:第三十九话<Chronometer实现倒计时> 以及使用异步实现倒计时:第三十三话< AsyncTask异步倒计时> 本篇文章 结合Timer. ...
- Android简易实战教程--第三十一话《自定义土司》
最近有点忙,好几天不更新博客了.今天就简单点,完成自定义土司. 主布局文件代码: <RelativeLayout xmlns:android="http://schemas.andro ...
- Android简易实战教程--第三十六话《电话录音》
今天完成一个简单的电话录音功能,即接通电话后,立即录下自己打电话的声音.实现起来比较简单:一个服务,一个TelephonyManager.一个MediaRecorder就够了. 1.布局提供一个开启录 ...
- Android简易实战教程--第三十五话《音乐播放》
已经好几天不更新博客了,今天轻松一点模拟个简单的"音乐播放器".1分钟看完~ 整个简单布局,加几个控制按钮: <LinearLayout xmlns:android=&quo ...
- Android简易实战教程--第三十九话《Chronometer实现倒计时》
Android提供了实现按照秒计时的API,今天就是用这个API实现简单的倒计时. 来个布局: <?xml version="1.0" encoding="utf- ...
- Android简易实战教程--第三十九话《简单的模糊查询》
今天这一篇小案例模拟模糊查询,即输入一个字符,显示手机对应的所有存在该字符的路径. 布局: <?xml version="1.0" encoding="utf-8& ...
- Android简易实战教程--第三十四话《 自定义SeekBar以及里面的一些小知识》
转载本专栏文章,请注明出处尊重原创:博客地址http://blog.csdn.net/qq_32059827/article/details/52849676:小杨的博客 许多应用可能需要加入进度,例 ...
- Android简易实战教程--第四十六话《RecyclerView竖向和横向滚动》
Android5.X后,引入了RecyclerView,这个控件使用起来非常的方便,不但可以完成listView的效果,而且还可以实现ListView无法实现的效果.当然,在新能方便也做了大大的提高. ...
- Android简易实战教程--第三十二话《使用Lrucache和NetworkImageView加载图片》
转载本专栏每一篇博客请注明转载出处地址,尊重原创.此博客转载链接地址:小杨的博客 http://blog.csdn.net/qq_32059827/article/details/5279131 ...
随机推荐
- MySQL查询机制
在MySQL中,每当查询被发送到服务端时,服务器在执行语句之前将会进行下面的检查: 用户是否有权限执行该语句? 用户是否有权限访问目标数据? 语句的语法是否正确 如果查询通过了这三个测试,就会被传递给 ...
- 招募:Wiki 文档翻译小伙伴招募
https://github.com/dotnetcore/CAP/issues/93 为了推进 CAP 的国际化工作,为全球其他 .NET 开发者提供更加良好的文档阅读体验,现在需要对CAP wik ...
- UOJ #30. 【CF Round #278】Tourists
Description Cyberland 有 n 座城市,编号从 1 到 n,有 m 条双向道路连接这些城市.第 j 条路连接城市 aj 和 bj.每天,都有成千上万的游客来到 Cyberland ...
- ●CodeForces 280D k-Maximum Subsequence Sum
题链: http://codeforces.com/problemset/problem/280/D 题解: 神题,巨恶心.(把原来的那个dp题升级为:序列带修 + 多次询问区间[l,r]内取不超过k ...
- 【UOJ UNR #1】争夺圣杯
来自FallDream的博客,未经允许,请勿转载,谢谢. 传送门 考虑直接对每个数字,统计它会产生的贡献. 单调栈求出每个数字左边第一个大等于他的数,右边第一个大于他的 (注意只能有一边取等) 假设左 ...
- [bzoj4755][Jsoi2016]扭动的回文串
来自FallDream的博客,未经允许,请勿转载,谢谢. JYY有两个长度均为N的字符串A和B. 一个“扭动字符串S(i,j,k)由A中的第i个字符到第j个字符组成的子串与B中的第j个字符到第k个字符 ...
- UDA机器学习基础—交叉验证
交叉验证的目的是为了有在训练集中有更多的数据点,以获得最佳的学习效果,同时也希望有跟多的测试集数据来获得最佳验证.交叉验证的要点是将训练数据平分到k个容器中,在k折交叉验证中,将运行k次单独的试验,每 ...
- Unix系统的文件目录项的内容是什么,这样处理的好处是什么?
(Unix系统采用树型目录结构,而且目录中带有交叉勾链.每个目录表称为一个目录文件.一个目录文件是由目录项组成的.) 每个目录项包含16个字节,一个辅存磁盘块(512B)包含32个目录项.在目录项中, ...
- 5-15 QQ帐户的申请与登陆 (25分) HASH
实现QQ新帐户申请和老帐户登陆的简化版功能.最大挑战是:据说现在的QQ号码已经有10位数了. 输入格式: 输入首先给出一个正整数NN(\le 10^5≤105),随后给出NN行指令.每行指令的格 ...
- 阿里Java研发工程师实习面经
十分幸运 拿到阿里云的offer,感谢周围无数人对我的支持和鼓励,所以写篇面经希望可以帮助大家. 面试中,运气占很大一部分的,所以你们若是没有通过,一定不要气馁,继续加油. 每个努力的人 都值得钦佩, ...