Skip to content

Instantly share code, notes, and snippets.

@biswa-rx
Created April 10, 2023 15:33
Show Gist options
  • Save biswa-rx/d4d5e52a6f6c446262634c94f7116511 to your computer and use it in GitHub Desktop.
Save biswa-rx/d4d5e52a6f6c446262634c94f7116511 to your computer and use it in GitHub Desktop.
JobIntentService Tutorial
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">
<EditText
android:id="@+id/edit_text_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Input" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="enqueueWork"
android:text="Enqueue Work" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.codinginflow.jobintentserviceexample">
<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=".ExampleJobIntentService"
android:permission="android.permission.BIND_JOB_SERVICE" />
</application>
</manifest>
package com.codinginflow.jobintentserviceexample;
import android.content.Context;
import android.content.Intent;
import android.os.SystemClock;
import android.support.annotation.NonNull;
import android.support.v4.app.JobIntentService;
import android.util.Log;
public class ExampleJobIntentService extends JobIntentService {
private static final String TAG = "ExampleJobIntentService";
static void enqueueWork(Context context, Intent work) {
enqueueWork(context, ExampleJobIntentService.class, 123, work);
}
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate");
}
@Override
protected void onHandleWork(@NonNull Intent intent) {
Log.d(TAG, "onHandleWork");
String input = intent.getStringExtra("inputExtra");
for (int i = 0; i < 10; i++) {
Log.d(TAG, input + " - " + i);
if (isStopped()) return;
SystemClock.sleep(1000);
}
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy");
}
@Override
public boolean onStopCurrentWork() {
Log.d(TAG, "onStopCurrentWork");
return super.onStopCurrentWork();
}
}
package com.codinginflow.jobintentserviceexample;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private EditText editTextInput;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextInput = findViewById(R.id.edit_text_input);
}
public void enqueueWork(View v) {
String input = editTextInput.getText().toString();
Intent serviceIntent = new Intent(this, ExampleJobIntentService.class);
serviceIntent.putExtra("inputExtra", input);
ExampleJobIntentService.enqueueWork(this, serviceIntent);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment