編輯:關於 Android 程式設計

Android 7.0 在走過五個開發者預覽版本之後,於 8 月 22 日正式推送,版本名定為 Nougat(牛軋糖)。
官方總結的主要更新如下。
JIT 編譯器、VR 模式、Vulkan API
新的 JIT 編譯器讓系統和應用的啟動更快、佔用內存更少,系統更新時也不再出現漫長的「Android is upgrading」;
Vulkan API 是一款全新的 3D 渲染 API,能給遊戲帶來更絢麗的畫面和更高的渲染效率,不過目前僅適用於支持 Vulkan 的硬件,如 Nexus 5X、6P 等。
Doze 模式、電池管理優化、Data Saver 模式、後台數據訪問限制
分屏模式、Picture-in-Picture、最近任務快速切換(雙擊菜單鍵)
Android 7.0 開始支持分屏多任務,屏幕可以拆成兩個顯示區域,每個區域相當於一個獨立桌面,能同時跑不同應用;
PiP 模式讓應用以懸浮窗形式顯示,目前支持 Android TV 與平板;
雙擊菜單鍵可在最近任務間快速切換。
捆綁通知、直接回復、通知控制。Android 7.0 重新設計了通知系統的展示與管理方式。
自定義快捷設置、重新設計的快捷設置欄與設置頁、緊急信息、鎖屏壁紙
設置模塊經過交互上的重新梳理,操作更直觀;系統同時開放接口,允許應用通過實現特定服務把自己的設置項集成到系統設置與下拉菜單中。
Emoji:更新至 Unicode 9 表情集
隱私與安全:Direct Boot、靜默系統應用更新、基於文件的加密、文件訪問控制
設備安裝與遷移:系統備份會保存更多設置項
多語言:本地多語言支持、新增語言與語言排序設置
輔助功能:文字轉語音(TTS)速度可變
Notification 系統被重新設計,擁有更豐富的交互能力和更自由的樣式定制。

設置頁的交互也重做了一輪,高頻操作變得更短更快。

分屏模式、通知系統、性能與功耗,這三塊和日常開發關係最緊密。
默認情況下,Activity 會在當前窗口內創建並跳轉。如果想控制它在分屏中的行為:
// 不參與分屏 android:resizeableActivity="false" // 以新任務方式啟動 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // 在相鄰的另一個窗口中創建並顯示 Activity intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT | Intent.FLAG_ACTIVITY_NEW_TASK);

用過 YouTube 的人應該都記得那種體驗:視頻縮成一個懸浮小窗浮在上層,底層應用照樣能操作。PiP 模式與之類似,允許 Activity 以窗口形式疊在其他 Activity 之上。遺憾的是,目前它只支持 Android TV 和平板。
Notification 在原有的 Notification.Action 基礎上新增了 Direct Reply,用戶可以直接在通知欄回覆消息。
Tips:關鍵類是 RemoteInput,通過 addRemoteInput(mRemoteInput) 掛到 Action 上。

