-1

The code below works without any problem on pre-Oreo versions.

Although I user startForeground method and making explicit intent to get Activity values, onHandleIntent method is never called.

I cannot find any issue or solution about this issue.

Is there any chance to overcome this issue?

Note: I try on my phone which runs a custom ROM and I can not simulate on Android Emulator this circumstance.

AndroidManifest.xml file

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.myservice"
    android:versionCode="1"
    android:versionName="1.0">

    <application ... >

      <service android:name="com.MyService">
        <intent-filter>
          <action android:name="com.MyService"/>
        </intent-filter>
      </service>

      <uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />

      <service android:name="com.myservice.ActivityHelper$ActivityRecognitionService" />

    </application>

</manifest>

MyService.java file

public class MyService extends Service {

    ...

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);

        runServiceAtForeground();

        return START_STICKY;
    }


    private void runServiceAtForeground () {
        ...

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            ...
            mNotificationManager.createNotificationChannel(channel);
        }

        Intent i = new Intent(getApplicationContext(), MyService.class);
        PendingIntent pi = PendingIntent.getActivity(this, 0, i, 0);
        NotificationCompat.Builder notif = new NotificationCompat.Builder(this, channel_id);
        notif.setContentIntent(pi);

        // MAKING FOREGROUND SERVICE
        startForeground(notif_id, notif.build());

        // STARTING ACTIVITY RECOGNITION
        mActivityHelper.startObserving();

    }

    ...

}

ActivityHelper.java file

public class ActivityHelper {

    ...

    public void startObserving() {
        ActivityRecognitionClient arc = ActivityRecognition.getClient(context);

        Task<Void> task = arc.requestActivityUpdates(20000, getActivityDetectionPendingIntent());

        task.addOnSuccessListener(new OnSuccessListener<Void>() {...});

        task.addOnFailureListener(new OnFailureListener() {...});
    }

    private PendingIntent getActivityDetectionPendingIntent() {
            Intent i = new Intent(mContext, ActivityRecognitionService.class);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                return PendingIntent.getBroadcast(mContext, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
            } else {
                return PendingIntent.getService(mContext, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
            }
    }


    public static class ActivityRecognitionService extends IntentService {

        public ActivityRecognitionService() {
            super("ActivityRecognitionService");
        }

        @Override
        public void onCreate() {
            super.onCreate();
        }

        @Override
        protected void onHandleIntent(Intent intent) {

            // THIS METHOD IS NEVER RUN ON OREO

            if (ActivityRecognitionResult.hasResult(intent)) {
                ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);

                if (listener != null) {
                    Log.v(TAG, result.getMostProbableActivity().getType());
                }

                stopSelf();
            }
        }

    }

}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
efkan
  • 12,991
  • 6
  • 73
  • 106
  • Do you see any errors in logcat? And how are you starting the service? – Sagar May 23 '18 at 12:18
  • @Sagar I encountered ths issue when using on a custom Android ROM. The code works on an official Android OS flawlessly. I could not answer your question. Logcat was writing nothing with related the code above. It was an unusual issue. Thanks for taking your time. – efkan May 23 '18 at 19:08

1 Answers1

0
private PendingIntent getActivityDetectionPendingIntent() {
        Intent i = new Intent(mContext, ActivityRecognitionService.class);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            return PendingIntent.getBroadcast(mContext, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
        } else {
            return PendingIntent.getService(mContext, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
        }
}

Either ActivityRecognitionService is a BroadcastReceiver or it is a Service. It cannot be both simultaneously. That does not change based upon what OS version the device is running. And since ActivityRecognitionService extends IntentService, PendingIntent.getBroadcast() will not give you a useful PendingIntent.

Also:

  • MyService is unused, insofar as it is not in your manifest snippet and it is not referenced from your other code snippets

  • The runServiceAtForeground() method on MyService creates an activity PendingIntent pointing at MyService, but MyService is not an activity, and so this will not work

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thank you very much. You are right (I've edited the question). Actually I had inserted `getBroadcast` because I thought it might be a solution after encountered this issue. I had tried to make the code as readable as possible. But somethings have been overlooked. I guess `runServiceAtForeground()` method you pointed may be the problem. I'll try to solve it right away. – efkan May 23 '18 at 13:06
  • I've checked the code. runServiceAtForeground() method works. Actually you were right and now I changed it as getService(). However it's not the issue because of `pendingIntent` expects a `Context` as parameter, I guess so (pls forgive me if I'm wrong as one who benefits your articles). As a result, `onHandleIntent` does not work still. There must be another thing I overlooked. – efkan May 23 '18 at 13:53
  • @efkan: I have not used the activity recognition APIs, so I do not know if there are any problems specific to them. Check LogCat to see if you are getting messages from system processes regarding the inability to use this service. Also, you might try starting the service manually via `startService()` and see if that works. – CommonsWare May 23 '18 at 14:04
  • FYI, the code works on Android 8.0 (Samsung Galaxy) flawlessly. I have been trying to get worked the code on Android 8.1 (LineageOS on Xperia Z). I've spent the all day for this. So the problem can also be the code of Android OS I use. Also I wanna say, thank you for taking your time. Have a beautiful day.. – efkan May 23 '18 at 19:00