上一篇小案例,完成了一个普通的通知,点击通知启动了一个活动。但是那里的通知没有加入些“靓点”,这一篇就给它加入自定义的布局,完成自定义的通知。

应用:比如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》的更多相关文章

  1. Android简易实战教程--第四十八话《Android - Timer、TimerTask和Handler实现倒计时》

    之前本专栏文章中的小案例有写到:第三十九话<Chronometer实现倒计时> 以及使用异步实现倒计时:第三十三话< AsyncTask异步倒计时> 本篇文章 结合Timer. ...

  2. Android简易实战教程--第三十一话《自定义土司》

    最近有点忙,好几天不更新博客了.今天就简单点,完成自定义土司. 主布局文件代码: <RelativeLayout xmlns:android="http://schemas.andro ...

  3. Android简易实战教程--第三十六话《电话录音》

    今天完成一个简单的电话录音功能,即接通电话后,立即录下自己打电话的声音.实现起来比较简单:一个服务,一个TelephonyManager.一个MediaRecorder就够了. 1.布局提供一个开启录 ...

  4. Android简易实战教程--第三十五话《音乐播放》

    已经好几天不更新博客了,今天轻松一点模拟个简单的"音乐播放器".1分钟看完~ 整个简单布局,加几个控制按钮: <LinearLayout xmlns:android=&quo ...

  5. Android简易实战教程--第三十九话《Chronometer实现倒计时》

    Android提供了实现按照秒计时的API,今天就是用这个API实现简单的倒计时. 来个布局: <?xml version="1.0" encoding="utf- ...

  6. Android简易实战教程--第三十九话《简单的模糊查询》

    今天这一篇小案例模拟模糊查询,即输入一个字符,显示手机对应的所有存在该字符的路径. 布局: <?xml version="1.0" encoding="utf-8& ...

  7. Android简易实战教程--第三十四话《 自定义SeekBar以及里面的一些小知识》

    转载本专栏文章,请注明出处尊重原创:博客地址http://blog.csdn.net/qq_32059827/article/details/52849676:小杨的博客 许多应用可能需要加入进度,例 ...

  8. Android简易实战教程--第四十六话《RecyclerView竖向和横向滚动》

    Android5.X后,引入了RecyclerView,这个控件使用起来非常的方便,不但可以完成listView的效果,而且还可以实现ListView无法实现的效果.当然,在新能方便也做了大大的提高. ...

  9. Android简易实战教程--第三十二话《使用Lrucache和NetworkImageView加载图片》

    转载本专栏每一篇博客请注明转载出处地址,尊重原创.此博客转载链接地址:小杨的博客    http://blog.csdn.net/qq_32059827/article/details/5279131 ...

随机推荐

  1. 学习React系列(十)——Render Props

    解决问题:将行为封装,供多个组件使用(在多个组件之间分享某段代码) 组件中的props属性中包含一个"render"属性(该属性为一个返回值为元素的方法),然后在该组件的rende ...

  2. BST讲解

    BST 第一步,什么是BST,所谓BST就是满足一种特定性质的二叉树,这个性质一般情况是当前节点的权值比他的左子树的所有点的权值大,比他的右子树的所有点的权值小,满足这样性质的二叉树就称为BST,下面 ...

  3. AXIOS源代码重点难点分析

    摘要 vue使用axios进行http通讯,类似jquery/ajax的作用,类似angular http的作用,axios功能强大,使用方便,是一个优秀的http软件,本文旨在分享axios源代码重 ...

  4. 在iview的Table中添加Select(render)

    首先对Render进行分析,在iview官方的文档中,找到了table插入Button的例子: { title: 'Action', key: 'action', width: 150, align: ...

  5. UVA 5009 Error Curves

    Problem Description Josephina is a clever girl and addicted to Machine Learning recently. She pays m ...

  6. [HEOI2016]排序

    题目描述 在2016年,佳媛姐姐喜欢上了数字序列.因而他经常研究关于序列的一些奇奇怪怪的问题,现在他在研究一个难题,需要你来帮助他.这个难题是这样子 的:给出一个1到n的全排列,现在对这个全排列序列进 ...

  7. ●BZOJ 2752 [HAOI2012]高速公路(road)

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2752题解: 期望,线段树. 把每个路段看成一个点,那么对于l~R的操作,就可以转化为对l~r ...

  8. hdu 5919 主席树(区间不同数的个数 + 区间第k大)

    Sequence II Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Tot ...

  9. [bzoj4162]shlw loves matrix II

    来自FallDream的博客,未经允许,请勿转载,谢谢 给定矩阵k*k的矩阵M,请计算 M^n,并将其中每一个元素对 1000000007 取模输出. k<=50 n<=2^10000 考 ...

  10. Thinkphp中的U函数(Thinkphp3.2.3版本)

    U函数的作用是根据当前的URL设置生成对应的URL地址,使用U函数可以确保项目在移植过程中不受环境的影响. U方法的定义规则如下(方括号内参数根据实际应用决定): U('地址表达式',['参数'],[ ...