RemoteInput mRemoteInput = new RemoteInput.Builder(KEY_TEXT_REPLY)
.setLabel("Replay")
.build();
Intent intent = new Intent(this, TestActivity.class);
intent.putExtra("notification_replay", true);
PendingIntent mPendingIntent = PendingIntent.getActivity(
this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification.Action action = new Notification.Action.Builder(
R.drawable.notification_icon, "Replay", mPendingIntent)
.addRemoteInput(mRemoteInput)
.build();
Notification notification = new Notification.Builder(getApplicationContext())
.setContentTitle("ContentTitle")
.setContentText("ContentText")
.setSmallIcon(R.drawable.notification_icon)
.setActions(action)
.build();
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
notificationManagerCompat.notify(NOTIFICATION_ID, notification);

Notification.Action 支持按需添加多個操作按鈕:setActions(Action... actions)。完整示例:
import android.app.Activity;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.RemoteInput;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationManagerCompat;
import android.util.Log;
import android.view.View;
public class TestActivity extends Activity {
private static final String TAG = "TestActivity";
private static final String KEY_TEXT_REPLY = "key_test_reply";
private static int NOTIFICATION_ID = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
findViewById(R.id.notification).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
send();
}
});
// 從通知欄直接回覆後,會帶著 Intent 回到這裡
if (getIntent().getExtras() != null) {
if (getIntent().getExtras().getBoolean("notification_replay")) {
Bundle remoteInput = RemoteInput.getResultsFromIntent(getIntent());
if (remoteInput != null) {
Log.d(TAG, "onCreate: " + remoteInput.getCharSequence(KEY_TEXT_REPLY));
}
sendReceiverNotification();
}
}
}
private void sendReceiverNotification() {
Notification secondNotification = new Notification.Builder(getApplicationContext())
.setSmallIcon(R.drawable.notification_icon)
.setContentText("Message sent.")
.build();
NotificationManagerCompat notificationManagerCompat =
NotificationManagerCompat.from(this);
notificationManagerCompat.notify(NOTIFICATION_ID, secondNotification);
}
public void send() {
RemoteInput mRemoteInput = new RemoteInput.Builder(KEY_TEXT_REPLY)
.setLabel("Replay")
.build();
Intent intent = new Intent(this, TestActivity.class);
intent.putExtra("notification_replay", true);
PendingIntent mPendingIntent = PendingIntent.getActivity(
this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification.Action action = new Notification.Action.Builder(
R.drawable.notification_icon, "Replay", mPendingIntent)
.addRemoteInput(mRemoteInput)
.build();
Notification.Action actionTest = new Notification.Action.Builder(
R.drawable.notification_icon, "Test", mPendingIntent)
.build();
Notification notification = new Notification.Builder(getApplicationContext())
.setContentTitle("ContentTitle")
.setContentText("ContentText")
.setSmallIcon(R.drawable.notification_icon)
.setActions(action, actionTest)
.build();
NotificationManagerCompat notificationManagerCompat =
NotificationManagerCompat.from(this);
notificationManagerCompat.notify(NOTIFICATION_ID, notification);
}
}
更細的通知配置項可參考官方文檔:
developer.android.com/guide/topics/ui/notifiers/notifications.html
Android 7.0 允許應用把自己的設置項添加到下拉快捷設置中,具體實現參考 android.service.quicksettings.Tile。
彩蛋:開啟開發者選項後,你會在快捷設置編輯界面裡看到「顯示圖層」和「GPU 呈現模式分析」兩個新磁貼,非常實用。

Android 7.0 進一步強化了 Doze 對 CPU 喚醒的控制和網絡訪問限制,後台應用的可乘之機更少。
Android 7.0 收緊了私有文件的訪問權限。如果仍以 file:// URI 的形式把私有文件暴露給其他應用,會拋出 FileUriExposedException(SecurityException 的子類),需要改用 FileProvider。
Google Fuchsia 項目:
fuchsia.googlesource.com
fuchsia-review.googlesource.com
參考文檔:
android.com/versions/nougat-7-0
developer.android.com/about/versions/nougat
Android 官方開發文件中文版:動畫與轉場框架完整導覽
本文整理自 Android 官方開發文件 Training 系列中的 Transitions 單元,說明如何運用轉場框架,讓檢視的出現與消失變得流暢而連貫。 為什麼需要轉
Android Studio 必裝插件:Parcelable 代碼自動生成
在 Android 開發裡,只要想把對象通過 Intent 或 Bundle 傳遞,就得老老實實實現 Parcelable 接口:寫 writeToParcel、聲明 C
Objective-C 中的 Runtime:從數據結構到實戰應用
前言 Runtime 是一套比較底層的純 C 語言 API,我們平時寫的 OC 程式碼在運行時,最終都會落到 Runtime 的 C 實現上。Runtime 是開源的,有
SwipeListView 實現微信、QQ 風格的列表滑動刪除
微信和 QQ 的最近联系人列表都支持左滑唤出刪除按鈕,操作直覺、反饋順滑,是很值得借鑒的交互。自己實現並不難,開源庫 SwipeListView 已經把大部分工作封裝好了