package com.example.andday11notifiation; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; //通知的簡單記錄,四個按鈕事件代碼 public class MainActivity extends Activity { private NotificationManager notifyManager;// 通知管理器 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //得到通知管理器 notifyManager = (NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE); } // 簡單文本的通知 public void notifySimpleText(View view) { NotificationCompat.Builder builder = new NotificationCompat.Builder( this); // 設置提示圖標,標題,內容,循環滾動的內容 builder.setSmallIcon(R.drawable.ic_launcher) .setContentTitle("提示信息") .setContentText("注意:今晚氣溫變冷,請多加衣物!!") .setTicker("注意:今晚氣溫變冷,請多加衣物!!注意:今晚氣溫變冷,請多加衣物!!") // 將信息滾動顯示 .setDefaults( Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE) // 默認提醒方式聲音和震動,要加權限 .setContentIntent( PendingIntent.getActivity(this, 100, new Intent( Intent.ACTION_CALL, Uri.parse("tel:10086")), PendingIntent.FLAG_ONE_SHOT));// 點擊通知能夠打電話,記得加權限 /* * setContentIntent(PendingIntent.getActivity(context, requestCode, * intent, flags)) context:上下文 requestCode:請求碼 * intent:一個延時意圖,即當點擊的通知時候才跳轉,flags:當通知來時只能點擊一次這個通知 */ notifyManager.notify(1, builder.build());// 通知管理進行通知管理動做,啓動發送通知 } // 進度的通知 public void notifyProgress(View view) { new Thread(new Runnable() { @Override public void run() { NotificationCompat.Builder builder = new NotificationCompat.Builder( MainActivity.this); // 設置提示圖標、標題,內容,循環滾動的內容 builder.setSmallIcon(R.drawable.ic_launcher) .setContentTitle("有新通知").setContentText("正在下載!") .setTicker("正在下載!正在下載!正在下載!"); for (int i = 0; i <= 100; i++) { // builder.setProgress(max, progress, indeterminate) builder.setProgress(100, i, false);// indeterminate進度條樣式:一把用false notifyManager.notify(2, builder.build()); try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // 下載完後通知跟新:提示下載完 notifyManager.cancel(2);// 下載完取消 /* * NotificationCompat.Builder builder2 = new * NotificationCompat.Builder( MainActivity.this); * //能夠重複利用上面的builder,不必再新建立一個 */ builder = new NotificationCompat.Builder(MainActivity.this); builder.setSmallIcon(R.drawable.ic_launcher) .setContentTitle("提示消息").setContentText("下載完畢"); notifyManager.notify(3, builder.build()); } }).start(); } // 列表通知,即顯示多列表個選項 public void notifyList(View view) { NotificationCompat.Builder builder = new NotificationCompat.Builder( this); builder.setSmallIcon(R.drawable.ic_launcher).setContentTitle("提示消息"); NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle(); // 上面new一個通知樣式 style.addLine("列表1:你好"); style.addLine("列表2:嘿嘿嘿"); style.addLine("列表3:區34213123"); builder.setStyle(style);// 加進樣式 notifyManager.notify(4, builder.build()); } // 大圖標通知 public void notifyBigImage(View view) { NotificationCompat.Builder builder = new NotificationCompat.Builder( this); builder.setSmallIcon(R.drawable.ic_launcher).setContentTitle("提示消息"); NotificationCompat.BigPictureStyle style = new NotificationCompat.BigPictureStyle(); // 設置一個通知樣式 style.bigLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.notify_newmessage)); style.bigPicture(BitmapFactory.decodeResource(getResources(), R.drawable.mm));// 通知的大圖片 builder.setStyle(style); notifyManager.notify(5, builder.build()); } }