- 浏览: 41261 次
- 性别:
- 来自: 济南
最新评论
-
kensunhu:
正是我想要的。典型的app ui布局。谢谢!
android UI - 仿威信tab样式 -
007007jing:
bing_zz 写道兄弟加油!谢谢
android2.3 api demo 学习系列(7)--App/Activity/Hello World -
bing_zz:
兄弟加油!
android2.3 api demo 学习系列(7)--App/Activity/Hello World
android2.3 api demo 学习系列(21)--App/Notification/Incoming Message
- 博客分类:
- Android ApiDemo
现在我们开始学习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);
发表评论
-
android2.3 api demo 学习系列(23)--App/Notification/StatusBarNotification
2012-07-07 19:51 1300apidemo-StatusBarNotification里面 ... -
android2.3 api demo 学习系列(22)--App/Notification/Notifying Service Controller
2012-07-06 14:56 1674因为还没有看到service的demo,这里先不对servic ... -
android2.3 api demo 学习系列(20)--App/Menu
2012-07-06 09:58 1114现在来学习下menu的相关 ... -
android2.3 api demo 学习系列(19)--App/Intent and Launcher Shortcuts
2012-07-06 09:36 1062第一个demo:Intent,根据指定的类型,枚举出所有符合条 ... -
android2.3 api demo 学习系列(18)--App/Dialog
2012-07-06 09:13 957今天主要学习Dialog: 1、一般的dialog ... -
android2.3 api demo 学习系列(17)--App/Alarm/AlarmController and Alarm Service
2012-07-03 17:12 2146本次学习将apidemo中得两个demo:AlarmContr ... -
android2.3 api demo 学习系列(16)--App/Activity/Translucent and Blur activity
2012-07-03 11:47 1855本次同样是将apidemo中得两个demo合并起来学习:Tra ... -
android2.3 api demo 学习系列(15)--App/Activity/SetWallpaper
2012-07-03 11:00 1092本次示例我们整合了apidemo里面的两个demo:SetWa ... -
android2.3 api demo 学习系列(14)--App/Activity/Screen Orientation
2012-07-03 09:50 3080下面我们来学习下Screen Orientaiton的demo ... -
android2.3 api demo 学习系列(13)--App/Activity/Save & Restore
2012-07-02 17:29 1437前面文章android2.3 api demo 学习系 ... -
android2.3 api demo 学习系列(12)--App/Activity/Reorder Activitys
2012-07-02 16:45 959Reorder Activitys Demo主要是实现打开ac ... -
android2.3 api demo 学习系列(11)--App/Activity/Redirection
2012-07-02 15:52 830APIDEMO里面的redirection示例本身并没有新技术 ... -
android2.3 api demo 学习系列(10)--App/Activity/RecevieResult
2012-07-02 14:48 959在先前的文章 activity之间跳转传值 已经学习过这方面的 ... -
android2.3 api demo 学习系列(9)--App/Activity/QuickContactsDemo
2012-07-01 19:46 959现在我们来学习如何使用Content Provider来访问a ... -
android2.3 api demo 学习系列(8)--App/Activity/Preference State
2012-07-01 19:45 870android保存数据有很多种方式,其中最简单的就是使用Sha ... -
android2.3 api demo 学习系列(7)--App/Activity/Hello World
2012-06-29 14:03 1049学习android当然不能少了HelloWorld,接下来我们 ... -
android2.3 api demo 学习系列(6)--App/Activity/ForwardActivity
2012-06-29 13:50 799本次学习activity的跳转 1、构建intent ... -
android2.3 api demo 学习系列(5)--App/Activity/Dialog
2012-06-29 11:42 970前面我们已经学习了Custom Dialog 和 Custom ... -
android2.3 api demo 学习系列(4)--App/Activity/Custom Title
2012-06-29 11:26 1054android的标题栏默认是由android:lable定义的 ... -
android基础知识---Providing Resources
2012-06-29 10:42 775android的可使用的资源文件,google建议我们在开发应 ...
相关推荐
react-native-in-app-notification-master.rar
NULL 博文链接:https://kakukemeit.iteye.com/blog/1806388
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-in-app-notification : 安装 yarn add react-native-in-app-notification 要么 npm install react-native-in-app-...
Laravel开发-laravel-mobile-notification 向移动设备(APN、GCM)发送推送通知的Laravel/Lumen包
Android-react-native-push-notification.zip,响应本机本地和远程通知,安卓系统是谷歌在2008年设计和制造的。操作系统主要写在爪哇,C和C 的核心组件。它是在linux内核之上构建的,具有安全性优势。
"在窗口顶端或者下端弹出自定义的提示视图(notification View)。例如,图中上方的黑色标签“This is the notification.""。可用于显示当前app的状态,用户操作的提示等等。" 注意:请在Mac下解压使用
在窗口顶端弹出自定义的提示视图(notification View)。弹出的notification view可以自动隐藏。如果有多个notification view,则进行排队,按顺序显示。可用于显示当前app的状态,用户操作的提示、提醒通知等等。...
自定义UITabBar。自定义Tab Bar的文字、颜色和图片,不同tab之间切换的动作效果,以及自定义Tab Bar弹出消息的样式。 注意:请在Mac下解压使用
Laravel开发-laravel-push-notification Push Notification 服务端支持 app的推送通知发送工具,支持结果的apns和安装的gcm
该存储库包含荷兰语COVID-19 Notification App CoronaMelder的原生Android实现。 android应用位于您当前正在查看的存储库中。 可以在以下位置找到iOS应用: : 后端可以在这里找到: : 可以在此处找到用作开发应用...
Android上的另一种推送通知解决方案是 ,其功能是在iOS和Android上提供相同的API,而该功能在Android上支持更多的通知功能。React本机系统通知 发送或安排React Native的Android系统通知。目录import React , { ...
极光推送 JAVA服务端 jpush-client-3.2.8 与 集成源码 jpush-api-java-client-master
yarn add vue-toast-notification # npm npm install vue-toast-notification 用法 import Vue from 'vue' ; import VueToast from 'vue-toast-notification' ; // Import one of the available themes //import '...
Android 通知(notification)简单实用Demo,包含点击功能简单例子,供大家下载学习
learn push notification with android
android notification完全解析Demo
安装使用yarn或npm将软件包安装为项目依赖项: $ yarn add @dafcoe/vue-notification--- or ---$ npm install --save @dafcoe/vue-notification用法全局(在main.js / main.ts文件上)或本地(在组件上)导入...
类型:NotificationDemo 功能:从网上下载apk存储到手机指定目录,并在通知栏里显示其下载进度,如果下载错误,或者不想要该apk可以删除 涉及知识: 文件流、网络下载链接协议、读写权限、Handler、Notification、...
Android notification进度条 demo,欢迎下载!!!!!!!!!!!!!!!!!!!!!!!!!!!!