jelly 页面 教程

您可能听说过Android Jelly Bean(API级别16)。 Google改进了许多功能并引入了新功能。 其中之一就是通知。 现在,他们通过引入媒体丰富的通知使通知更加通用。 Google提出了以下三种特殊的通知样式。 甚至开发人员也可以使用远程视图编写自己的自定义通知样式。不建议使用旧的Notification类构造函数,并引入了全新的增强版Notification。

通知类型:

  • 基本通知 –显示带有图标的简短通知。
  • 大图通知 –显示视觉内容,例如位图。
  • 大文本通知 –显示多行Textview对象。
  • 收件箱样式通知–显示任何类型的列表,例如消息,标题等。

旧的语法要求我们创建一个通知对象,但是现在Android使用构建器模式来创建通知对象。 引入了Notification.Builder类以使此任务更容易。 此类返回可根据您的要求配置的构建器对象。 引入了辅助类,例如Notification.BigPictureStyle,Notification.BigTextStyleNotification.InboxStyle 。 这些类是重新构建器类,它们采用Notification.Builder类创建的对象并像这样修改行为。

项目信息:有关项目的元数据。

平台版本: Android API级别16。
IDE: Eclipse Helios服务版本2
模拟器: Android 4.1(API 16)

先决条件:对Android应用程序框架和Intent有初步了解。

首先通过Eclipse> File> New Project> Android Application Project创建项目 将出现以下对话框。 填写必填字段,即“应用程序名称”,“项目名称”和“包名称”。 不要忘记选择Build SDK版本(在本教程中,已选择Google API 16)。 现在按下一步按钮。

出现对话框后,选择BlankActivity,然后单击下一步

填写下面显示的对话框的“活动名称”和“布局”文件名,然后单击“ 完成”按钮。

此过程将设置基本项目文件。 现在,我们将在activity_main.xml文件中添加四个按钮。 您可以使用图形布局编辑器或xml编辑器来修改布局文件。 文件的内容应如下所示。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:gravity="center_horizontal"><Buttonandroid:id="@+id/btBasicNotification"android:layout_width="fill_parent"android:layout_height="wrap_content"android:gravity="center_horizontal|center_vertical"android:onClick="sendBasicNotification"android:text="@string/btBasicNotification" android:background="@drawable/button_background"android:textColor="#000000"/><Buttonandroid:id="@+id/btBigTextNotification"android:layout_width="fill_parent"android:layout_height="wrap_content"android:gravity="center_horizontal|center_vertical"android:onClick="sendBigTextStyleNotification"android:text="@string/btBigTextNotification" android:background="@drawable/button_background"android:textColor="#000000"/><Buttonandroid:id="@+id/btBigPictureNotification"android:layout_width="fill_parent"android:layout_height="wrap_content"android:gravity="center_horizontal|center_vertical"android:onClick="sendBigPictureStyleNotification"android:text="@string/btBigPictureNotification"android:background="@drawable/button_background"android:textColor="#000000" /><Buttonandroid:id="@+id/btInboxStyleNotification"android:layout_width="fill_parent"android:layout_height="wrap_content"android:gravity="center_horizontal|center_vertical"android:onClick="sendInboxStyleNotification"android:text="@string/btInboxStyleNotification"android:background="@drawable/button_background"android:textColor="#000000"/>
</LinearLayout>

您可能已经注意到onClick方法与各个按钮相关联。 如果您不知道如何定义和使用背景文件进行查看,请忽略android:background字段。 现在,我们将定义方法sendBasicNotification,sendBigTextStyleNotification,sendBigPictureStyleNotification和sendInboxStyleNotification。 顾名思义,方法发送特定的通知。 在每种方法中,我们都将创建Notification.Builder对象,并自定义该对象。 在这里,构建器模式已用于自定义对象。 完成自定义后,调用build()方法获取通知对象。 在这个新的通知系统中,最多可以将三个动作与一个通知相关联,这些动作显示在通知内容下方。 这可以通过在构建器对象上调用addAction()方法来实现。 您在通知中看到的图标数与sendBigPictureStyleNotifcation()方法中注意到的图标数相同。 通知优先级也可以通过调用setPriority()方法来设置,如sendBigTextStyleNotification()方法所示。 在下面给出的代码中,意图已用于调用HandleNotificationActivity。

