Android UI基础——Notification控件
2016-04-07 08:52
806 查看
Notification是手机状态栏的通知,显示在手机最上方,是一种具有全局效果的通知,程序一般通过NotificationManager服务来发送Notification。
接下来通过具体的代码实现Notification来介绍属性等内容,代码中会通过三个按钮的监听事件来发送Notification,系统默认的的Notification使用增加的Notification.Builder类,使用该类创建Notification对象。自定义的Notification就使用Notification类创建对象。两种创建方法还是有点区别的,可以在代码中慢慢体会,代码如下:
注:PendingIntent的详细介绍,引用连接如下:
http://blog.csdn.net/zeng622peng/article/details/6180190
自定义Notification的布局代码如下:
效果图如下:
接下来通过具体的代码实现Notification来介绍属性等内容,代码中会通过三个按钮的监听事件来发送Notification,系统默认的的Notification使用增加的Notification.Builder类,使用该类创建Notification对象。自定义的Notification就使用Notification类创建对象。两种创建方法还是有点区别的,可以在代码中慢慢体会,代码如下:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Button mButton1,mButton2,mButton3; private NotificationManager manager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //调用getSystemService(NOTIFICATION_SERVICE)方法获取系统的NotificationManager服务 manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); mButton1 = (Button) findViewById(R.id.button_default); mButton2 = (Button) findViewById(R.id.button_custom); mButton3 = (Button) findViewById(R.id.button_clean); mButton1.setOnClickListener(this); mButton2.setOnClickListener(this); mButton3.setOnClickListener(this); } @TargetApi(Build.VERSION_CODES.JELLY_BEAN) @Override public void onClick(View v) { switch (v.getId()){ case R.id.button_default: Notification.Builder builder = new Notification.Builder(this);//创建Notification.builder对象 builder.setTicker("有一条新消息");//设置显示在状态栏的通知提示信息 builder.setSmallIcon(R.mipmap.ic_launcher);//设置通知的图标 builder.setContentTitle("Notification标题");//设置通知内容的标题 builder.setContentText("这是Notification的内容");//设置通知内容 builder.setAutoCancel(true);//设置打开通知,该通知自动消失 //PendingIntent是Intent的包装,是在外部执行的用于跳转页面,但不是马上跳转,在下面会有详细解释 PendingIntent pendingIntent = PendingIntent.getActivities(this,0, new Intent[]{new Intent(MainActivity.this, MainActivity.class)},0); builder.setContentIntent(pendingIntent); manager.notify(0,builder.build()); break; case R.id.button_custom: Notification notification = new Notification();//创建Notification对象 notification.icon = R.mipmap.dialog_1; notification.tickerText = "自定义的一条新消息"; notification.flags = Notification.FLAG_AUTO_CANCEL;//设置打开通知,该通知自动消失 //RemoteViews类描述了一个View对象能够显示在其他进程中,可以融合从一个 layout资源文件实现布局 RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.notify); remoteViews.setImageViewResource(R.id.image,R.mipmap.ic_launcher); remoteViews.setTextViewText(R.id.text1,"自定义notify的内容"); remoteViews.setTextViewText(R.id.text2,"自定义notify第二行内容"); PendingIntent pendingIntent1 = PendingIntent.getActivities(this,1, new Intent[]{new Intent(MainActivity.this, MainActivity.class)},0); notification.contentIntent = pendingIntent1; notification.contentView = remoteViews; manager.notify(2,notification); break; case R.id.button_clean: manager.cancelAll();//删除通知 } } }
注:PendingIntent的详细介绍,引用连接如下:
http://blog.csdn.net/zeng622peng/article/details/6180190
自定义Notification的布局代码如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@color/colorAccent"> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <LinearLayout android:id="@+id/line" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:layout_toRightOf="@+id/image"> <TextView android:id="@+id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/colorPrimary"/> <TextView android:id="@+id/text2" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </RelativeLayout>
效果图如下:
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories