jelly
通知类型:
- 基本通知–显示带有图标的简短通知。
- 大图通知–显示视觉内容,例如位图。
- 大文本通知–显示多行Textview对象。
- 收件箱样式通知–显示任何类型的列表,例如消息,标题等。
旧的语法要求我们创建一个通知对象,但是现在Android使用构建器模式来创建通知对象。 引入了Notification.Builder类以使此任务更容易。 此类返回可根据您的要求配置的构建器对象。 引入了辅助类,例如Notification.BigPictureStyle,Notification.BigTextStyle和Notification.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">
<Button
android: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"
/>
<Button
android: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"
/>
<Button
android: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" />
<Button
android: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 {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public 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 flag
notification.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 flag
notification.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 {
@Override
protected 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" >
<TextView
android: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-sdk
android:minSdkVersion="16"
android:targetSdkVersion="16" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<activity
android: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>
<activity
android: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