明确的意图不适用于 Android 8.1

Explicit intents don't works on Android 8.1

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

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

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

有机会解决这个问题吗?

注意:我尝试在我的 phone 上运行自定义 ROM,但我无法在 Android 模拟器上模拟这种情况。

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

    }

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

要么 ActivityRecognitionServiceBroadcastReceiver 要么 Service。两者不能同时存在。这不会根据设备 运行 的 OS 版本而改变。而且由于 ActivityRecognitionService extends IntentServicePendingIntent.getBroadcast() 不会给你一个有用的 PendingIntent.

还有:

  • MyService 未使用,因为它不在您的清单片段中并且未被您的其他代码片段引用

  • MyService 上的 runServiceAtForeground() 方法创建了一个指向 MyService 的 activity PendingIntent,但是 MyService 是不是 activity,所以这将不起作用