原创作品,允许转载,转载时请务必以超链接形式标明文章原始出处、作者信息和本声明。否则将追究法律责任。http://jackxlee.blog.51cto.com/2493058/682435
Notification和NotificationManager的操作相对比较简单,一般用来获取系统级的服务NotificationManager,然后实例化Notification的对象,设置它的一系列属性(比如说图标、时间、标题、内容等),最后通过NotificationManager发出通知即可。
The description from SDK about Notification:
- AclassthatrepresentshowapersistentnotificationistobepresentedtotheuserusingtheNotificationManager.
- TheNotification.BuilderhasbeenaddedtomakeiteasiertoconstructNotifications.
- Foraguidetocreatingnotifications,seetheCreatingStatusBarNotificationsdocumentintheDevGuide.
接下来使用一个简单的案例来演示一下Notification的使用:
首先MainActivity的代码如下:
- publicclassMainActivityextendsActivity{
- privateButtonbtnSend;
-
- //定义BroadcastReceiver的action
-
privatestaticfinalStringNotificationDemo_Action="com.ceo.notification.activity.NotificationDemo_Action";
-
- /**Calledwhentheactivityisfirstcreated.*/
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- //getthewidgets'instance
- getInstance();
-
- btnSend.setOnClickListener(newView.OnClickListener(){
- @Override
- publicvoidonClick(Viewv){
-
Intentintent=newIntent();
- intent.setAction(NotificationDemo_Action);
- sendBroadcast(intent);
- }
- });
- }
-
- publicvoidgetInstance(){
-
btnSend=(Button)findViewById(R.id.btnSend);
-
- }
- }
相对应的main.xml布局:
- <?xmlversion="1.0"encoding="utf-8"?>
-
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
-
android:orientation="vertical"
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent"
-
>
-
<TextView
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:gravity="center"
-
android:textColor="#EEE"
-
android:textStyle="bold"
-
android:textSize="25sp"
-
android:text="Notification应用的小案例"
-
/>
-
<Button
-
android:id="@+id/btnSend"
-
android:text="sendnotification"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
/>
-
</LinearLayout>
SecondActivity的代码如下:
- publicclassSecondActivityextendsActivity{
- privateButtonbtnCancel;
- //声明Notification
- privateNotificationnotification;
- //声明NotificationManager
- privateNotificationManagermNotification;
- //标识Notification的ID
-
privatestaticfinalintID=1;
-
- @Override
- protectedvoidonCreate(BundlesavedInstanceState){
- //TODOAuto-generatedmethodstub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.second);
-
- getInstance();
- //怎样获得NotificationManager的实例?
-
Stringservice=NOTIFICATION_SERVICE;
-
mNotification=(NotificationManager)getSystemService(service);
-
- //获得Notification的实例
-
notification=newNotification();
-
- //设置该图标会在状态栏显示
-
inticon=notification.icon=android.R.drawable.stat_notify_chat;
- //设置提示信息
-
StringtickerText="TestNotification";
- //设置显示时间
-
longwhen=System.currentTimeMillis();
-
notification.icon=icon;
-
notification.tickerText=tickerText;
-
notification.when=when;
-
-
Intentintent=newIntent(this,MainActivity.class);
-
PendingIntentpi=PendingIntent.getActivity(this,0,intent,0);
- notification.setLatestEventInfo(this,"消息","HelloAndroid",pi);
- mNotification.notify(ID,notification);
-
- btnCancel.setOnClickListener(newView.OnClickListener(){
- @Override
- publicvoidonClick(Viewv){
-
mNotification.cancel(ID);//--->取消通知
- }
- });
- }
-
- publicvoidgetInstance(){
-
btnCancel=(Button)findViewById(R.id.btnCancel);
- }
-
- }
相对应的second.xml布局:
- <?xmlversion="1.0"encoding="utf-8"?>
-
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
-
android:orientation="vertical"
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent"
-
>
-
<TextView
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:gravity="center"
-
android:textColor="#EEE"
-
android:textStyle="bold"
-
android:textSize="25sp"
-
android:text="显示通知界面"
-
/>
-
<Button
-
android:id="@+id/btnCancel"
-
android:text="cancelnotification"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
/>
-
</LinearLayout>
MyReceiver的代码如下:
- publicclassMyReceiverextendsBroadcastReceiver{
- @Override
- publicvoidonReceive(Contextcontext,Intentintent){
- //实例化Intent
-
Intenti=newIntent();
- //在新任务中启动Activity
- i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- //设置Intent启动的组件名称
- i.setClass(context,SecondActivity.class);
- //启动Activity,显示通知
- context.startActivity(i);
- }
- }
当然不要忘了在AndroidManifest文件中注册广播和Activity等:
- <?xmlversion="1.0"encoding="utf-8"?>
-
<manifestxmlns:android="http://schemas.android.com/apk/res/android"
-
package="com.ceo.notification.activity"
-
android:versionCode="1"
-
android:versionName="1.0">
-
<applicationandroid:icon="@drawable/icon"android:label="@string/app_name">
-
<activityandroid:name=".MainActivity"
-
android:label="@string/app_name">
-
<intent-filter>
-
<actionandroid:name="android.intent.action.MAIN"/>
-
<categoryandroid:name="android.intent.category.LAUNCHER"/>
-
</intent-filter>
-
</activity>
-
<receiverandroid:name="MyReceiver">
-
<intent-filter>
-
<actionandroid:name="com.ceo.notification.activity.NotificationDemo_Action"/>
-
</intent-filter>
-
</receiver>
-
<activityandroid:name=".SecondActivity"></activity>
-
</application>
-
<uses-sdkandroid:minSdkVersion="8"/>
-
</manifest>
最后直接上图:
至此Android中Notification的使用介绍完毕,预祝大家成功。
分享到:
相关推荐
了解Android Notification的基本用法
主要介绍了Android Notification 使用方法详解的相关资料,这里提供实例来帮助大家理解掌握这部分内容,需要的朋友可以参考下
简单介绍Android Notification用法 详细介绍NotificationManageService和StatusBarManageService与App层交互过程
Android Notification使用详解 Notification 核心代码(链式调用):适用于Android 4.0以上(不兼容低版本) Notification noti = new Notification.Builder(this) .setContentTitle(标题名称) .setContentText...
这个项目整合了Android平台所有Notification的使用方法,有些使用场景不生效,不知道是否是我使用方法有问题。
主要介绍了Android Notification使用方法总结的相关资料,这里提供了四种使用方法,需要的朋友可以参考下
Android Notification消息框 Toast弹出框用法演示范例,本例中关于 Toast弹出框的演示,演示了适时的 Toast和长时间的 Toast,关于Notification的定义,则演示了高级Notification的用法,自定义4种Notification的...
一个关于安卓toast和notification使用方法的Android源码,来自eoeandroid社区,对初学者或许是个帮助吧。
Android Notification通知 简单使用 demo下载。 解决问题:兼容高低版本,帮助用户打开通知开关,android5.x手机状态栏icon是白块问题。 详细文章请参考 ...
主要介绍了Android中Notification用法,以实例形式较为详细的分析并总结了Notification的功能与使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下
Notification最新用法、实现Notification的通知栏常驻、Notification的各种样式big View、解决Notification点击无效
主要介绍了Android中Notification的用法汇总的相关资料,需要的朋友可以参考下
主要针对Android Notification通知进行解析,本文主要介绍的是notification通知的使用方法,感兴趣的小伙伴们可以参考一下
利用Android Notification实现普通通知和自定义通知的功能,非常实用
主要介绍了Android Notification的多种用法总结的相关资料,需要的朋友可以参考下
ndroid Notification 用法源码.~~~~~~~~~~~~
本文实例讲述了Android中Notification用法。分享给大家供大家参考,具体如下: Notification可以理解为通知的意思一般用来显示广播信息 用Notification就必须要用到NotificationManager 想用Notification一般有三个...
本文实例讲述了Android开发之Notification通知用法。分享给大家供大家参考,具体如下: 根据activity的生命周期,在activity不显示时,会执行onStop函数(比如按下home键),所以你在onStop函数(按退出键除外)里面把...