2

I just want to write a "Hello World IntentService App". Unfortunately, I could not do that.

Problem:

MyIntentService.enqueueWork() method does not work.

enqueueWork() function of IntentService does not accept parameter MyIntentService.class

I searched in Google and YouTube a lot, but could not find anything helpful. Thank you very much in advance.

Sources:

https://developer.android.com/training/run-background-service/create-service

https://developer.android.com/training/run-background-service/send-request

https://developer.android.com/reference/android/support/v4/app/JobIntentService#enqueuework

Step 1: Create a new Android project

Step 2: Create a new Service File -> New -> Service -> Service (IntentService) (Just leave the name: MyIntentService)

// Step 3:
// Open: MyIntentService.java
// Just add a Toast in the method onHandleIntent() :

Toast.makeText(this, "Hello World!", Toast.LENGTH_SHORT).show();

// Step 4:
// Open: MainActivity.java. 
// Add below code in an appropriate method:

Intent mServiceIntent = new Intent();
mServiceIntent.putExtra("name", "Harun");

int JOB_ID = 1000;
MyIntentService.enqueueWork(getApplicationContext(), MyIntentService.class, JOB_ID, mServiceIntent);

// Gives this error before run Error for enqueueWork -> "Cannot resolve method 'enqueueWork(android.content.Context, java.lang.Class, int, android.content.Intent)'"

// Step 5: Now, I run the application, and gives this error:

Compilation failed; see the compiler error output for details.

error: cannot find symbol method enqueueWork(Context, Class <- MyIntentService ->, int, Intent)

enqueueWork() function of IntentService does not accept parameter MyIntentService.class


Thanks a lot!

1 Answers1

3

Instead of:

MyIntentService.enqueueWork()

it should be:

JobIntentService.enqueueWork(getApplicationContext(), MyIntentService.class, JOB_ID, mServiceIntent);
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
  • Thank you very much Rafsan Ahmad! This solved my problem. But I still have one next problem: This works: MyIntentService.startActionFoo(getApplicationContext(), "aaa", "bbb"); But this does not work, crashes: Intent mServiceIntent = new Intent(); int JOB_ID = 1000; JobIntentService.enqueueWork(getApplicationContext(), MyIntentService.class, JOB_ID, mServiceIntent); Thanks a lot! – Harun Altay Jan 15 '19 at 22:32
  • If it crashes, then add the crash log in the question. Maybe crash occur in other lines of code. – rafsanahmad007 Jan 15 '19 at 22:38
  • Exception is here -> JobIntentService.class -> public static void enqueueWork(@- NonNull Context context, @NonNull ComponentName component, int jobId, @- NonNull Intent work) { if (work == null) { throw new IllegalArgumentException("work must not be null"); } else { } } } Intent/work is not null, but it says null. I could not understand. Thanks. – Harun Altay Jan 15 '19 at 23:07
  • 1
    This means that there is a mistake in Google web page: https://developer.android.com/training/run-background-service/send-request RSSPullService.enqueueWork(getContext(), RSSPullService.class, RSS_JOB_ID, mServiceIntent); Am I wrong? – Harun Altay Jan 16 '19 at 00:41
  • pls provide the full logcat stacktrace from your android studio. https://stackoverflow.com/questions/16817566/restore-logcat-window-within-android-studio Without the logcat it is difficult to debug the problem. Also you can create a new question with your exception -- so everybody can take a look. @HarunAltay – rafsanahmad007 Jan 16 '19 at 12:27