編輯:關於 Android 程式設計
Android N 引入了一批全新 API,讓應用得以發佈擁有更高可見性與互動性的通知。它擴展了既有的 RemoteInput 通知 API,在手持裝置上支援內嵌式回覆——使用者無須進入應用,就能直接從通知欄快速回覆訊息。
此外,Android N 還能把相似的通知捆綁成一則來呈現,這背後使用的是既有的 NotificationCompat.Builder.setGroup() 方法。使用者可以展開通知群組,並對每則通知分別進行回覆、清除等操作。最後,Android N 新增的 API 也允許你在自定義通知檢視中使用系統裝飾元素,確保外觀與標準範本保持一致。
本文將重點說明在應用裡導入這些新通知功能時,值得留意的幾項關鍵變更。
有了直接回復功能,使用者能直接在通知介面內快速回覆短信或更新任務清單。在手持裝置上,這可透過通知中另外附加的按鈕完成內嵌回覆;當使用者以鍵盤輸入完成後,系統會把文字附加到你為通知操作指定的 Intent,再發送到手持裝置應用。
要建立支援直接回復的通知操作,先建立一個 RemoteInput.Builder 實例,其構造函數接受一個作為文字輸入鍵的字符串,之後應用再用這把鍵取出輸入內容:
//遠程的輸入控件構造一個 RemoteInput remoteInput = new RemoteInput.Builder(KEY_TEXT_REPLY).setLabel("回復點什麼呢?").build();
接著用 addRemoteInput() 把 RemoteInput 附加到操作上:
// Create the reply action and add the remote input. Intent intent = new Intent(); intent.putExtra("id", id); intent.setClass(MainActivity.this, MainActivity.class); PendingIntent replyPendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, PendingIntent.FLAG_ONE_SHOT); Notification.Action action = new Notification.Action.Builder(R.mipmap.ic_launcher, "點擊這裡回復東西", replyPendingIntent) .addRemoteInput(remoteInput).build();
最後套用操作並發出通知:
// Build the notification and add the action. final Notification newMessageNotification = new Notification.Builder(MainActivity.this).setSmallIcon(R.mipmap.ic_launcher).setContentTitle("通知欄標題") .setContentText("通知欄內容").setPriority(Notification.PRIORITY_HIGH).setDefaults(Notification.DEFAULT_VIBRATE).addAction(action).build(); // Issue the notification. NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); notificationManager.notify(id++, newMessageNotification);
如此一來,使用者就能在通知欄看到通知並直接回覆了。

從 PendingIntent 拉起的元件,可以透過 Intent 取得資料,再用相同的 id 發送通知來更新回覆狀態:
Bundle remoteInput = RemoteInput.getResultsFromIntent(intent); String res = null; if (remoteInput != null) { res = (String) remoteInput.getCharSequence(KEY_TEXT_REPLY); } Notification repliedNotification = new Notification.Builder(this).setSmallIcon(R.mipmap.ic_launcher).setContentText(res + "回復成功").build(); // Issue the new notification.使用這個相同的ID沖掉以前的notifacation NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); int id = intent.getIntExtra("id", 1); Log.d("EDSHENG", "onNewIntent: " + id); notificationManager.notify(id, repliedNotification);

分組通知有兩個重點:一是建立一個作為群組的通知,二是把子通知歸入該群組。
注意:若沒有分組,在 N 上一般通知超過 4 則時,系統會自動把它們分到同一組。
//遠程的輸入控件構造一個 RemoteInput remoteInput = new RemoteInput.Builder(KEY_TEXT_REPLY).setLabel("group回復點什麼呢?").build(); // Create the reply action and add the remote input. Intent intent = new Intent(); intent.putExtra("id", id); intent.setClass(MainActivity.this, MainActivity.class); PendingIntent replyPendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, PendingIntent.FLAG_ONE_SHOT); Notification.Action action = new Notification.Action.Builder(R.mipmap.ic_launcher, "group點擊這裡回復東西", replyPendingIntent) .addRemoteInput(remoteInput).build(); // Build the notification and add the action. final Notification group = new Notification.Builder(MainActivity.this).setSmallIcon(R.mipmap.ic_launcher).setContentTitle("group通知欄標題") .setGroup("分組信息").setAutoCancel(true).setStyle(new Notification.InboxStyle()).setGroupSummary(true) .setContentText("group通知欄內容").addAction(action).build(); Notification newMessageNotification = new Notification.Builder(MainActivity.this).setSmallIcon(R.mipmap.ic_launcher).setContentTitle("group子通知欄標題") .setGroup("分組信息").setAutoCancel(true).setStyle(new Notification.InboxStyle()) .setContentText("group子通知欄內容").addAction(action).build(); // Issue the notification. NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); notificationManager.notify(id++, newMessageNotification); notificationManager.notify(1000, group);
先建立一個呼叫 setGroupSummary 的群組通知,再讓其他通知以相同的 setGroup 歸入該組;後續若有更多通知,只要沿用同一組名即可持續加入。

