Create a QR Code Payment Quick Settings tile

Quick Settings are tiles displayed in the Quick Settings panel. Users can tap these tiles to quickly complete recurring tasks. This document shows you how to create a custom Quick Settings tile for QR Code payments.

Before continuing, be sure you're familiar with general instructions and best practices for creating custom Quick Settings tiles for your app.

To create your tile, follow these steps:

  1. Create your custom icon.
  2. Create and declare your TileService.

  3. To launch the QR Code payment, fill in the onClick() method. Long-tapping a tile prompts the App Info screen for the user. To override this behavior and instead launch an activity for setting preferences, add an <intent-filter> to one of your activities with 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. To protect users' sensitive payment information, perform only safe actions on securely-locked devices.

    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. When first introducing this feature, prompt the user to add your tile.