看了一下Notification的相关内容,总结一下,先看一下android developer guide:
To create a status bar notification:
- Get a reference to the
NotificationManager
:String ns = Context.NOTIFICATION_SERVICE; NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
- Instantiate the
Notification
:int icon = R.drawable.notification_icon; CharSequence tickerText = "Hello"; long when = System.currentTimeMillis(); Notification notification = new Notification(icon, tickerText, when);
- Define the notification's message and
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);
- Pass the
Notification
to theNotificationManager
:private static final int HELLO_ID = 1; mNotificationManager.notify(HELLO_ID, notification);
That's it. Your user has now been notified.
以下是例子:
布局文件非常简单只定义两个Button即可
package com.csdn.sendnotification; import android.app.Activity; import android.app.Notification; import android.app.PendingIntent; import android.view.View; import android.view.View.OnClickListener; import android.app.NotificationManager; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.widget.Button; public class SendNotificationActivity extends Activity { /** Called when the activity is first created. */ int notificationNumber=0; private Button sendButton; private Button cancelButton; private NotificationManager notiManager; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); sendButton=(Button)findViewById(R.id.sendButton); cancelButton=(Button)findViewById(R.id.cancelButton); notiManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE); sendButton.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub notificationNumber+=1; Notification notification = new Notification(); notification.icon = R.drawable.icon; notification.tickerText = "texttexttexttexttext"; //如果很长会滚动显示 notification.when = System.currentTimeMillis(); notification.flags=Notification.FLAG_AUTO_CANCEL; //自动清除Notification,拉下状态栏点击Notification就会清除通知,去掉这句话点击Notification不会消失
notification.defaults=Notification.DEFAULT_ALL; //震动Notification.DEFAULT_VIBRATE,两者都需在AndroidManifest.xml加入权限:android.permission.VIBRATE notification.sound = Uri.parse("file:///sdcard/ringstones/Fire.mp3"); //自定义提示音 notification.number=notificationNumber; //如果多次弹出的Notification的ID相同,那么Notification就只会更新而不会再次滚动提醒,会在图标上显示数目 Intent intent = new Intent(SendNotificationActivity.this, SendNotificationActivity.class); PendingIntent pi = PendingIntent.getActivity(SendNotificationActivity.this, 0, intent, 0); notification.setLatestEventInfo(SendNotificationActivity.this, "title:标题","content:内容", pi); notiManager.notify(1234, notification); //1234为notification的ID } }); cancelButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { notiManager.cancel(1234); //取消通知,另一种及拉下状态栏点击Notification } }); } }