Android N 引進了自定義通知樣式的新 API。透過 MessageStyle 類,你能變更通知中顯示的多個標籤,包括會話標題、其他訊息與內容檢視,特別適合聊天對話場景。
Notification notification = new Notification.Builder(MainActivity.this).setSmallIcon(R.mipmap.ic_launcher) .setStyle(new Notification.MessagingStyle("Me") .setConversationTitle("Team lunch") .addMessage("Hi", System.currentTimeMillis(), null) // Pass in null for user. .addMessage("What's up?", System.currentTimeMillis(), "Coworker") .addMessage("Not much", System.currentTimeMillis(), null) .addMessage("How about lunch?", System.currentTimeMillis(), "Coworker")).build(); // Issue the notification. NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); notificationManager.notify(id++, notification);

Android N 還加入了名為 TileService 的類,專門針對快捷通知欄進行擴展,讓開發者能自定義下拉通知欄中的快捷設定圖示,把應用內的一些捷徑入口擺在這裡。

實作時,我們繼承 TileService 並重寫 onClick 方法,在裡頭啟動我們的 Activity,再把手機的通知欄收起來。別忘了在 AndroidManifest 中完成 service 宣告,並加入收起、展開通知欄所需的權限:
具體實作如下:
public class TitleService extends TileService { public TitleService() { } @Override public void onTileAdded() { super.onTileAdded(); } @Override public void onTileRemoved() { super.onTileRemoved(); } @Override public void onClick() { super.onClick(); Intent intent = new Intent(); intent.setClass(getApplicationContext(),MainActivity.class); //intent.setComponent(new ComponentName("com.tencent.mtt", "com.tencent.mtt.external.wifi.WifiLaunchActivity")); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); getBaseContext().startActivity(intent); collapseStatusBar(getBaseContext()); } public static void collapseStatusBar(Context context) { try { Object statusBarManager = context.getSystemService("statusbar"); Method collapse; if (Build.VERSION.SDK_INT <= 16) { collapse = statusBarManager.getClass().getMethod("collapse"); } else { collapse = statusBarManager.getClass().getMethod("collapsePanels"); } collapse.invoke(statusBarManager); } catch (Exception localException) { localException.printStackTrace(); } } }
用版面配置做出頂部與底部工具列
很多 App 的介面結構是「上方一排圖示工具列、中間是內容區、下方一排操作按鈕」。這個效果完全不需要自訂 View,純靠 RelativeLayout 的定位屬性就能完成
Android PagerTabStrip 子控制項完整用法與樣式客製
先看成品: 效果相當順手,比自己動手寫一個標籤指示器要流暢得多。接下來就來認識 PagerTabStrip 以及它的使用方式。 PagerTabStrip 是 v4 支
ButterKnife 源碼解析:編譯期生成程式碼如何取代 findViewById
身為 Android 開發者,大概沒人沒聽過 ButterKnife。它把 findViewById 與 setOnClickListener 這類樣板收進注解裡,開發效
Android 百度地圖集成實戰:路線規劃與導航
關於百度地圖的導航功能曾困擾我很長一段時間,現在終於整理出來了,來給大家分享一下。其實並不難,把百度的 API 範例 Demo 下載下來,找到需要的部分即可。使用前當然要