通知:顯示在手機的通知欄上
通知的基本用法:
1 創建一個NotificationManager 對通知進行管理
NotificationManager manager =
(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
2 創建Notification
Notification notification = new NOtification
(R.drawable.icon,”This is tiket text”,System.currentTimeMills());
3 對通知的佈局進行設定
notification.
setLatestEventInfo(context,”This is Title”,”This is content”,null);
4 讓通知顯示出來 ,第一個參數爲一個任意id
manager.notify(1,notification);
案例:
public class MianActivity extends Activity implements OnClickListener{
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sendNotice = (Button)findViewById(R.id.send_button);
sendNotice.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.send_button:
//創建通知管理器
NotificationManager notificationManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
//創建通知對象
Notification notification = new Notification(R.drawable.ic_launcher, "This is ticker text",
System.currentTimeMillis());
Intent intent = new Intent(MianActivity.this,NotificationActivity.class);
//相當於通知中的Intent,點擊通知就會傳送至NotificationActivity.class
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
notification.setLatestEventInfo(this, "This is the Title", "This is the Text", pendingIntent);
//高級功能
//聲音
Uri soundUri = Uri.fromFile(new File("/system/media/audio/ringtones/nubia_mile.ogg"));
notification.sound=soundUri;
//震動
long[] vibrates = {0,1000,1000,1000};
notification.vibrate=vibrates;
//顏色
notification.ledARGB=Color.GREEN;
notification.ledOnMS=1000;
notification.ledOffMS=1000;
notification.flags=Notification.FLAG_SHOW_LIGHTS;
//默認聲音led 震動 顏色效果
// notification.defaults=Notification.DEFAULT_ALL;
notificationManager.notify(1,notification);
break;
default:
break;
}
}
}
public class NotificationActivity extends Activity {
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.notification_layout);
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancel(1);
//notificationManager.notify(1,notification);
}
}