package com.example.jellybeannotificationexample;import android.app.Activity;
import android.app.Notification;
import android.app.Notification.Builder;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;public class NotificationMainActivity extends Activity {@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.activity_main, menu);return true;}public void sendBasicNotification(View view) {Notification notification = new Notification.Builder(this).setContentTitle("Basic Notification").setContentText("Basic Notification, used earlier").setSmallIcon(R.drawable.ic_launcher_share).build();notification.flags |= Notification.FLAG_AUTO_CANCEL;NotificationManager notificationManager = getNotificationManager();notificationManager.notify(0, notification);}public void sendBigTextStyleNotification(View view) {String msgText = "Jeally Bean Notification example!! "+ "where you will see three different kind of notification. "+ "you can even put the very long string here.";NotificationManager notificationManager = getNotificationManager();PendingIntent pi = getPendingIntent();Builder builder = new Notification.Builder(this);builder.setContentTitle("Big text Notofication").setContentText("Big text Notification").setSmallIcon(R.drawable.ic_launcher).setAutoCancel(true);.setPriority(Notification.PRIORITY_HIGH).addAction(R.drawable.ic_launcher_web, "show activity", pi);Notification notification = new Notification.BigTextStyle(builder).bigText(msgText).build();notificationManager.notify(0, notification);}public void sendBigPictureStyleNotification(View view) {PendingIntent pi = getPendingIntent();Builder builder = new Notification.Builder(this);builder.setContentTitle("BP notification")// Notification title.setContentText("BigPicutre notification")// you can put subject line..setSmallIcon(R.drawable.ic_launcher)// Set your notification icon here..addAction(R.drawable.ic_launcher_web, "show activity", pi).addAction(R.drawable.ic_launcher_share,"Share",PendingIntent.getActivity(getApplicationContext(), 0,getIntent(), 0, null));// Now create the Big picture notification.Notification notification = new Notification.BigPictureStyle(builder).bigPicture(BitmapFactory.decodeResource(getResources(),R.drawable.big_picture)).build();// Put the auto cancel notification flagnotification.flags |= Notification.FLAG_AUTO_CANCEL;NotificationManager notificationManager = getNotificationManager();notificationManager.notify(0, notification);}public void sendInboxStyleNotification(View view) {PendingIntent pi = getPendingIntent();Builder builder = new Notification.Builder(this).setContentTitle("IS Notification").setContentText("Inbox Style notification!!").setSmallIcon(R.drawable.ic_launcher).addAction(R.drawable.ic_launcher_web, "show activity", pi);Notification notification = new Notification.InboxStyle(builder).addLine("First message").addLine("Second message").addLine("Thrid message").addLine("Fourth Message").setSummaryText("+2 more").build();// Put the auto cancel notification flagnotification.flags |= Notification.FLAG_AUTO_CANCEL;NotificationManager notificationManager = getNotificationManager();notificationManager.notify(0, notification);}public PendingIntent getPendingIntent() {return PendingIntent.getActivity(this, 0, new Intent(this,HandleNotificationActivity.class), 0);}public NotificationManager getNotificationManager() {return (NotificationManager) getSystemService(NOTIFICATION_SERVICE);}
}

我们定义了基本的HandleNotificationActivity,当为此活动触发意图时,它仅显示一条简单消息。 该文件的内容如下。

package com.example.jellybeannotificationexample;import android.app.Activity;
import android.os.Bundle;public class HandleNotificationActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.handle_notification_activity);}
}

相应的布局文件(handle_notification_activity.xml)如下所示

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:gravity="center_horizontal|center_vertical" ><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/tvHandleNotification"android:textSize="20dp"android:textStyle="bold|italic" /></LinearLayout>

现在,您必须定义Android manifiedt文件。 HandleNotificationActivity应该包含在清单文件中,然后将意图过滤器用于 这项活动。

<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.jellybeannotificationexample"android:versionCode="1"android:versionName="1.0" ><uses-sdkandroid:minSdkVersion="16"android:targetSdkVersion="16" /><applicationandroid:icon="@drawable/ic_launcher"android:label="@string/app_name"><activityandroid:name=".NotificationMainActivity"android:label="@string/title_activity_main" ><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activityandroid:name=".HandleNotificationActivity"android:label="@string/title_activity_main" ><intent-filter><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>

一旦完成编码,就执行它。 您将看到如下图所示的应用程序。 单击按钮后,您将在屏幕上方看到相应的通知。 如果您向下拖动通知,则可以看到整个消息和相应的图标。 下面的图片是被下拉时的通知。

大文字样式
应用
收件箱样式
大图风格
基本通知

如果您想进一步了解源代码,可以在这里找到。

参考:我们的JCG合作伙伴 Rakesh Cusat在Code4Reference博客上提供了有关新的Android Jelly Bean通知的教程 。

