Bo Hellgren
Bo Hellgren

Reputation: 181

Cannot start Activity from Service

I have made a Flutter app for Android Auto which displays a diagram on the phone screen and then also displays the same diagram on the AA screen in the car. It has a standard Flutter MainActivity, and a service provided by me. If I tap the icon on the phone, Android starts both the activity and the service. But if I start the app from the car display, AA only starts the service. To fix this I tried to start MainActiviy from the service with

                    val intent = Intent(carContext, MainActivity::class.java).apply {
                                addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                            }
                            val pendingIntent : PendingIntent = PendingIntent.getActivity(
                                carContext,
                                0,
                                intent,
                                PendingIntent.FLAG_IMMUTABLE
                            )
                            // Check Android version before calling the method
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
                                val options = ActivityOptions.makeBasic()
                                options.setPendingIntentBackgroundActivityStartMode(
                                    ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_ALLOWED)
                                pendingIntent.send(options.toBundle())
                            } else {
                                pendingIntent.send()
                            }

In the manifest, I have the Flutter standard lines

            <activity
                android:name=".MainActivity"
                android:exported="true"
                android:launchMode="singleTop"
                android:taskAffinity=""
                android:theme="@style/LaunchTheme"
                android:configChanges= etc...
                android:hardwareAccelerated="true"
                android:windowSoftInputMode="adjustResize">
            <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>

Now it works on an emulated Pixel 8a API 33. So there it does not need the MODE_BACKGROUND_ACTIVITY_START_ALLOWED. But on a real Galaxy Watch S10e API 31 it does not work! Nothing happens. Why?? I get the log messages below. The warning says allowBackgroundActivityStart: false. If that is the problem, using MODE_BACKGROUND_ACTIVITY_START_ALLOWED requires API 34. How can I get it to work on API 31 also?

2025-01-14 11:54:54.751 31406-31406 CarApp.H                pid-31406                            I  App: [se.ndssoft.autoglucose/.CarHomeService] app info: [Library version: [1.7.0-beta03] Min Car Api Level: [1] Latest Car App Api Level: [8]] Host min api: [1]  Host max api: [7] [CONTEXT ratelimit_period="5 SECONDS" skipped=94 ]
2025-01-14 11:54:54.752 31406-31406 CarApp.H                pid-31406                            D  App: [se.ndssoft.autoglucose/.CarHomeService], Host negotiated api: [7] [CONTEXT ratelimit_period="5 SECONDS" skipped=94 ]
2025-01-14 11:54:54.788  6130-6130  Compatibil...geReporter se.ndssoft.autoglucose               D  Compat change id reported: 160794467; UID 11536; state: ENABLED
2025-01-14 11:54:54.807   969-4019  ActivityTaskManager     pid-969                              I  START u0 {flg=0x10000000 cmp=se.ndssoft.autoglucose/.MainActivity} from uid 11536
2025-01-14 11:54:54.814   969-4019  ActivityTaskManager     pid-969                              W  Background activity start [callingPackage: se.ndssoft.autoglucose; callingUid: 11536; appSwitchAllowed: true; isCallingUidForeground: true; callingUidHasAnyVisibleWindow: false; callingUidProcState: BOUND_TOP; isCallingUidPersistentSystemProcess: false; realCallingUid: 11536; isRealCallingUidForeground: true; realCallingUidHasAnyVisibleWindow: false; realCallingUidProcState: BOUND_TOP; isRealCallingUidPersistentSystemProcess: false; originatingPendingIntent: PendingIntentRecord{952b2d8 se.ndssoft.autoglucose startActivity}; allowBackgroundActivityStart: false; intent: Intent { flg=0x10000000 cmp=se.ndssoft.autoglucose/.MainActivity }; callerApp: ProcessRecord{f53a5b7 6130:se.ndssoft.autoglucose/u0a1536}; inVisibleTask: false]
2025-01-14 11:54:54.819   969-4019  ActivityTaskManager     pid-969                              D  TaskLaunchParamsModifier:task=null activity=ActivityRecord{d4b3a97 u0 se.ndssoft.autoglucose/.MainActivity display-id=0 display-windowing-mode=1 suggested-display-area=DefaultTaskDisplayArea@115968570
2025-01-14 11:54:54.820   969-4019  [secipm]                pid-969                              D  mSecIpmManager Preload se.ndssoft.autoglucose dex files
2025-01-14 11:54:54.823   969-4019  ActivityTaskManager     pid-969                              D  TaskLaunchParamsModifier:task=null activity=ActivityRecord{d4b3a97 u0 se.ndssoft.autoglucose/.MainActivity t-1} display-id=0 display-windowing-mode=1 suggested-display-area=DefaultTaskDisplayArea@115968570 non-freeform-display display-area=DefaultTaskDisplayArea@115968570 maximized-bounds
2025-01-14 11:54:54.823   969-2523  PkgPredict...Controller pid-969                              D  create a new DexPreloadTask pkg:se.ndssoft.autoglucose  path:/data/app/~~TMfK3jVa236aEXamP4Tehw==/se.ndssoft.autoglucose-975VscmnCKXNJVH5wiZSMw==
2025-01-14 11:54:54.824   969-4019  ActivityManager         pid-969                              D  Received ACTIVITY intent 0x952b2d8 Key{startActivity pkg=se.ndssoft.autoglucose intent=flg=0x10000000 cmp=se.ndssoft.autoglucose/.MainActivity flags=0x4000000 u=0} requestCode=0 res=0 from uid 11536
2025-01-14 11:54:54.829   969-2523  PkgPredict...Controller pid-969                              D  /data/app/~~TMfK3jVa236aEXamP4Tehw==/se.ndssoft.autoglucose-975VscmnCKXNJVH5wiZSMw==/oat/arm64/base.odex is already preloaded

Upvotes: 0

Views: 48

Answers (1)

Ben Sagmoe
Ben Sagmoe

Reputation: 1447

Without logs, my best guess would be that you're running into some of the restrictions on background activity launching.

Upvotes: 0

Related Questions