android整合--notification推送通知
来源:互联网 发布:刷信誉源码平台 编辑:程序博客网 时间:2024/04/27 13:26
到目前为止,想必大家已经都熟悉使用Toast去给用户显示信息了。尽管使用Toast很方便,但是Toast显示的通知并不是永久存储的。它只在屏幕上显示一小段时间,然后就消失了。如果它包含一些特别重要的信息,如果用户没有观察屏幕,那么用户就很容易错过它。
对于那些重要的信息,应该采用一种更加持久保存的方法。在这种情况下,应该使用NotificationMnanger(消息管理器)去显示一个长久的信息,这个消息被显示在了StatusBar(状态栏)上面,使用用户能够很容易地看见。
接下来展示如何发送一个Notification通知。
首先建个工程Notification
先不解释 上代码
public class NotificationsActivity extends Activity { /** Called when the activity is first created. */int notificationId = 1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void onclick(View view){ displayNotification(); } public void displayNotification(){ //设置点击通知跳转页面 Intent intent = new Intent(this,NotificationView.class); intent.putExtra("notificationid", notificationId); //得到pendingintent 延迟执行intent PendingIntent pendintent = PendingIntent.getActivity(this, 0, intent, 0); //获取通知管理器并设置图标与显示时间 NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); Notification notfi = new Notification(R.drawable.icon,"today there is a meeting",System.currentTimeMillis()); //设置通知标题与内容 CharSequence form = "System notification"; CharSequence message = "Meeting is at 3pm"; notfi.setLatestEventInfo(this, form, message, pendintent); //---100ms delay, vibrate for 250ms, pause for 100 ms and // then vibrate for 500ms--- //震动提示 notfi.vibrate = new long[]{100,250,100,500}; nm.notify(notificationId, notfi); }}
点击通知转到这个activity
public class NotificationView extends Activity {/* (non-Javadoc) * @see android.app.Activity#onCreate(android.os.Bundle) */@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.notification);NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);nm.cancel(getIntent().getExtras().getInt("notificationId"));}}
main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns: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:text="@string/hello" /> <Button android:id="@+id/btnnoti" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="onclick" android:text="点击通知" /></LinearLayout>
notification.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Here are the details for the notification..." /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.test.Notifications" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="7" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".NotificationsActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity><activity android:name=".NotificationView" android:label="notification detail"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.VIBRATE"/> </manifest>
大致思路为
1 设置通知跳转后的intent和activity,通知id
Intent intent = new Intent(this,NotificationView.class);
intent.putExtra("notificationid", notificationId);
2 得到pendingintent(后篇讲一下pengdingintent 和intent的区别)
PendingIntent pendintent = PendingIntent.getActivity(this, 0 , intent, 0);
3 获取通知管理器并设置图标与显示时间
NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notfi = new Notification(R.drawable.icon,"today there is a meeting",System.currentTimeMillis());
4 //设置通知标题与内容
CharSequence form = "System notification";
CharSequence message = "Meeting is at 3pm";
notfi.setLatestEventInfo(this, form, message, pendintent);
5 //震动提示(可加可不加)
notfi.vibrate = new long[]{100,250,100,500};
nm.notify(notificationId, notfi);
- android整合--notification推送通知
- android整合--notification推送通知
- Android-Notification推送栏通知
- Android实现推送PushService通知Notification
- Android学习笔记(32):通知推送Notification
- Android 使用Notification创建推送通知
- Android实现推送PushService通知Notification
- Android 通知栏Notification的整合
- Android 通知栏Notification的整合
- Android 通知栏Notification的整合
- Android 通知栏Notification的整合
- 推送通知 Push Notification
- Android 上的消息推送通知(Push Notification)
- android入门(八) 状态栏通知(消息推送) Notification
- Android开发笔记(五十二)通知推送Notification
- Android 通知栏Notification的整合 全面学习
- Android 通知栏Notification的整合 全面学习
- Android 通知栏Notification的整合 全面学习
- easymock教程-easymock的典型使用
- HDU 4059 The Boss on Mars(容斥原理)
- C++指针的管理
- 编程珠玑之第一章习题7:处理程序存在的缺陷的问答与测试
- poj 2417 Discrete Logging 求解模方程a^x=b(mod n),n为素数+模板题(baby_step giant_step)
- android整合--notification推送通知
- SSH学习笔记二
- Java--chapter03--输出希腊字母
- 函数调用机制与调用约定(转载)
- opengl片段测试之后的逻辑操作
- Android源码目录结构详解
- 结构体和类的区别
- java三种写文件对象详解
- C语言中volatile关键字的作用