翻译自: https://www.javacodegeeks.com/2012/08/android-jelly-bean-notification-tutorial.html

jelly 页面 教程

jelly 页面 教程_Android Jelly Bean通知教程相关推荐

  1. Android Jelly Bean通知教程

    您可能已经听说过Android Jelly Bean(API级别16). Google改进了许多功能并引入了新功能. 其中之一就是通知. 现在,他们通过引入媒体丰富的通知使通知更加通用. Google ...

  2. jelly_Android Jelly Bean通知教程

    jelly 您可能听说过Android Jelly Bean(API级别16). Google改进了许多功能并引入了新功能. 其中之一就是通知. 现在,他们通过引入媒体丰富的通知使通知更加通用. Go ...

  3. android实例教程_Android内部存储示例教程

    android实例教程 Today we will look into android internal storage. Android offers a few structured ways t ...

  4. jelly 博客_Android –使用Jelly Bean通知

    jelly 博客 最近的帖子是很久以前的! 我最近很忙...但是从Android中学到了很多! (还有Node js--我爱上了这个平台!) 我为跟随我的人决定的其他事情是,从现在开始,文章将仅以英文 ...

  5. Android –使用Jelly Bean通知

    最近的帖子是很久以前的! 我最近很忙...但是从Android中学到了很多! (还有Node js--我爱上了这个平台!) 我为跟随我的人决定的其他事情是,从现在开始,文章将仅以英文显示(对不起= / ...

  6. Spring IoC,Spring Bean示例教程

    Spring IoC,Spring Bean示例教程 欢迎来到Spring IoC示例教程.Spring Framework基于Inversion of Control原理.依赖注入是在应用程序中实现 ...

  7. java手工注入bean_java相关:Spring中如何动态注入Bean实例教程

    java相关:Spring中如何动态注入Bean实例教程 发布于 2020-3-8| 复制链接 摘记: 前言在Spring中提供了非常多的方式注入实例,但是由于在初始化顺序的不同,基于标注的注入方式, ...

  8. React + webpack 开发单页面应用简明中文文档教程(一)一些基础概念

    React + webpack 开发单页面应用简明中文文档教程(一)一些基础概念 React 入门系列教程导航 React + webpack 开发单页面应用简明中文文档教程(一)一些基础概念 Rea ...

  9. 一篇文章了解Github和Git教程-AndroidStudio上传Github教程

    前言 为了方便保存自己的代码,下班后可以回家继续进行,自己的码农工作,介绍一下Github. 什么是Github呢? 作为一个编程人员,我觉得得了解一下Github吧! 当然,如果你放弃了码农或者技术 ...

最新文章

  1. OpenCV代码提取:merge/split函数的实现
  2. 创建一个栈存储结构,并且写入一些对栈的基本的操作
  3. ps抠头发插件_「福利」PS抠图神级插件——VertusFluid Mask
  4. django orm级联_Django数据表关联关系映射(一对一、一对多、多对多)
  5. 微软在Build 2019大会上发布Fluid Framework协作平台
  6. 解决dom4j java.lang.NoClassDefFoundError: org/jaxen/JaxenException
  7. Android中样式及主题
  8. 栈——验证栈序列(洛谷 P4387)
  9. 大学物理光学思维导图_在线思维导图软件安利:简单、方便画图,大学生、小学生都能用...
  10. matlab2c使用c++实现matlab函数系列教程-randn函数
  11. python 线程同步_python线程同步(2)
  12. 这个时代,开发简单多了
  13. 基于卷积网络的度量学习
  14. 独立访客数UV、访问量PV和IP数的区别
  15. mysql查询数据1168_mysqldump 1168 error
  16. 在寂静的夜中、独自沉沦_悲伤QQ个人签名
  17. 您选择的分区不支持无损调整容量操作
  18. 《哈利波特》购书最低折扣
  19. Could not find or load main class org.apache.hadoop.mapreduce.v2.app.MRAppMaster
  20. sentinel 实时监控服务

热门文章

  1. 《第七天》之第六天鼠妹和伍超为什么活不下去
  2. 美尼尔综合症的中药调理
  3. cmake:macro、endmacro
  4. 新公司新入手的一个新项目的心路历程
  5. ucore_lab3_虚拟内存管理
  6. 学校计算机语音室管理制度,多媒体、语音室管理制度
  7. 电商数仓笔记2_用户行为采集(数据采集模块)
  8. mac os 秒开 word doc 文档
  9. 微型计算机主板usb电源损坏,电脑主板usb不供电的原因及解决方法
  10. 白杨SEO:评论推广引流,学会这招,既可锻炼执行力又可拓展思路!