首页 > 解决方案 > Hello World Android IntentService - IntentService 的 enqueueWork() 函数不接受参数 MyIntentService.class

问题描述

我只想写一个“Hello World IntentService App”。不幸的是,我做不到。

问题:

MyIntentService.enqueueWork() 方法不起作用。

IntentService 的 enqueueWork() 函数不接受参数 MyIntentService.class

我在谷歌和 YouTube 上搜索了很多,但找不到任何有用的东西。非常感谢您提前。

资料来源:

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

第 1 步:创建一个新的 Android 项目

第二步:新建Service File -> New -> Service -> Service (IntentService) (只留下名字: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);

// 在运行前给出这个错误 enqueueWork 的错误 -> "Cannot resolve method 'enqueueWork(android.content.Context, java.lang.Class, int, android.content.Intent)'"

// 第 5 步:现在,我运行应用程序,并给出以下错误:

编译失败;有关详细信息,请参阅编译器错误输出。

错误:找不到符号方法 enqueueWork(Context, Class <- MyIntentService ->, int, Intent)

IntentService 的 enqueueWork() 函数不接受参数 MyIntentService.class


非常感谢!

标签: android

解决方案


代替:

MyIntentService.enqueueWork()

它应该是:

JobIntentService.enqueueWork(getApplicationContext(), MyIntentService.class, JOB_ID, mServiceIntent);

推荐阅读