-1

下面的代码在奥利奥之前的版本上没有任何问题。

尽管我使用startForeground方法并explicit有意获取 Activity 值,onHandleIntent但从未调用方法。

我找不到有关此问题的任何问题或解决方案。

有没有机会克服这个问题?

注意:我在运行自定义 ROM 的手机上进行了尝试,但我无法在 Android Emulator 上模拟这种情况。

AndroidManifest.xml文件

<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文件

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文件

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

    }

}
4

1 回答 1

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

要么ActivityRecognitionService是一个BroadcastReceiver ,要么是一个Service。它不能同时是两者。这不会根据设备运行的操作系统版本而改变。而既然ActivityRecognitionService extends IntentServicePendingIntent.getBroadcast()不会给你一个有用的PendingIntent

还:

  • MyService未使用,只要它不在您的清单片段中并且没有从您的其他代码片段中引用

  • runServiceAtForeground()on 方法MyService创建一个PendingIntent指向的活动MyService,但MyService不是一个活动,因此这不起作用

于 2018-05-23T12:23:00.583 回答