Notification自定义界面
来源:互联网 发布:c语言pow函数 编辑:程序博客网 时间:2024/05/29 07:32
前言
之前在做一个手机的播放器,需要做到在通知栏显示控制播放的界面,如下:
这是让服务在前台运行就可以实现的(可以参考我的前一篇文章Service在前台运行),今天我们就要实现Notification的自定义界面,当然就不实现如上图所示的了,而是下面一个简单的界面,随自己的需要搭建自己想要的界面。
可以看到,我实现了一个简单的界面,包括一个ImageView和Button,下面我就说说该如何实现它,其实很简单。
实现
首先我们要准备一个界面文件:
notification.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_marginTop="10dp" android:layout_marginBottom="10dp" android:background="#333300" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:paddingLeft="20dp" android:layout_width="70dp" android:layout_height="50dp" android:src="@drawable/ic_qiuda" /> <Button android:layout_marginLeft="30dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="点击我" /></LinearLayout>
然后新建一个Service的子类,MyService:
public class MyService extends Service { public static final String TAG = "MyService"; @Override public void onCreate() { super.onCreate(); Notification notification = new Notification(R.drawable.ic_launcher, "JcMan", System.currentTimeMillis()); RemoteViews view = new RemoteViews(getPackageName(),R.layout.notification); notification.contentView = view; startForeground(1, notification); } @Override public IBinder onBind(Intent intent) { return null; }}
可以看到,在onCreate方法里面我们设置界面的不再是用LayoutInflater来得到界面,而是用RemoteViews来new出来一个界面,构造方法传入的是包名和界面资源的ID即可,然后我们把notification.contentView设置成我们new出来的自定义界面即可。
小结
普通的Notification可以用来进行通知,但是当有特殊需要的时候,我们就需要自定义界面,而且有时候还需要对自定义的界面添加点击的方法,如在上图的界面里面有一个Button如何对Button的点击事件进行响应,这是一个比较难的问题,因为这不是简单的setOnClickListener就可以的,需要另外的实现,需要用到广播机制,我将会在下一篇文章中说明如何为Notification的自定义界面添加点击事件。
0 0
- Notification自定义界面
- 自定义 Notification中 布局界面
- 自定义Notification 聊天界面的编写
- Notification&&自定义Notification
- 自定义notification
- 自定义Notification
- 自定义Notification
- 自定义Notification~~
- 自定义Notification
- 自定义Notification
- 自定义Notification
- 自定义Notification
- 0831Android基础自定义Notification+仿QQ聊天界面的小Demo(上)
- 自定义的Notification
- Android自定义Notification
- Android自定义Notification
- 使用自定义的 Notification
- 自定义Notification之进度条
- 初学者遭遇离奇错误——求两点间的距离(C++)
- DB9针型:RS485输出信号及接线端子引脚分配
- 快速滚动滑块(Fast ScrollThumb)的实现
- day10
- UESTC 94 Bracket Sequence(线段树的区间更新)
- Notification自定义界面
- 代码之美-2[转]
- html基础1
- 《深入浅出struts2》--第七章,类型转换-支持与Map一起使用
- PAT (Advanced Level) 1056. Mice and Rice (25) 模拟比赛,用queue辅助
- webapp
- mysql编码设置
- spring中@value注解需要注意
- jQuery判断浏览器是移动端还是电脑端自动跳转