0

I try to start a background service on BOOT_COMPLETE intent. First I try to simply startService from the broadcast receiver. According to some Stackoverflow posts, it seems to be impossible: "Not allowed to start service, app is in background "

The proposed solution was to use a SchedulerJob.

Then I use the following post to do it: https://stackoverflow.com/a/57681267/9203344

Here is the MainActivity I made:

package com.tee.tee

import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import android.util.Log;

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        Log.d("teeMainActivity", "oncreate");
        Util.schedulerJob(getApplicationContext());


    }
}

Here is the broadcadt receiver I made:

package com.tee.tee;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;




public class teeBroadCastReceiver extends BroadcastReceiver {

    private static final String TAG = "teeBroadCastReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
                    Log.d(TAG, "intent.getAction() " + intent.getAction());

        if( Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())){
            Log.d(TAG, "Receive boot complete broadcast");
            // Intent activityIntent = new Intent(context, MainActivity.class);
            // activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            // context.startActivity(activityIntent);
            // Intent serviceIntent = new Intent(context, teeService.class);
            // context.startService(serviceIntent);
            Util.schedulerJob(context);

        }
    }
}

The service:

package com.tee.tee;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;
import android.app.job.JobParameters;
import android.app.job.JobService;
import android.widget.Toast;

public class teeService extends JobService {
@Override
public boolean onStartJob(JobParameters params) {

    Log.d("teeService", "je suis la");
    Util.schedulerJob(getApplicationContext()); // reschedule the job
    Toast.makeText(this, "Bg Service", Toast.LENGTH_SHORT).show();
    return true;
}

@Override
public boolean onStopJob(JobParameters params) {
    return true;
  }
 }

And the Util file, like in the previous post:

package com.tee.tee;

import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.content.ComponentName;
import android.content.Context;
import android.util.Log;

public class Util {
// schedule the start of the service every 10 - 30 seconds
public static void schedulerJob(Context context) {
    Log.d("schedulerJob", "00");
    ComponentName serviceComponent = new ComponentName(context,teeService.class);
    JobInfo.Builder builder = new JobInfo.Builder(0,serviceComponent);
    Log.d("schedulerJob", "01");

    builder.setMinimumLatency(1*1000);    // wait at least
    builder.setOverrideDeadline(3*1000);  //delay time
    builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED);  // require unmetered network
    builder.setRequiresCharging(false);  // we don't care if the device is charging or not
    builder.setRequiresDeviceIdle(true); // device should be idle
    System.out.println("(scheduler Job");
    Log.d("schedulerJob", "02");

    JobScheduler jobScheduler = null;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
        jobScheduler = context.getSystemService(JobScheduler.class);
    }
    Log.d("schedulerJob", "03");

    jobScheduler.schedule(builder.build());
    Log.d("schedulerJob", "04");
   }
  }

When I start the MainActivity, the toast works properly. But When I restart my phone. The broadcast receiver receive the BOOT_COMPLETE , the schedulerJob log are display but the service doesn't seem to work (no log).

Can someone help me about that?

Edit:

Here is my manisfest:

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

    <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/Theme.tee">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name="com.tee.tee.teeBroadCastReceiver" android:enabled="true" android:exported="true" android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
            <intent-filter>
                <category android:name="android.intent.category.DEFAULT" />
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.ACTION_BOOT_COMPLETED" />
                <action android:name="android.intent.action.QUICKBOOT_POWERON" />
                <action android:name="com.htc.intent.action.QUICKBOOT_POWERON" />
            </intent-filter>
        </receiver>
        <service android:name=".teeService"
                 android:permission="android.permission.BIND_JOB_SERVICE"
                android:label="Word service"></service>
    </application>
</manifest>
dev dev
  • 109
  • 15

0 Answers0