Android 13 - 用于快速设置面板的 TileService 在单击时不会折叠

Pra*_*ani 5 android flutter android-app-quick-setting tile-service

简短问题:startActivityAndCollapse不适用于 Android 13

长问题:我正在为快速设置面板创建一个图块。我尝试实现这个演示。它适用于除Android 13之外的所有其他设备

override fun onClick() {
    super.onClick()
    try {
        val newIntent =
            FlutterActivity.withNewEngine().dartEntrypointArgs(listOf("launchFromQuickTile"))
                .build(this)
        newIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
        startActivityAndCollapse(newIntent)
    } catch (e: Exception) {
        Log.d("debug", "Exception ${e.toString()}")
    }
}
Run Code Online (Sandbox Code Playgroud)

上面的代码可以打开应用程序,但不会折叠快速设置面板。

有什么解决办法,有帮助吗?

编辑:

我对此进行了更多研究,发现只有当我通过 Android 活动时它才有效。

示例(安卓):

val newIntent = Intent(this, MainActivity::class.java)
newIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
startActivityAndCollapse(newIntent)
Run Code Online (Sandbox Code Playgroud)

示例(颤振):

val newIntent = FlutterActivity.withNewEngine().dartEntrypointArgs(listOf("launchFromQuickTile")).build(this)
newIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
startActivityAndCollapse(newIntent)
Run Code Online (Sandbox Code Playgroud)

还有其他方法可以使用 Params 打开 Flutter App 吗?

Rah*_*hul 2

我尝试了以下代码并且它有效。我使用了https://github.com/android/user-interface-samples/tree/main/Quick-Settings示例应用程序。

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.a">
   <application
        android:label="a"
        android:name="${applicationName}"
        android:icon="@mipmap/ic_launcher">
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that, this theme continues
                 to determine the Window background behind the Flutter UI. -->
            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />

       <service
           android:name=".QSIntentService"
           android:label="Flutter QuickTile"
       android:icon="@mipmap/ic_launcher"
                android:exported="true"
       android:permission="android.permission.BIND_QUICK_SETTINGS_TILE">
       <intent-filter>
           <action android:name="android.service.quicksettings.action.QS_TILE" />
       </intent-filter>
   </service>
    </application>
</manifest>

Run Code Online (Sandbox Code Playgroud)

MainActivity.kt

package com.example.a

import android.R
import android.app.StatusBarManager
import android.content.ComponentName
import android.os.Build
import android.os.Build.VERSION
import android.os.Bundle
import android.service.quicksettings.TileService
import androidx.core.graphics.drawable.IconCompat
import io.flutter.embedding.android.FlutterActivity
import com.google.common.util.concurrent.MoreExecutors


class MainActivity: FlutterActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        if (VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            val statusBarService = this.getSystemService(
                StatusBarManager::class.java
            )
            val componentName = ComponentName(
                this.applicationContext,
                TileService::class.java.getName()
            )

            statusBarService.requestAddTileService(
                componentName,
                "Quick Settings",
                IconCompat.createWithResource(
                    applicationContext,
                    R.drawable.stat_sys_warning).toIcon(this)
                ,
                MoreExecutors.directExecutor()
            ) { integer: Int? ->
                setResult(integer!!)
                finish()
            }
        }

    }
}

Run Code Online (Sandbox Code Playgroud)

TileService.kt

package com.example.a

import android.content.Intent
import android.service.quicksettings.Tile
import android.service.quicksettings.TileService
import io.flutter.embedding.android.FlutterActivity.withNewEngine


class QSIntentService : TileService() {
    override fun onClick() {

        // Check to see if the device is currently locked.
        val isCurrentlyLocked = this.isLocked
        if (!isCurrentlyLocked) {
            val tile = qsTile
            val tileLabel = tile.label.toString()
            val tileState: String =
                if (tile.state == Tile.STATE_ACTIVE) "Active" else "Inactive"
            val intent =
                withNewEngine().dartEntrypointArgs(listOf("launchFromQuickTile"))
                .build(this)
                .setClass(this, MainActivity::class.java)
                    .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            startActivityAndCollapse(intent)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

构建.gradle

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
    compileSdkVersion flutter.compileSdkVersion
    ndkVersion flutter.ndkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = '1.8'
    }

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.example.a"
        // You can update the following values to match your application needs.
        // For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-build-configuration.
        minSdkVersion 24
        targetSdkVersion flutter.targetSdkVersion
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

    buildTypes {
        release {
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig signingConfigs.debug
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.5.1'
    implementation 'com.google.guava:guava:31.1-android'
}
Run Code Online (Sandbox Code Playgroud)

主程序.dart

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
    compileSdkVersion flutter.compileSdkVersion
    ndkVersion flutter.ndkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = '1.8'
    }

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.example.a"
        // You can update the following values to match your application needs.
        // For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-build-configuration.
        minSdkVersion 24
        targetSdkVersion flutter.targetSdkVersion
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

    buildTypes {
        release {
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig signingConfigs.debug
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.5.1'
    implementation 'com.google.guava:guava:31.1-android'
}
Run Code Online (Sandbox Code Playgroud)

酒吧规范。

import 'package:flutter/material.dart';

void main(args) {
  WidgetsFlutterBinding.ensureInitialized();
  print(args);
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'List',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      debugShowCheckedModeBanner: false,
      home: const Text('Foobar'),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

输出 I/颤振:[launchFromQuickTile]

Stackoverflow 不允许视频嵌入,因此我将工作上传到了 Youtube 上。您可以在此处观看视频https://youtu.be/W_iM2yR_07Y