Android 13 -用于快速设置面板的磁贴服务不会在点击时折叠

unhi4e5o  于 2022-12-31  发布在  Android
关注(0)|答案(1)|浏览(160)
    • bounty将在2天后过期**。回答此问题可获得+50声望奖励。Pratik Butani希望引起更多人关注此问题。

简短问题:* * 启动活动和折叠不适用于Android 13
长问题:我正在为快速设置面板创建一个磁贴。我尝试过实现这个demo。它在除
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()}")
    }
}

上面的代码可以打开应用程序,但不能折叠快速设置面板。
有什么解决办法,有什么帮助吗?

编辑日期:

我已经看了更深入,发现它的工作只有当我通过Android的活动.
示例**(安卓系统):**

val newIntent = Intent(this, MainActivity::class.java)
newIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
startActivityAndCollapse(newIntent)

示例**(扑动):**

val newIntent = FlutterActivity.withNewEngine().dartEntrypointArgs(listOf("launchFromQuickTile")).build(this)
newIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
startActivityAndCollapse(newIntent)

是否有其他方法可以使用参数打开Flutter应用程序?

plicqrtu

plicqrtu1#

我尝试了下面的代码,它工作。我使用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>

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()
            }
        }

    }
}

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)
        }
    }
}

build.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'
}

main.dart

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'),
    );
  }
}

出版规范

name: a
description: A new Flutter project.

publish_to: "none"

version: 1.0.0+1

environment:
  sdk: ">=2.18.5 <3.0.0"

dependencies:
  cupertino_icons: ^1.0.2
  flutter:
    sdk: flutter

dev_dependencies:
  flutter_lints: ^2.0.0
  flutter_test:
    sdk: flutter

flutter:
  uses-material-design: true

输出I/ Flutter :[从快速平铺启动]
Stackoverflow不允许视频嵌入,因此我上传了Youtube上的工作。你可以在这里看到视频https://youtu.be/W_iM2yR_07Y

相关问题