`
007007jing
  • 浏览: 42322 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

android2.3 api demo 学习系列(21)--App/Notification/Incoming Message

阅读更多

现在我们开始学习android的Status Bar Notifications相关内容

1、首先我们来实现一个Notification:在status bar添加一个图片和信息,并让手机震动。用户打开status bar看到更详细的信息,点击该Notification打开一个activity

首先我们来看实现代码:

 

 protected void showNotification() {
	        // look up the notification manager service
	        NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

	        // The details of our fake message
	        CharSequence from = "angie";
	        CharSequence message = "今天加班记得订饭啊。。。";

	        // The PendingIntent to launch our activity if the user selects this notification
	        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
	                new Intent(this, IncomingMessageView.class), 0);

	        // The ticker text, this uses a formatted string so our message could be localized
	        String tickerText = getString(R.string.app_notification_incomingmessage_view_msg1, message);

	        // construct the Notification object.
	        Notification notif = new Notification(R.drawable.main, tickerText,
	                System.currentTimeMillis());

	        // Set the info for the views that show in the notification panel.
	        notif.setLatestEventInfo(this, from, message, contentIntent);

	        // after a 100ms delay, vibrate for 250ms, pause for 100 ms and
	        // then vibrate for 500ms.
	        notif.vibrate = new long[] { 100, 250, 100, 500};

	        // Note that we use R.layout.incoming_message_panel as the ID for
	        // the notification.  It could be any integer you want, but we use
	        // the convention of using a resource id for a string related to
	        // the notification.  It will always be a unique number within your
	        // application.
	        nm.notify(R.string.app_notification_incomingmessage_view_msg1, notif);
	    }

 

 apidemo里面有详细的注释说明,能很轻松的理解代码的意图。

我们来看下效果图:


 

相关知识:

1、sdk中关于notification的说明

我们可以使用activity和service来创建一个notification,由于activity可以和用户交互,一般我们更多的是service来使用notification提醒用户。

我们通过Notification的实例来配置notification的图片,信息,intent以及一些其他的附加信息。android的系统是使用NotificationManager来执行和管理Notification,我们不能直接的实例化NotificationManager,必须通过getSystemService()来获取它的引用。将我们得Notification通过notify()传递进去。下面我们来看下创建一个notification的步骤

1)获得 NotificationManager引用:

String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
2)实例化
 Notification:
int icon = R.drawable.notification_icon;
CharSequence tickerText = "Hello";
long when = System.currentTimeMillis();

Notification notification = new Notification(icon, tickerText, when);
 3)定义notification的 message 和 PendingIntent:
Context context = getApplicationContext();
CharSequence contentTitle = "My notification";
CharSequence contentText = "Hello World!";
Intent notificationIntent = new Intent(this, MyClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
 4)将notification递交给NotificationManager
private static final int HELLO_ID = 1;

mNotificationManager.notify(HELLO_ID, notification);
  2、管理notification
我们通过 NotificationManager.notify(int, Notification)交付我们得notification给NotificationManager。全第一个参数为notification的唯一标示,第二个参数为notification对象。我们可以通过第一个参数来修改删除notification。
3、修改更新notification。通过再次调用setLateEventInfo方法可以修改除了上下文中得标题文字外的所有notification属性。修改后再次notify()就ok
4、自定义notification的layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp" >
    <ImageView android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="10dp" />
    <TextView android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/image"
        style="@style/NotificationTitle" />
    <TextView android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/image"
        android:layout_below="@id/title"
        style="@style/NotificationText" />
</RelativeLayout>
 我们注意到textview的style属性,在版本2.3之前我们可以这么定义
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="NotificationText">
      <item name="android:textColor">?android:attr/textColorPrimary</item>
    </style>
    <style name="NotificationTitle">
      <item name="android:textColor">?android:attr/textColorPrimary</item>
      <item name="android:textStyle">bold</item>
    </style>
    <!-- If you want a slightly different color for some text,
         consider using ?android:attr/textColorSecondary -->
</resources>
 2.3之后:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="NotificationText" parent="android:TextAppearance.StatusBar.EventContent" />
    <style name="NotificationTitle" parent="android:TextAppearance.StatusBar.EventContent.Title" />
</resources>
 代码中加载自定义样式
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout);
contentView.setImageViewResource(R.id.image, R.drawable.notification_image);
contentView.setTextViewText(R.id.title, "Custom notification");
contentView.setTextViewText(R.id.text, "This is a custom layout");
notification.contentView = contentView;
 自定义notification中无需再调用setLatestEventInfo
Intent notificationIntent = new Intent(this, MyClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
mNotificationManager.notify(CUSTOM_VIEW_ID, notification);
 

 

 

  • 大小: 44.3 KB
  • 大小: 145.8 KB
分享到:
评论

相关推荐

    react-native-in-app-notification-master.rar

    通过对“react-native-in-app-notification-master”的源码进行深入学习,开发者不仅可以了解如何在React Native中实现应用内通知,还能提升对React Native框架、组件化开发以及跨平台编程的理解。此外,研究源码也...

    Android ApiDemos示例解析(26):App->Notification->IncomingMessage

    本文将深入解析`ApiDemos`中的一个特定示例——`App-&gt;Notification-&gt;IncomingMessage`,帮助开发者更好地理解和应用Android的通知功能。 通知(Notification)是Android系统中一种关键的用户界面元素,它在状态栏中...

    android-cts-2.3_r13-linux_x86-arm.zip

    CTS在此版本中的作用是验证设备是否能够正确执行Android API级别9(对应2.3)的各种功能和API,包括但不限于: 1. **UI组件测试**:CTS包含了对各种UI组件如Activity、Service、BroadcastReceiver和ContentProvider...

    react-native-push-notification.zip

    React Native Push Notification是一个用于在React Native应用中实现本地通知和远程推送通知的库。这个库允许开发者在iOS和Android平台上集成推送通知功能,为用户提供实时的消息提醒,增强用户体验。下面将详细讲解...

    Android代码-react-native-push-notification

    npm install --save react-native-push-notification react-native link NOTE: For Android, you will still have to manually update the AndroidManifest.xml (as below) in order to use Scheduled Notifications...

    react-native-in-app-notification:React Native的可自定义应用内通知组件

    react-native-in-app-notification的基本外观: 您可以使用自定义组件来使react-native-in-app-notification : 安装 yarn add react-native-in-app-notification 要么 npm install react-native-in-app-...

    Android 多种android控件的Demo-IT计算机-毕业设计.zip

    本项目“Android多种android控件的Demo”是一个毕业设计学习资源,旨在帮助开发者熟悉并掌握Android控件的应用。下面将对这个项目中的主要知识点进行详细讲解。 1. **布局管理器(Layouts)**: - **线性布局...

    android api demo讲解

    ##### (26) App-&gt;Notification-&gt;IncomingMessage - **目的**:演示如何处理收到的消息通知。 - **主要内容**: - 使用NotificationManager创建通知。 - 处理消息到达时的通知逻辑。 ##### (27) App-&gt;Notification...

    Android-21 Android SDK platforms 21(Android5.0)

    **Android-21 Android SDK Platforms 21 (Android 5.0) 知识点详解** Android 5.0(代号Lollipop)是Google在2014年推出的一个重大更新,它对Android系统进行了许多重要的改进和优化,不仅提升了用户体验,也增强了...

    Laravel开发-laravel-mobile-notification

    本文将深入探讨“Laravel开发-laravel-mobile-notification”这一主题,特别是如何利用Laravel/Lumen发送推送通知到移动设备,包括Apple Push Notification (APN) 和 Google Cloud Messaging (GCM)。 首先,Laravel...

    Android代码-各种DemoApp

    【Android代码-各种DemoApp】是一个集合了众多Android应用程序示例的项目,旨在帮助开发者学习和理解Android平台上的编程实践。这个项目包含了多种类型的Demo应用,涵盖了Android开发的基础到高级特性,是Android...

    android版本更新

    在Android平台上,版本更新是应用保持最新特性和修复问题的关键环节。本文将深入探讨如何实现一个基础的Android版本更新机制,特别关注通知栏显示更新进度的功能。这对于初次接触这一领域的开发者来说尤其有用。 ...

    vue-notification:使用Vue3的NotificationToast组件

    安装使用yarn或npm将软件包安装为项目依赖项: $ yarn add @dafcoe/vue-notification--- or ---$ npm install --save @dafcoe/vue-notification用法全局(在main.js / main.ts文件上)或本地(在组件上)导入...

    Android-react-native-push-notification.zip

    Android-react-native-push-notification.zip,响应本机本地和远程通知,安卓系统是谷歌在2008年设计和制造的。操作系统主要写在爪哇,C和C 的核心组件。它是在linux内核之上构建的,具有安全性优势。

    Android API Demo 源码

    Android API Demo 是一套由Google官方提供的示例代码库,旨在帮助开发者理解和学习Android平台的各种API功能。这个源码集合涵盖了从基础UI组件到高级服务和网络通信的多种技术,是Android开发者的宝贵参考资料。通过...

    Laravel开发-laravel-push-notification Push Notification 服务端支持

    在本文中,我们将深入探讨如何使用 Laravel 框架中的 "laravel-push-notification" 扩展包来实现 Push Notification 的服务端支持。Push Notification 是移动应用中常见的功能,用于向用户实时发送消息、提醒或者...

    Android_api_demo

    ##### (26) App -&gt; Notification -&gt; IncomingMessage - **概述**:这部分展示了如何实现接收消息的通知功能。 - **技术点**: - **NotificationCompat.Builder**:使用 NotificationCompat.Builder 构建通知。 - *...

    Android 通知(notification)简单实用Demo,包含点击功能

    这个"Android 通知(notification)简单实用Demo"提供了一个基础的实现,包括点击功能,非常适合初学者理解和实践。 一、通知的基本结构 一个Android通知通常由以下几个部分组成: 1. **通知图标**:显示在状态栏...

    (0092)-iOS/iPhone/iPAD/iPod源代码-状态栏(Status Bar)-Discreet Notification View

    "在窗口顶端或者下端弹出自定义的提示视图(notification View)。例如,图中上方的黑色标签“This is the notification.""。可用于显示当前app的状态,用户操作的提示等等。" 注意:请在Mac下解压使用

    (0178)-iOS/iPhone/iPAD/iPod源代码-弹出视图(Popup View)-Notification Manager

    在窗口顶端弹出自定义的提示视图(notification View)。弹出的notification view可以自动隐藏。如果有多个notification view,则进行排队,按顺序显示。可用于显示当前app的状态,用户操作的提示、提醒通知等等。...

Global site tag (gtag.js) - Google Analytics