4

I am breaking my head around it with no solution's so far. I seen many guides how to accomplish this (Documentation, Tutorial) and successfully made it happen in android 7. What I managed to do is successfully get notification with service running at background when user activity changed. My app works perfectly. The problem is with android 8 and google battery management. My service is always killed. So... What I already tried to overcome this problem:

  1. Explain the user how to disable battery optimization for my app so it will not kill my service. The problem is its too comlicated for cummon user.

  2. Add

    uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"
    

and check it with code:

Intent intent = new Intent();
            String packageName = context.getPackageName();
            PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                if (pm.isIgnoringBatteryOptimizations(packageName)){
                    intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);

                }
                else {
                    intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
                    intent.setData(Uri.parse("package:" + packageName));
                }
            }
            context.startActivity(intent);

But I undersood my app can be banned from play store, and this is big problem.

  1. Use foreground service with persistent notification. The problem is its very anoin to user to see this ongoing and sometimes irrelevant notification.

So how can I use ActivityRecognitionClient and always receive notification when activity changed even when my app is in background in android 8? Maybe connecting directly google service for this somehow?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Dim
  • 4,527
  • 15
  • 80
  • 139
  • Would a `DetectedActivityFence` be useful? See e.g. https://developers.google.com/awareness/android-api/fence-api-overview – stkent May 25 '18 at 13:06
  • Actually it is useful. I maybe will use this API instead of current API, it have more functions. But it still require service to run in the background, isn't it? If it does the problem remains. – Dim May 25 '18 at 17:51
  • 1
    I'm not sure how the awareness APIs themselves are affected by changes in Oreo, but the system monitors and sends the broadcasts. Once you receive a broadcast for a fence, you'd be safest to use `startForegroundService()` while responding to the broadcast. This works even when your app is in the background and the corresponding user-facing notification will only be visible for a short while. – stkent May 25 '18 at 19:11
  • Awareness API is actually what I need with use of Broadcast Receiver I can do what I wanted. Can you please post it as answer? – Dim Jun 03 '18 at 07:51
  • Sure thing; posted! Glad this worked out for you :) – stkent Jun 03 '18 at 12:23

1 Answers1

3

Consider using a DetectedActivityFence, part of the Awareness APIs. In this case it is the system that monitors and sends you broadcasts, so there's no need to run your own background service.

Once you receive a broadcast for a fence, you can use startForegroundService to respond to the broadcast. This works even when your app is in the background and the corresponding user-facing notification will only be visible for a short while.

stkent
  • 19,772
  • 14
  • 85
  • 111
  • Hi, I have migrated my app to use DetectedActivityFence. It work so so... sometimes it takes too long to receive broadcast about activity change. But mostly it works fine. But on some devices like Samsung it not works at all when the app is closed. Do you have any idea how to improve refresh time or why it not working (not receiving broadcast) on some devices? – Dim Jul 31 '18 at 15:17
  • Unfortunately I don't have any advice for addressing the issues you described :/ – stkent Jul 31 '18 at 23:48
  • @Dim Did you find any solution to the Samsung issue? – dor506 Oct 22 '18 at 11:11
  • @dor506 Sorry but no. I found some other mobile phones (Some Xiaomi models) with that issue. Because it is very hard to debug and test I left the app which I developing for now. Maybe I will get back to it after ActivityRecognitionApi will be updated or some other solution. If you come by any solution I be glad to hear from you. – Dim Oct 22 '18 at 15:08
  • Just fyi, not sure if it will make a difference but android have released a newer api called Activity transition Api. https://developer.android.com/guide/topics/location/transitions – Andrew Irwin Oct 25 '18 at 15:09