创建二维码支付快速设置磁贴

快速设置是显示在快速设置面板中的磁贴。用户可以点击这些磁贴来快速完成重复性任务。本文件将向您展示如何为二维码支付创建自定义快速设置磁贴。

在继续之前,请确保您熟悉为您的应用创建自定义快速设置磁贴 的一般说明和最佳实践。

创建您的磁贴,请按照以下步骤操作

  1. 创建您的自定义图标.
  2. 创建并声明您的TileService.

  3. 要启动二维码支付,请填写onClick()方法。长按磁贴会提示用户的应用信息屏幕。要覆盖此行为并改为启动用于设置首选项的活动,请向您的一个活动添加<intent-filter>,其中包含ACTION_QS_TILE_PREFERENCES

    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. 首次引入此功能时,请提示用户添加您的磁贴