建立筆記應用程式

記事應用程式是 Android 的核心功能,可以提高使用者在各種平台上的工作效率 大螢幕裝置。記事應用程式可讓使用者在 擷取畫面內容並加上註解 並儲存筆記,以便日後檢查及修改

記事應用程式可以從螢幕鎖定畫面存取,也可以在執行其他操作時存取 應用程式。

支援使用觸控筆做記事,帶來卓越的使用者體驗。

「記事」角色

RoleManager.ROLE_NOTES敬上 可識別記事應用程式,並授予 LAUNCH_CAPTURE_CONTENT_ACTIVITY_FOR_NOTE 權限。

如要取得應用程式的記事角色,請按照下列步驟操作:

  1. 呼叫 isRoleAvailable() 檢查角色狀態
  2. 如果有記事角色可用,請呼叫 createRequestRoleIntent() 以便獲得記事專屬的意圖
  3. 呼叫 startActivityForResult() 附註意圖,提示使用者授予記事角色 應用程式。

只有一個應用程式可擁有記事角色。

應用程式開啟以回應隱含 ACTION_CREATE_NOTE敬上 意圖動作如果從裝置螢幕鎖定畫面叫用,應用程式會完整開啟 螢幕;如果在浮動視窗中叫用螢幕時叫用。

應用程式資訊清單

應用程式必須加入下列宣告,才能使用記事角色 :

<activity
    android:name="YourActivityName"
    android:exported="true"
    android:showWhenLocked="true"
    android:turnScreenOn="true">
    <intent-filter>
        <action android:name="android.intent.action.CREATE_NOTE" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

這項聲明可讓使用者將記事角色指派給應用程式,以便取得 預設的記事應用程式:

  • ACTION_CREATE_NOTE敬上 設定應用程式回應的意圖動作

  • showWhenLocked敬上 方便您從裝置螢幕鎖定畫面存取您的應用程式

  • turnScreenOn 啟用 應用程式,在應用程式執行時開啟裝置螢幕

應用程式功能

一款與眾不同的應用程式,提供完善的記事應用程式 像是筆記功能

觸控筆支援

