0

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();
   }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Lee
  • 255
  • 4
  • 16
  • 1
    _"When an app goes into the background, it has a window of several minutes in which it is still allowed to create and use services. At the end of that window, the app is considered to be idle. At this time, the system stops the app's background services"_ https://developer.android.com/about/versions/oreo/background#services – Michael Sep 20 '18 at 15:50
  • See also https://developer.android.com/reference/android/support/v4/app/JobIntentService – Michael Sep 20 '18 at 16:00
  • That makes sense and is exactly how it is acting. I've read that page 100 times and I don't know how I missed that part about the time limit, but its there. lol. Is there a way to keep it from going idle? From your second response, you are recommending JobIntentService? Right? Do you know of any example code, off hand, that you know works? – Lee Sep 20 '18 at 16:02
  • The best solution might be to switch to a `JobIntentService` (see the second link I posted). It's supposed to work across all Android versions (well, from API level 4 and later anyway). – Michael Sep 20 '18 at 16:05
  • Excellent. I'll pursue that as my objective then. I saw it before and was concerned about its backwards compatibility. Many thanks – Lee Sep 20 '18 at 16:07

0 Answers0