0

I tested a BroadcastReceiver which runs a JobIntentService in emulator, that worked successfully .

but it fails when i test the code to my physical phone ASUS ZE551ML .

Manifest file :

<?xml version="1.0" encoding="utf-8"?>

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

<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">
  <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=".MyBroadcastReceiver" android:enabled="true" android:exported="true">
        <intent-filter>
            <category android:name="android.intent.category.DEFAULT"/>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />
        </intent-filter>
  </receiver>

  <service android:name=".MyJobIntentService"
        android:permission="android.permission.BIND_JOB_SERVICE"

        android:exported="false"/>
</application>

BroadcastReceiver class :

package com.packt.backgroundservicedemo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class MyBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // Write Code..

        if(Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
            Intent i = new Intent(context, MyJobIntentService.class);
            i.putExtra("sleepTime", 12);
            MyJobIntentService.enqueueWork(context,i);
//            context.startService(i);

        }


    }
}

JobIntentService class :

package com.packt.backgroundservicedemo;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.app.JobIntentService;
import android.util.Log;
import android.widget.Toast;


public class MyJobIntentService extends JobIntentService {

    static final int JOB_ID = 15;


    static void enqueueWork(Context context,Intent intent){
        enqueueWork(context,MyJobIntentService.class,JOB_ID,intent);
    }

    private static final String TAG = MyJobIntentService.class.getSimpleName();

    @Override
    public void onCreate() {
        super.onCreate();
        Toast.makeText(this,"Task Execution Started",Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onHandleWork(@NonNull Intent intent) {

        // Write code here..
        Log.i(TAG,"onHandleWork(), Thread name:" + Thread.currentThread().getName());

        int duration = intent.getIntExtra("sleepTime",-1);
        int ctr = 1;
        //Dummy long operation
        while (ctr<=duration){
            Log.i(TAG,"Time elapsed " + ctr + " secs");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            ctr++;
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this,"Task Execution Finished",Toast.LENGTH_SHORT).show();



    }
}

Repeat : the code works well at emulator devices, pre Oreo and Oreo devices.

The log doesn't contain any related boot_completed reveiced for my application.

Thanks.

Hamza Hajeir
  • 119
  • 1
  • 8

2 Answers2

0

My bet is - your emulator doesn't have a passcode lock, while your real device does. BOOT_COMPLETED is broadcast only after unlocking with the passcode, and you'll have to wait for the other BroadcastReceivers to receive this event (other apps that registered for this event), depending on where you're in the queue of the system.

Arseny Levin
  • 664
  • 4
  • 10
  • Thanks Arseny, but the application doesn't receive `broadcast receiver` also after unlocking the device. Also i checked the log and saw some apps receiving `boot_completed` receiver. – Hamza Hajeir Jul 16 '18 at 14:04
  • Is there any specific code or actions i can do to support ASUS mobiles ? I feel the problem comes from it. – Hamza Hajeir Jul 16 '18 at 14:08
  • Don't know specifically about ASUS. Perhaps check if they have their own permission manager (like Xiaomi does), which requires using their specific API. – Arseny Levin Jul 16 '18 at 15:13
0

There's an Auto-start Manager application which was denying my app to run at startup.

Asus Auto-start Manager

Now my application works well at start.

Thanks @Arseny Levin for the hint.

Hamza Hajeir
  • 119
  • 1
  • 8