使用 EXTRA_USE_STYLUS_MODE敬上 意圖額外項目設為 true,應用程式應開啟接受觸控筆的記事 (或 。

如果意圖 Extra 設為 false,應用程式應開啟一則記事,表示接受 鍵盤輸入。

螢幕鎖定畫面存取權

您的應用程式必須提供全螢幕活動,在啟動應用程式時執行 並在裝置螢幕鎖定畫面上開啟相關檔案。

只有在使用者同意時 (在 解鎖裝置狀態) 來顯示過往的記事。否則當您從以下位置開啟檔案時 螢幕鎖定時,應用程式應一律建立新記事。

您可以使用下列程式碼,檢查應用程式是否已從螢幕鎖定畫面啟動 KeyguardManager#isKeyguardLocked()。 如想要求使用者驗證及解鎖裝置,請呼叫 KeyguardManager#requestDismissKeyguard()

Kotlin

val keyguardManager =
getSystemService(KEYGUARD_SERVICE) as KeyguardManager
keyguardManager.requestDismissKeyguard(  this, object :
KeyguardDismissCallback() {  override fun onDismissError() {  // Unlock failed.
Dismissing keyguard is not feasible.  }  override fun onDismissSucceeded() {  //
Unlock succeeded. Device is now unlocked.  }  override fun onDismissCancelled()
{  // Unlock failed. User cancelled operation or request otherwise cancelled.  }
 } )

Java

KeyguardManager keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);

boolean isLocked = keyguardManager.isKeyguardLocked();

keyguardManager.requestDismissKeyguard(
    this,
    new KeyguardManager.KeyguardDismissCallback() {

  @Override
  public void onDismissError() {
      // Unlock failed. Dismissing keyguard is not feasible.
  }

  @Override
  public void onDismissSucceeded() {
      // Unlock succeeded. Device is now unlocked.
  }

  @Override
  public void onDismissCancelled() {
      // Unlock failed. User cancelled operation or request otherwise cancelled.
  }
});

浮動視窗

針對上下文的記事,您的應用程式必須提供可在 浮動視窗

您的應用程式必須支援 multi-instance敬上 模式讓使用者可以在多個浮動視窗中建立多則記事 系統以全螢幕模式啟動記事應用程式,或在分割畫面中啟動記事應用程式 模式。

內容擷取

內容擷取是記事應用程式的重要功能,包含內容 可讓使用者擷取記事背後的螢幕畫面 浮動視窗。使用者可以擷取全部或部分內容,並將 編輯擷取的內容,並加上註解或醒目顯示擷取的內容。

記事應用程式應提供 UI 預設用途,啟動 ActivityResultLauncher敬上 建立者: registerForActivityResult()ACTION_LAUNCH_CAPTURE_CONTENT_ACTIVITY_FOR_NOTE敬上 意圖動作是直接或透過 ActivityResultContract

系統活動會擷取內容並將其儲存在裝置上,然後將 內容 URI registerForActivityResult()

以下範例使用通用 StartActivityForResult敬上 合約:

Kotlin

private val startForResult =
registerForActivityResult(  ActivityResultContracts.StartActivityForResult()) {
 result: ActivityResult ->  if (result.resultCode ==
Intent.CAPTURE_CONTENT_FOR_NOTE_SUCCESS) {  val uri = result.data?.data  // Use
the URI to paste the captured content into the note.  }  } override fun
onCreate(savedInstanceState: Bundle?) {  super.onCreate(savedInstanceState)
setContent {  NotesTheme {  Surface(color =
MaterialTheme.colorScheme.background) {  CaptureButton(  onClick = {
Log.i("ContentCapture", "Launching intent...")
startForResult.launch(Intent(ACTION_LAUNCH_CAPTURE_CONTENT_ACTIVITY_FOR_NOTE))
})  }  }  } } @Composable fun CaptureButton(onClick: () -> Unit) {
Button(onClick = onClick)
 {Text("Capture Content")} }

Java

private final ActivityResultLauncher<Intent> startForResult = registerForActivityResult(
    new ActivityResultContracts.StartActivityForResult(),
    result -> {
        if (result.getResultCode() == Intent.CAPTURE_CONTENT_FOR_NOTE_SUCCESS) {
            Uri uri = result.getData() != null ? result.getData().getData() : null;
            // Use the URI to paste the captured content into the note.
        }
    });

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button captureButton = findViewById(R.id.capture_button);

    captureButton.setOnClickListener(
        view -> {
            Log.i("ContentCapture", "Launching intent...");
            startForResult.launch(new Intent(ACTION_LAUNCH_CAPTURE_CONTENT_ACTIVITY_FOR_NOTE));
        });
}

應用程式應處理所有結果代碼:

內容擷取成功後,請將擷取的圖片貼到記事 範例:

Kotlin

registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
 result: ActivityResult ->  if (result.resultCode ==
Intent.CAPTURE_CONTENT_FOR_NOTE_SUCCESS) {  val uri = result.data?data  // Use
the URI to paste the captured content into the note.  } }

Java

registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
    result -> {
        if (result.getResultCode() == Intent.CAPTURE_CONTENT_FOR_NOTE_SUCCESS) {
            Uri uri = result.getData() != null ? result.getData().getData() : null;
            // Use the URI to paste the captured content into the note.
        }
    });

只有在出現時機,您才應透過 UI 預設用途提供內容擷取功能 記事應用程式是在浮動視窗中執行,而不是在 執行全螢幕 (從裝置螢幕鎖定畫面啟動)。(使用者可以 記事應用程式本身的螢幕截圖 (當中顯示裝置螢幕截圖) 功能)。

如要判斷應用程式是否位於浮動視窗 (或對話框中),請呼叫 方法如下:

  • isLaunchedFromBubble()敬上 ,檢查您的記事應用程式是否從以下位置啟動全螢幕 裝置螢幕鎖定畫面。
  • isRoleHeld(RoleManager.ROLE_NOTES)敬上 ,驗證您的應用程式是否為預設的記事應用程式 (您的應用程式可以 在對話或其他類型的對話框中,如果應用程式沒有回應 記事角色)

其他資源