建立 QR code 付款快速設定方塊

「快速設定」方塊會顯示在「快速設定」面板中,使用者可以輕觸這些方塊,快速完成週期性工作。 本文件說明如何為 QR code 付款建立自訂「快速設定」圖塊。

繼續操作前,請務必熟悉為應用程式建立自訂快速設定資訊方塊的一般操作說明和最佳做法。

如要建立資訊方塊,請按照下列步驟操作:

  1. 建立自訂圖示
  2. 建立並宣告 TileService

  3. 如要啟動 QR code 付款功能,請填寫 onClick() 方法。長按資訊方塊後,系統便會向使用者顯示「應用程式資訊」畫面。如要覆寫此行為並啟動活動以設定偏好設定,請使用 ACTION_QS_TILE_PREFERENCES<intent-filter> 加入其中一個活動。

    Kotlin

    import android.service.quicksettings.TileService
    
    // Called when the user taps on your tile in an active or inactive state.
    override fun onClick() {
       // Create Intent, replace MainActivity::class.java with QR Code Activity
       val intent = Intent(this, MainActivity::class.java)
       // Create PendingIntent
       val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE)
       if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
         startActivityAndCollapse(pendingIntent)
       } else {
         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
         startActivityAndCollapse(intent)
       }
    }
    

    Java

    import android.service.quicksettings.TileService;
    
    // Called when the user taps on your tile in an active or inactive state.
    @Override
    public void onClick() {
     // Create Intent, replace MainActivity.class with QR Code Activity
     Intent intent = new Intent(MyQSTileService.this, MainActivity.class);
     // Create PendingIntent
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE);
     if (VERSION.SDK_INT >= VERSION_CODES.UPSIDE_DOWN_CAKE) {
       startActivityAndCollapse(pendingIntent);
     } else {
       intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
       startActivityAndCollapse(intent);
     }
    }
    
  4. 為了保護使用者機密的付款資訊,請只在安全鎖定的裝置上執行安全操作

    Kotlin

    import android.service.quicksettings.TileService
    
    override fun onClick() {
       val intent = Intent(this, MainActivity::class.java)
       val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE)
    
       // ...
    
       if (isSecure()) {
           startActivityAndCollapse(pendingIntent)
       } else {
           unlockAndRun {
               startActivityAndCollapse(pendingIntent)
           }
       }
       // ...
    }
    

    Java

    import android.service.quicksettings.TileService;
    
    @Override
    public void onClick() {
     Intent intent = new Intent(MyQSTileService.this, MainActivity.class);
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE);
     ...
     if (isSecure()) {
       startActivityAndCollapse(pendingIntent);
     } else {
       unlockAndRun(new Runnable() {
         @Override
         public void run() {
           startActivityAndCollapse(pendingIntent);
         }
       });
      }
     ...
    }
    
  5. 第一次介紹這項功能時,請提示使用者新增資訊方塊