到目前为止,想必大家已经都熟悉使用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)
- */
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.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);