Search code examples
androidnotificationsbackgroundintentservice

I can not run service in android background


I am running service in android background, and I have some problem. Over Android Oreo version, I tried to run endless service using AlarmService and StartForegroundService() in android.
but I have got no result.

My testing device is Xiaomi, YunOS, Huawei and android version is 9.0, 4.4 and YunOS 5.1
At first, when mainactivity run and next close, in onResume() and onPause() function, I started service with startforegroundservice() and startservice() function using Receiver.
There are some codes with the followings:

- 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" />

What am I doing wrong?


Solution

  • You can do nothing about that some device manufacturers doesn't allow services to run in background when app is closed it's the os which kills the service however you need to give your app self start permission if it's really necessary for you to run service in background!