My background service starts after a reboot. So why is it being stopped soon after?

45 views Asked by At

First, it has been a while since I've done any Android development and I'm no expert by any means. But what I'm trying to do, I've done before and it worked great; however, it is my understanding that Android updates may have changed how I need to do what I'm trying to do. On to my problem.

I'm working on a project that needs periodic communication with a remote server. In some cases, the work is scheduled and sometimes it is reacting to some event on the phone. In short, I need a background service that operates without bothering the user and will provide notification(s) as necessary. I've written a service, as I know how, I've researched the internet, compared it to what I've done before, and I keep getting the same problem. In any case, I've broken everything down to the simplest of services and the problem still exists.

My Note 9 device is running Android 8.1.0; however, I need broad compatibility. I'm just using this device for testing while trying to keep compatibility in mind. But I can't even get the basic functionality to work.

I've inserted code to ring the phone every so often, from the service. I removed it for simplicity and to make sure that wasn't causing the problem. When I execute the service from the app/activity, everything works fine and the service continues to execute. When I exit the app/activity, the service continues.

When I reboot the phone, the service executes as expected. Over a period of a minute, or so, I get the following notice. Why? What am I doing wrong? What changes have taken place that I need to adjust to?

enter image description here

The following is my code.

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    android:installLocation="internalOnly"
    package="com.example.exampleservice">

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <receiver
            android:name="com.example.exampleservice.BootReceiver">
            <intent-filter android:priority="0">
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

        <service android:name=".MyService" />

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

</manifest>

MainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button buttonStart;
    private Button buttonStop;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        buttonStart = (Button) findViewById(R.id.buttonStart);
        buttonStop = (Button) findViewById(R.id.buttonStop);

        buttonStart.setOnClickListener(this);
        buttonStop.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (view == buttonStart) {
            startService(new Intent(this, MyService.class));
        } else if (view == buttonStop) {
            stopService(new Intent(this, MyService.class));
        }
    }
}

BootReceiver.java

public class BootReceiver extends BroadcastReceiver {
   @Override
   public void onReceive(Context context, Intent intent)
   {    if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
           Intent serviceIntent = new Intent(context, MyService.class);
           context.startService(serviceIntent);
       }
   }
}

MyService.java

public class MyService extends Service {
   @Override
   public int onStartCommand(Intent intent, int flags, int startId) {
       return START_STICKY;
   }

   @Nullable
   @Override
   public IBinder onBind(Intent intent) {
       return null;
   }

   @Override
   public void onRebind(Intent intent) {
   }

   @Override
   public boolean onUnbind(Intent intent) {
       return true;
   }

   @Override
   public void onDestroy() {
       super.onDestroy();
   }
}
0

There are 0 answers