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

android2.3 api demo 学习系列(22)--App/Notification/Notifying Service Controller

阅读更多

因为还没有看到service的demo,这里先不对service做过多的介绍,本demo的主要意图是通过service发送notification

1、在service的onCreate方法中启动新的线程来发送notification

@Override
    public void onCreate() {
        mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        // Start up the thread running the service.  Note that we create a
        // separate thread because the service normally runs in the process's
        // main thread, which we don't want to block.
        Thread notifyingThread = new Thread(null, mTask, "NotifyingService");
        mCondition = new ConditionVariable(false);
        notifyingThread.start();
    }

 2、具体执行

private Runnable mTask = new Runnable() {
        public void run() {
            for (int i = 0; i < 4; ++i) {
                showNotification(R.drawable.list,
                        R.string.app_notification_service_happy);
                if (mCondition.block(5 * 1000)) 
                    break;
                showNotification(R.drawable.launcher,
                        R.string.app_notification_service_sad);
                if (mCondition.block(5 * 1000)) 
                    break;
            }
            // Done with our work...  stop the service!
            NotifyingService.this.stopSelf();
        }
    };

 3、notification的实例化和发送

private void showNotification(int moodId, int textId) {
        // In this sample, we'll use the same text for the ticker and the expanded notification
        CharSequence text = getText(textId);

        // Set the icon, scrolling text and timestamp.
        // Note that in this example, we pass null for tickerText.  We update the icon enough that
        // it is distracting to show the ticker text every time it changes.  We strongly suggest
        // that you do this as well.  (Think of of the "New hardware found" or "Network connection
        // changed" messages that always pop up)
        Notification notification = new Notification(moodId, getText(R.string.app_notification_service_title), System.currentTimeMillis());

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

        // Set the info for the views that show in the notification panel.
        notification.setLatestEventInfo(this, getText(R.string.app_notification_service_title),
                       text, contentIntent);

        // Send the notification.
        // We use a layout id because it is a unique number.  We use it later to cancel.
        mNM.notify(MOOD_NOTIFICATIONS, notification);
    }

 里面涉及到的notification的内容基本在上一篇文章都有详细的介绍:android2.3 api demo 学习系列(21)--App/Notification/Incoming Message

效果图:


  • 大小: 120.9 KB
0
2
分享到:
评论

相关推荐

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

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

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

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

    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...

    Android代码-android-upload-service

    Android Upload Service Easily upload files in the background with automatic Android Notification Center progress indication. Download the latest demo app APK which uses the library and try it ...

    android api demo讲解

    ### Android API Demo详解 #### 一、概述 本篇文章旨在为初学者提供一套全面而深入的Android API Demo解析,帮助大家更好地理解Android开发中的各种基础知识和技术细节。文章将按照给出的目录顺序,逐一分析每个...

    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版本更新

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

    Android代码-各种DemoApp

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

    react-native-push-notification.zip

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

    Laravel开发-laravel-mobile-notification

    1. **Apple Push Notification Service (APN)**:APN是苹果公司提供的服务,允许开发者向运行iOS、watchOS、tvOS或macOS的应用程序发送远程通知。通过使用Laravel Mobile Notifications包,开发者可以轻松地配置和...

    uni-app 中使用uni push的demo

    【uni-app 中使用uni push的全面解析】 uni-app是一款由DCloud(海豚浏览器团队)开发的多端统一开发框架,它允许开发者使用Vue.js语法编写一次代码,就能跨平台运行到iOS、Android、H5、小程序等多个平台。而uni ...

    Service-Demo-android.zip_DEMO_android service_android service de

    9. **Notification的使用**:当Service需要在后台运行时通知用户,可以利用Notification API来创建通知栏提示。 通过分析这个DEMO,开发者不仅能学习到如何创建和管理Service,还能理解如何在实际应用中处理与...

    Android_api_demo

    ### Android API Demo 知识点概述 #### 一、概览 本文档旨在全面解析 Android API Demo 中的各种案例,通过具体实例深入理解 Android 开发中的关键技术和应用实践。该文档覆盖了从简单的用户界面设计到复杂的后台...

    Android API Demo 源码

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

    Android-react-native-push-notification.zip

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

    Android SDK (SDK Platforms)-android-26.zip

    Android SDK (SDK Platforms)-android-26.zip 是一个包含了Android开发所需的重要组件的压缩包,主要针对Android操作系统版本26,也称为Android Oreo(8.0)。这个压缩包是开发者构建、测试和调试Android应用的基础...

    vue-notification:使用Vue3的NotificationToast组件

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

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

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

    Android2.3ApiDemo

    **Android 2.3 API Demo 知识点详解** Android 2.3,也被称为 Gingerbread(姜饼),是Google在2010年推出的Android操作系统的一个重要版本。这个版本引入了许多新特性和改进,旨在优化用户体验和开发者工具。`...

Global site tag (gtag.js) - Google Analytics