Android简易实战教程--第三十八话《自定义通知NotifiCation》
来源:互联网 发布:淘宝知识产权侵权标准 编辑:程序博客网 时间:2024/06/05 00:29
上一篇小案例,完成了一个普通的通知,点击通知启动了一个活动。但是那里的通知没有加入些“靓点”,这一篇就给它加入自定义的布局,完成自定义的通知。
应用:比如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;@Overrideprotected 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() {@Overridepublic void onClick(View v) {// 展示通知showNotificationNewAPI();}});btCancel.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// 关闭通知cancelNotification();}});}/**新API展示通知*/public void showNotificationNewAPI(){/**获取通知对象*/manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);//自定义布局的通知Notification notification = showCustomViewNotification();//开启通知,第一个参数类似代表该通知的id,标记为1manager.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 {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.demo);String content = getIntent().getStringExtra("msg");Toast.makeText(getApplicationContext(), content, 0).show();}}事件结果就是打印显示一行土司。
运行看看自定义通知吧!效果图如下:
1 0
- Android简易实战教程--第三十八话《自定义通知NotifiCation》
- Android简易实战教程--第三十七话《NotifiCation》
- Android简易实战教程--第三十一话《自定义土司》
- Android简易实战教程--第二十八话《加载大图片》
- Android简易实战教程--第三十四话《 自定义SeekBar以及里面的一些小知识》
- Android简易实战教程--第三话《自己实现打电话》
- Android简易实战教程--第三十三话《 AsyncTask异步倒计时》
- Android简易实战教程--第三十五话《音乐播放》
- Android简易实战教程--第三十六话《电话录音》
- Android简易实战教程--第三十九话《Chronometer实现倒计时》
- Android自定义Notification通知
- Android实战简易教程<十八>(ViewPager组件详解)
- Android实战简易教程-第三十八枪(模仿腾讯QQ的网络状态提示和设置功能实现)
- Android简易实战教程--第四十八话《Android - Timer、TimerTask和Handler实现倒计时》
- Android简易实战教程--第十七话《自定义彩色环形进度条》
- Android实战简易教程-第三十二枪(自定义View登录注册界面EditText-实现一键清空)
- Android实战简易教程-第三十二枪(自定义View登录注册界面EditText-实现一键清空)
- Android实战简易教程-第三十三枪(自定义View实现控件晃动提示效果)
- MySQL数据库企业生产常用4种安装方法介绍和选择
- 二叉树遍历
- 重装系统U盘版本
- JavaScript实现tab标签-解决循环bug问题
- EM算法学习笔记
- Android简易实战教程--第三十八话《自定义通知NotifiCation》
- hdu 5912 Fraction -2016中国大学生程序设计竞赛(长春)
- 面试经历
- 【egret】 egret游戏中添加“长按识别二维码”
- MAC 下修改mysql默认密码方法
- SRP Protocol Design
- 对N行N列二维数组的每一行排序,偶数行(0当作偶数)由小到大排序,奇数行由大到小排序
- C++顺序容器
- 《C程序设计》