0

When I start JobIntentService, it works fine, but after entering phone-sleep it suspends after some time. When I unlock my phone, it starts working again.

I want to have a long task working in background, it have to not suspend or stop.

Here's my JobInteneService:

 public static void enqueueWork(Context context, Intent work) {
        enqueueWork(context, ExampleJob.class, 1, work);
    }

    @Override
    public void onCreate() {

        Log.d(TAG, "onCreate() called");
        super.onCreate();
    }


    @Override
    protected void onHandleWork(@NonNull Intent intent)
    {
        cancelRingtone = Uri.parse("android.resource://com.example.myapplication/" + R.raw.cancel);
        cancelAlarm = RingtoneManager.getRingtone(this, cancelRingtone);

        while(running)
        {
            cancelAlarm.play();
            try
            {
                Thread.sleep(60000);
            }
            catch (Exception e)
            {
                Log.i(TAG, "onHandleWork: "+e);
                running=false;
            }
        }
    }

    @Override
    public void onDestroy() {

        Log.d(TAG, "onDestroy() called");
        super.onDestroy();
    }

Here's MainActivity:

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

        ActivityCompat.requestPermissions(this,new String[]{
                Manifest.permission.WAKE_LOCK
        }, 1);
    }

    public void click(View view)
    {
        Intent mIntent = new Intent(this, ExampleJob.class);
        ExampleJob.enqueueWork(this, mIntent);

    }

Here's AndroidManifest.xml:

<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>
        <service android:name=".ExampleJob"
            android:permission="android.permission.BIND_JOB_SERVICE"/>

    </application>

With this code my phone will play beep sound every minute. In phone-sleep mode I count up to 6 beeps in half an hour. Did I implement JobIntentService properly? If JobIntentService isn't made for that work, what else should I use?

  • It seems, using alarm manager is best for this case – Eren Tüfekçi Nov 03 '19 at 21:37
  • This is an example, but I want to make an app like this: [link](https://play.google.com/store/apps/details?id=in.abhisheknair.motionalarm) , so this `service` has to be enabled all the time instead of once a minute or something. – Mateusz Szwedka Nov 06 '19 at 14:27
  • Looked for the app. It seems developer not uses services. He/she possibly is using context based sensor changes broadcasts(When the app is force quited. Alarm became deactive ). – Eren Tüfekçi Nov 06 '19 at 21:01

0 Answers0