文章出處
文章列表
消息通知使我們很常見的,當收到一條消息的時候,通知欄會顯示一條通知;
直接看代碼:
1 public class MainActivity extends Activity { 2 3 private NotificationManager nm; 4 @Override 5 protected void onCreate(Bundle savedInstanceState) { 6 super.onCreate(savedInstanceState); 7 setContentView(R.layout.activity_main); 8 nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 9 } 10 11 public void click(View v){ 12 Notification notification = new Notification.Builder(this) 13 .setContentTitle("我是大標題") 14 .setContentText("我是標題的內容") 15 .setSmallIcon(R.mipmap.ic_launcher) 16 .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher)) 17 .build(); 18 nm.notify(10,notification); 19 } 20 public void click1(View v){ 21 nm.cancel(10); 22 } 23 }
首先創建一個Notification的實例,然后通過NotificationManager的實例將Notification發送出來。
nm.notify(10,notification);
發出一條通知,id為10;
nm.cancel(10);
將id為10的通知取消。
文章列表
全站熱搜