android / codelab-activity_transitionapi Goto Github PK
View Code? Open in Web Editor NEWCodelab walks you through tracking activity transition changes (start/stop run, walk, idle, drive, etc.).
License: Other
Codelab walks you through tracking activity transition changes (start/stop run, walk, idle, drive, etc.).
License: Other
I'm using the Transition API in a foreground service (by showing an ongoing notification) to constantly detect the user's physical activity transitions. It worked pretty well on Galaxy S9. However, when I tested the app on Pixel 5, I got multiple transitions from STILL to WALKING and WALKING to STILL even though the user was just picking up their phone and putting it back down. I was wondering if anyone encountered a similar issue. Thanks!
I did no changes in the code, compiled, ran on Samsung with Android 13, application was terminated with following error, adding FLAG_IMMUTABLE to PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0) eliminates the exception, but no activity is reported
FATAL EXCEPTION: main
Process: com.google.example.android.basicactivityrecognitiontransitionsample, PID: 21094
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.google.example.android.basicactivityrecognitiontransitionsample/com.google.example.android.basicactivityrecognitiontransitionsample.MainActivity}: java.lang.IllegalArgumentException: com.google.example.android.basicactivityrecognitiontransitionsample: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent. Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.
Underneath the third code snippet, it says
Here we as if activity recognition is approved. If it is and activity recognition is already enabled, we disable it. Otherwise, we enable it.
Shouldn't it be?
Here we (check/verify) if activity recognition is approved. If it is and activity recognition is already enabled, we disable it. Otherwise, we enable it.
I tested this application, but it seems it doesn't work. (Cloned directly from this repo)
We compiled this example and it works well on a Google Pixel 2 running Android 11. We also integrated the activity transition API into our app and again, it works well on the Pixel 2. Then we started testing on a Samsung S9 and we are getting mixed results. As mentioned, we integrated the Activity Transition API into our app as well, and had both installed on the same phone for comparison. We also downloaded and installed other apps that utilize the Activity Transition API. We wanted to rule out any bugs in our code or possibly the demo code.
It was working fine for a while, then the API just suddenly stopped reporting activity transitions to all the apps. Our app creates a log for debugging and we can see our app suddenly just stops receiving activity transition updates. At the same time we checked the other apps and found the same result - all of them stopped receiving transition updates at the same time. Even after a full hard reboot of the Samsung phone, none of the apps were receiving transition updates anymore. Even tried uninstalling / reinstalling apps - no difference. All activity transitions stopped.
Then suddenly, they will start coming again. We do nothing different. All the apps registered for activity transitions suddenly started receiving them again. Then they will stop again as suddenly as they start.
We have tested with a few other phones and so far the Samsung S9+ is the only one doing this. There doesn't seem to be a pattern either. We can leave the phone sitting for hours, pick it up, and it works. We can do many transitions sequentially and it works. Then when it decides to stop working, we cannot get it working again even with a reboot. We just have to wait until it decides to work again.
Samsung Galaxy S9+
Android 10
Perhaps the codelab should link to where the repository is, instead of directly to the zip file:
https://github.com/googlecodelabs/activity_transitionapi-codelab
in https://developer.android.com/codelabs/activity-recognition-transition#5
you should not pass 0 for the flags rather the pending intent needs tp specify the mutability flag.
mActivityTransitionsPendingIntent =
PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
In the Register a BroadcastReceiver for the transition section the
search for TODO: Register a BroadcastReceiver to listen for activity transitions
sentence should be replaced by
search for TODO: Register the BroadcastReceiver to listen for activity transitions
no changes, only updated pending intent flags and build , its never called onreceive in API 33.
The process to remove updates when the app is closing is repeated along with the code to follow in step 6 (Setup activity transitions and request updates).
I have added the link for reference here.https://codelabs.developers.google.com/codelabs/activity-recognition-transition/index.html?index=..%2F..index#5
I am using s22, android 12.
When receive an intent ,there is extra :"com.google.android.location.internal.EXTRA_ACTIVITY_RESULT", however from the source code:
return !hasResult(var0) ? null : (ActivityTransitionResult)SafeParcelableSerializer.deserializeFromIntentExtra(var0, "com.google.android.location.internal.EXTRA_ACTIVITY_TRANSITION_RESULT", CREATOR);
It should be com.google.android.location.internal.EXTRA_ACTIVITY_TRANSITION_RESULT, so Is there a bug in samsung phone?
TransitionReceiver is not a class that I can access when I make this variable in my own empty project (not using the codelab file). It shows highlighted read when I try to make this a variable - see pictures attached.
I am using Android Studio version 4.0.1.
Any ideas what class I can use to replace this?
The link to repository download is not working, as it is
https://github.com/googlecodelabs/activity_transitionapi-codelab/BasicActivityTransitionsCodelab.zip
and it should be:
https://github.com/googlecodelabs/activity_transitionapi-codelab/blob/master/BasicActivityTransitionsCodelab.zip
Hi.
This happens after 2~3 days of using my app to track walking and running. Here is the code which is called every hour in the onStartJob
of a JobService.
val request = ActivityTransitionWalkRequest.build()
val client = ActivityRecognition.getClient(appContext)
client
.requestActivityTransitionUpdates(request, getPendingIntent(context))
.apply {
addOnSuccessListener { onDone.invoke(true) }
addOnFailureListener { onDone.invoke(false) }
}
I see from my logs that addOnSuccessListener
is called, but also that the a SecurityException
is thrown, and my BroadcastReceiver doesn't receive any transition events anymore. Here is some stacktrace:
16:51:27.602 E Bad call made by uid 10116. Package "xxx.xxx" does not belong to uid 10283.
16:51:27.602 E checkOperation
java.lang.SecurityException: Specified package "xxx.xxx" under uid 10283 but it is not
at com.android.server.appop.AppOpsService.verifyAndGetBypass(AppOpsService.java:4696)
at com.android.server.appop.AppOpsService.verifyAndGetBypass(AppOpsService.java:4564)
at com.android.server.appop.AppOpsService.checkOperationUnchecked(AppOpsService.java:3253)
at com.android.server.appop.AppOpsService.checkOperationImpl(AppOpsService.java:3236)
at com.android.server.appop.AppOpsService.-$$Nest$mcheckOperationImpl(Unknown Source:0)
at com.android.server.appop.AppOpsService$CheckOpsDelegateDispatcher.lambda$checkOperation$0(AppOpsService.java:7596)
at com.android.server.appop.AppOpsService$CheckOpsDelegateDispatcher.$r8$lambda$KrwrxMLmGzFSEjEyo0aJ63o5J9E(Unknown Source:0)
at com.android.server.appop.AppOpsService$CheckOpsDelegateDispatcher$$ExternalSyntheticLambda9.apply(Unknown Source:24)
at com.android.server.policy.AppOpsPolicy.checkOperation(AppOpsPolicy.java:213)
at com.android.server.appop.AppOpsService$CheckOpsDelegateDispatcher.checkOperation(AppOpsService.java:7595)
at com.android.server.appop.AppOpsService.checkOperation(AppOpsService.java:3223)
at com.android.internal.app.IAppOpsService$Stub.onTransact(IAppOpsService.java:457)
at android.os.Binder.execTransactInternal(Binder.java:1280)
at android.os.Binder.execTransact(Binder.java:1244)
What can be the cause of this?
Thanks for any help. I can provide more source if needed.
Extra Note
in the addOnSuccessListener
callback, I register a broadcast receiver like this:
unregisterReceiver(transitionsReceiver)
if (transitionsReceiver == null) {
transitionsReceiver = ActivityTransitionReceiver()
}
registerReceiver(transitionsReceiver, IntentFilter(RECEIVER_ACTION))
Another observation: after restarting the phone, the security exception is no longer thrown and I receive transition updates.
Device / OS
Pixel 6 / Android 13
I am using Android 9 Xiaomi but it is not working.
I ran the complete
debug variant of the app on Galaxy Z Flip 3 5G (Android 12) and Xiaomi Redmi K40 (Android 12). After walking for a 10 metres or so, TransitionsReceiver.onReceive
was called. I assume it was walking activity detected. But intent.getExtras()
was null
. Hence, ActivityTransitionResult.hasResult(intent)
returned false
, and no information was printed.
I tried 31 and 32 for targetSdkVersion
and compileSdkVersion
What is the issue?
No activity is detected.
The sample app is not running on any of the Android 10 devices we tested on despite following the tutorial fully. Perhaps some permissions have changed?
I am trying the Activity transition API to detect the user activity, it is working fine when app is in foreground, but as soon the get closed from recent app list, the detection of Activity is stopped. The broadcast receiver is not receiving any change in activity after app close.
There's the sentence "Here we as if activity recognition is approved. If it is and activity recognition is already enabled, we disable it. Otherwise, we enable it."
The word 'as' should be 'ask'.
I get the following error when I try to run the codelab:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.google.example.android.basicactivityrecognitiontransitionsample, PID: 10099
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.google.example.android.basicactivityrecognitiontransitionsample/com.google.example.android.basicactivityrecognitiontransitionsample.MainActivity}: java.lang.IllegalArgumentException: com.google.example.android.basicactivityrecognitiontransitionsample: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3635)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3792)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2210)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7839)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
Caused by: java.lang.IllegalArgumentException: com.google.example.android.basicactivityrecognitiontransitionsample: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.
at android.app.PendingIntent.checkFlags(PendingIntent.java:375)
at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:645)
at android.app.PendingIntent.getBroadcast(PendingIntent.java:632)
at com.google.example.android.basicactivityrecognitiontransitionsample.MainActivity.onCreate(MainActivity.java:136)
at android.app.Activity.performCreate(Activity.java:8051)
at android.app.Activity.performCreate(Activity.java:8031)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1329)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3608)
... 12 more
Hi!
I have donwloaded and executed the code you provide but no activity is being detected. Is ther a problem in the code du to recent Android updates? What can be done to have the app work as expected?
Thanks in advance for your answer.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.