我无法在 android 后台提供 运行 服务

I can not run service in android background

我在 android 后台 运行ning 服务,我遇到了一些问题。 在 Android Oreo 版本上,我尝试 运行 使用 AlarmService 和 android 中的 StartForegroundService() 无休止的服务。
但是我没有结果。

我的测试设备是小米、YunOS、华为android版本是9.0、4.4和YunOS 5.1
首先,当 mainactivity 运行 和 next 关闭时,在 onResume()onPause() 函数中,我使用 Receiver startforegroundservice()startservice() 函数开始服务。
有一些代码如下:

- MainActivity

public void onResume() {
    mContext.stopService(new Intent(mContext, AppService.class));
}

public void onPause() {
    Intent intent = new Intent(mContext, RestartReceiver.class);
    intent.setAction("ACTION_REBOOT");
    mContext.sendBroadcast(intent);
}

-RestartReceiver

@Override
public void onReceive(Context context, Intent intent) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
    {
        context.startForegroundService(new Intent(context, 
        AppForegroundService.class));
    } else {
        context.startService(new Intent(context, AppService.class));
    }
}

- AppForegroundService



     @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            ToastUtil.showToast(this, "AppForegroundService onStartCommand");
            Log.d(TAG, "onStartCommand");
            startForeground(startId, getStartNotification());
            startService(new Intent(this, AppService.class));
            stopSelf();
            return START_STICKY;
        }

- AppService

@Override
public void onDestroy() {
    RestartAlarm();
    Log.d(TAG, "onDestroy: service done");
    ToastUtil.showToast(this, "onDestroy: service done");
}
private void RestartAlarm()
{
    ToastUtil.showToast(this, "Start AlarmService");
    Intent intent = new Intent(this, RestartReceiver.class);
    intent.setAction("ACTION_RESTART");
    sendBroadcast(intent);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, 
    intent, 0);
    long firstTime = SystemClock.elapsedRealtime();
    Log.d(TAG, "RestartAlarm: " + firstTime);
    firstTime += 1000;

    AlarmManager alarmManager = (AlarmManager) 
    getSystemService(Context.ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
    firstTime, 1000, pendingIntent);
}

- Manifest.xml

<service
    android:name=".service.AppService"
    android:enabled="true"
    android:exported="true"
    android:stopWithTask="false"/>
<service android:name=".service.AppForegroundService"
    android:enabled="true"
    android:exported="true"/>
<receiver
    android:enabled="true"
    android:exported="true"
    android:name=".service.AutoStartReceiver"
    android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
    <intent-filter>
        <action android:name="ACTION_REBOOT" />
        <action android:name="android.intent.action.BOOT_COMPLETED" 
        android:priority="999"/>
        </intent-filter>
</receiver>
<receiver
    android:name=".service.RestartReceiver"
    android:enabled="true">
    <intent-filter>
        <action android:name="ACTION_RESTART"/>
    </intent-filter>
</receiver>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" 
/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" 
/>
<uses-permission
    android:name="android.permission.READ_PHONE_STATE"
    android:required="false" />
<!-- 网络 -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" 
/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" 
/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission
    android:name="android.permission.ACCESS_FINE_LOCATION"
    android:required="false" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<!-- added from 2.7.2 -->
<uses-permission
    android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"
    android:required="false" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission 
android:name="android.permission.INSTANT_APP_FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission 
android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

<uses-permission android:name="android.permission.SET_ACTIVITY_WATCHER" 
/>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.VIBRATE" />

我做错了什么?

当应用程序被 closed 时,一些设备制造商不允许在后台 运行 服务,你无能为力它是 os 杀死服务但是你如果您确实需要 运行 后台服务,需要给您的应用自启动权限!